Skip to content
Projects

Polygon

The equations of simgeometry.objects.polygon, declared on the code that implements them and checked by the tests.

Polygon

classsource

Represent a polygon geometry object.

inside
toggle at edge vjvi  ⟺  (yi>y)≠(yj>y)  and  x<(xj−xi)(y−yi)(yj−yi)+10−16+xi\text{toggle at edge } v_j v_i \iff (y_i > y) \ne (y_j > y) \;\text{and}\; x < \frac{(x_j - x_i)(y - y_i)}{(y_j - y_i) + 10^{-16}} + x_i
level set
ϕ(p)=±min⁡edges∣p−a−clip⁡ ⁣((p−a)⋅e∣e∣2,0,1)e∣,e=b−a\phi(p) = \pm \min_\text{edges} \left| p - a - \operatorname{clip}\!\left(\tfrac{(p - a)\cdot e}{|e|^2}, 0, 1\right) e \right|, \qquad e = b - a
Assumes
  • the even-odd (crossing-number) rule along a ray towards +x+x, not a winding number; vjv_j is the vertex before viv_i, the closing edge implied
  • half-open in yy, so a vertex on the ray is counted once; 10−1610^{-16} guards horizontal edges
  • inside also needs the half-open bounding box l≤p<ul \le p < u; the compiled kernel contains_polygon_2d runs the test
  • ϕ\phi: the distance to the nearest edge, negative where contains_points is true; exact for a simple polygon
References
  • W. R. Franklin, PNPOLY - Point Inclusion in Polygon Test
  • M. Shimrat, Algorithm 112: Position of point relative to polygon, Commun. ACM 5, 434 (1962)
  • Quílez, 2D distance functions