PolygonOps.jl

inpolygon

PolygonOps.inpolygonFunction
inpolygon(p, poly)
inpolygon(p, poly, [::MembershipCheckAlgorithm])
inpolygon(p, poly, [::MembershipCheckAlgorithm]; in = 1, on = -1, out = 0)

check the membership of p in poly where poly is an AbstractVector of AbstractVectors. poly should have the first and last elements equal.

Returns:

  • in = 1
  • on = -1
  • out = 0

MembershipCheckAlgorithm:

  • HaoSun()
  • HormannAgathos()

Default is HaoSun() as it has the best performance and works invariant of winding order and self-intersections. However the HaoSun algorithm is new and bugs may be possible. The classic HormannAgathos algorithm is provided, however it is sensitive to self-intersections and winding order so may produce different results.

Custom return values:

The return logic can be customized to return alternate values and types by specify the in, on, and out keywords. For example, to treat the 'on' and 'in' cases the same and return a Bool: inpolygon(p, poly, in=true, on=true, out=false)

Algorithm by Hao and Sun (2018): https://doi.org/10.3390/sym10100477

Hormann-Agathos (2001) Point in Polygon algorithm: https://doi.org/10.1016/S0925-7721(01)00012-8

source

Examples

using PolygonOps
using StaticArrays

xv = [ 0.05840, 0.48375, 0.69356, 1.47478, 1.32158, 
        1.94545, 2.16477, 1.87639, 1.18218, 0.27615, 
        0.05840 ]
yv = [ 0.60628, 0.04728, 0.50000, 0.50000, 0.02015, 
        0.18161, 0.78850, 1.13589, 1.33781, 1.04650, 
        0.60628 ]

xa = 0:0.1:2.3
ya = 0:0.1:1.4

polygon = SVector.(xv,yv)
points = vec(SVector.(xa',ya))

inside = [inpolygon(p, polygon; in=true, on=false, out=false) for p in points]

using Plots
plot(Tuple.(polygon), legend=false)
scatter!(Tuple.(points), marker_z=inside)

PolygonOps.area

PolygonOps.areaFunction
area(poly)

Calculate the signed area via the shoelace formula. If the points are ordered counterclockwise, the result will be positive. If the points are ordered clockwise, the result will be negative.

source