Skip to content

Commit

Permalink
Compute the cell area for an infinite polygon
Browse files Browse the repository at this point in the history
* Cell area calculation for an infinity polygon

* Add forgotten tests
  • Loading branch information
AaronGhost authored Apr 16, 2024
1 parent 8e125c8 commit fe3ab83
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Delaunator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export triangulate, basictriangulation, update!, triangles, points, inhull

include("quicksort.jl")
include("geometry.jl")
export isinfinite, dualcell, firstpoint, lastpoint, segments
export isinfinite, dualcell, firstpoint, lastpoint, segments, cellarea

include("clipping.jl")
export clippedpoly, clippedpoly!, margin_bbox
Expand Down
29 changes: 29 additions & 0 deletions src/geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,35 @@ function dualcell(t::Triangulation, centers, i::Integer)
raystart, rayend)
end

"""
cellarea(p)
Compute the area of a possibly infinite polygon.
"""
function cellarea(p::InfinitePolygon)
if isinfinite(p)
return typemax(eltype(eltype(p)))
# Area of a line is zero
elseif length(p.points) < 3
return zero(eltype(eltype(p)))
else
# Compute the area of the polygon using the shoelace formula using iterators
area = zero(eltype(eltype(p)))
previous, ite = iterate(p.points)
while true
el = iterate(p.points, ite)
if isnothing(el)
break
end
current, ite = el
area += (previous[1] + current[1]) * (previous[2] - current[2])
previous = current
end
current = first(p.points)
return 0.5 * abs(area + (previous[1] + current[1]) * (previous[2] - current[2]))
end
end

# monotonically increases with real angle, but doesn't need expensive trigonometry
function pseudoAngle(dx, dy)
p = dx / (abs(dx) + abs(dy))
Expand Down
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ end
end
end

@testset "cellarea" begin
# Test Infinite case
p = Delaunator.InfinitePolygon([(-5.0,0.0)], (0.0,-1.0), (0.0,1.0))
@test cellarea(p) == Inf
# Test Degenerate (point, segment) case
p = Delaunator.InfinitePolygon([(-5.0,0.0)], (0.0, 0.0), (0.0, 0.0))
@test cellarea(p) == 0.0
p = Delaunator.InfinitePolygon([(-5.0,0.0), (5.0, 0.0)], (0.0, 0.0), (0.0, 0.0))
@test cellarea(p) == 0.0
# Test normal case
p = Delaunator.InfinitePolygon([(-1.0,-1.0),(1.0,-1.0),(1.0,1.0),(-1.0,1.0)], (0.0,0.0), (0.0,0.0))
@test cellarea(p) == 4.0
end

# test('triangulates plain array', (t) => {
# const d = new Delaunator([].concat(...points));
# t.same(d.triangles, Delaunator.from(points).triangles);
Expand Down

0 comments on commit fe3ab83

Please sign in to comment.