Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute the cell area for an infinite polygon #15

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -52,6 +52,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
Loading