Skip to content

Commit

Permalink
volume method for AbstractPolygon
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Apr 10, 2023
1 parent ea182bb commit 0096118
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/src/lib/sets/HPolygon.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ Inherited from [`LazySet`](@ref):
Inherited from [`AbstractPolytope`](@ref):
* [`isempty`](@ref isempty(::AbstractPolytope))
* [`isuniversal`](@ref isuniversal(::AbstractPolytope{N}, ::Bool=false) where {N})
* [`volume`](@ref volume(::AbstractPolytope))

Inherited from [`AbstractPolygon`](@ref):
* [`dim`](@ref dim(::AbstractPolygon))
* [`volume`](@ref volume(::AbstractPolygon))

Inherited from [`AbstractHPolygon`](@ref):
* [`an_element`](@ref an_element(::AbstractHPolygon))
Expand Down
2 changes: 1 addition & 1 deletion docs/src/lib/sets/HPolygonOpt.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ Inherited from [`LazySet`](@ref):
Inherited from [`AbstractPolytope`](@ref):
* [`isempty`](@ref isempty(::AbstractPolytope))
* [`isuniversal`](@ref isuniversal(::AbstractPolytope{N}, ::Bool=false) where {N})
* [`volume`](@ref volume(::AbstractPolytope))

Inherited from [`AbstractPolygon`](@ref):
* [`dim`](@ref dim(::AbstractPolygon))
* [`volume`](@ref volume(::AbstractPolygon))

Inherited from [`AbstractHPolygon`](@ref):
* [`an_element`](@ref an_element(::AbstractHPolygon))
Expand Down
2 changes: 1 addition & 1 deletion docs/src/lib/sets/VPolygon.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Inherited from [`AbstractPolytope`](@ref):
* [`isbounded`](@ref isbounded(::AbstractPolytope))
* [`isempty`](@ref isempty(::AbstractPolytope))
* [`isuniversal`](@ref isuniversal(::AbstractPolytope{N}, ::Bool=false) where {N})
* [`volume`](@ref volume(::AbstractPolytope))

Inherited from [`AbstractPolygon`](@ref):
* [`dim`](@ref dim(::AbstractPolygon))
* [`volume`](@ref volume(::AbstractPolygon))
23 changes: 22 additions & 1 deletion src/Interfaces/AbstractPolygon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Return the ambient dimension of a convex polygon.
The ambient dimension of the polygon, which is 2.
"""
@inline function dim(P::AbstractPolygon)
@inline function dim(::AbstractPolygon)
return 2
end

Expand Down Expand Up @@ -182,3 +182,24 @@ arguments implements the [`atan2` function](https://en.wikipedia.org/wiki/Atan2)
function _leq_trig(u::AbstractVector{N}, v::AbstractVector{N}) where {N<:AbstractFloat}
return jump2pi(atan(u[2], u[1])) <= jump2pi(atan(v[2], v[1]))
end

"""
volume(P::AbstractPolygon)
Compute the volume of a convex polygon.
### Input
- `P` -- convex polygon
### Output
A number representing the volume of `P`.
### Notes
In 2D the volume is equivalent to the area.
"""
function volume(P::AbstractPolygon)
return area(P)
end
6 changes: 4 additions & 2 deletions test/Sets/Polygon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,15 @@ for N in [Float64, Float32, Rational{Int}]
Z = minkowski_sum(X + Y, Singleton(zeros(N, 2)))
@test Z isa VPolygon{N} && ispermutation(vertices_list(Z), vlist)

# area
# area & volume
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)
@test area(P) == volume(P) == N(21)
Q = tohrep(P)
@test area(Q) == volume(Q) == N(21)
end

for N in [Float64, Float32]
Expand Down

0 comments on commit 0096118

Please sign in to comment.