Inpolygon usage

How can we generate following Octave codes in Julia with GeometricalPredicates package

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];
 [x,y] = meshgrid (xa, ya);
 [inn,on] = inpolygon (x, y, xv, yv);
 inside = inn & ! on;

What is the correct usage of inpolygon function of GeometricalPredicates package in this case?

GeometricalPredicates is not very convenient to use, since it requires all input coordinates to be in the range [1,2]. Also, it seems to only support inpolygon for convex polygons.
If you don’t need the very high precision near the edges that GeometricalPredicates seems to be trying to achieve, I recommend using Luxor instead:

using Luxor

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 = Point.(xv,yv)
points = vec(Point.(xa',ya))

inside = [isinside(p, polygon; allowonedge=true) for p in points]

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

tmp

6 Likes

This is very good. Thank you.

If you want something more lightweight and flexible you can try PolygonOps, inpolygon function.

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)
6 Likes

Thank you. This is good.

After some experiments, I have found that PolygonOps is better suited for me. Thanks again.

@sjkelly’s example should be in the docs :slight_smile:
https://juliageometry.github.io/PolygonOps.jl/stable/

Good idea! Added :slight_smile: