Skip to content

Commit

Permalink
add area method for VPolygon
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Apr 10, 2023
1 parent bec1500 commit ea182bb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
3 changes: 2 additions & 1 deletion docs/src/lib/sets/VPolygon.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ translate!(::VPolygon, ::AbstractVector)
remove_redundant_vertices(::VPolygon; ::String="monotone_chain")
remove_redundant_vertices!(::VPolygon; ::String="monotone_chain")
permute(::VPolygon, ::AbstractVector{Int})
linear_map(M::AbstractMatrix, P::VPolygon)
linear_map(::AbstractMatrix, ::VPolygon)
area(::VPolygon)
```
Inherited from [`LazySet`](@ref):
* [`norm`](@ref norm(::LazySet, ::Real))
Expand Down
21 changes: 21 additions & 0 deletions src/Sets/VPolygon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -711,3 +711,24 @@ The permuted polygon in vertex representation.
function permute(V::VPolygon, p::AbstractVector{Int})
return VPolygon([v[p] for v in V.vertices]; apply_convex_hull=true)
end

"""
area(V::VPolygon)
Compute the area of a polygon in vertex representation.
### Input
- `V` -- polygon in vertex representation
### Output
A number representing the area of `V`.
### Algorithm
See [`area(::LazySet)`](@ref).
"""
function area(V::VPolygon)
return _area_vlist(V.vertices; apply_convex_hull=false)
end
18 changes: 13 additions & 5 deletions test/Sets/Polygon.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
for N in [Float64, Float32, Rational{Int}]
# random polygons
rand(HPolygon)
rand(HPolygonOpt)
rand(VPolygon)

# Empty polygon
p = HPolygon{N}()

Expand Down Expand Up @@ -474,6 +469,14 @@ for N in [Float64, Float32, Rational{Int}]
@test Z isa VPolygon{N} && ispermutation(vertices_list(Z), vlist)
Z = minkowski_sum(X + Y, Singleton(zeros(N, 2)))
@test Z isa VPolygon{N} && ispermutation(vertices_list(Z), vlist)

# area
v1 = N[1, 3]
v2 = N[2, 1]
v3 = N[4, 6]
v4 = N[9, 2]
P = VPolygon([v1, v2, v3, v4])
@test area(P) == N(21)
end

for N in [Float64, Float32]
Expand Down Expand Up @@ -654,6 +657,11 @@ for N in [Float64]
@test isa(Pr, HPolygon{Rational{BigInt}, Vector{Rational{BigInt}}})
end

# random polygons
rand(HPolygon)
rand(HPolygonOpt)
rand(VPolygon)

# default Float64 constructors
@test HPolygon() isa HPolygon{Float64, Vector{Float64}}
@test HPolygonOpt() isa HPolygonOpt{Float64, Vector{Float64}}
Expand Down

0 comments on commit ea182bb

Please sign in to comment.