PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ ptarray_contains_point()

int ptarray_contains_point ( const POINTARRAY pa,
const POINT2D pt 
)

The following is based on the "Fast Winding Number Inclusion of a Point in a Polygon" algorithm by Dan Sunday.

http://softsurfer.com/Archive/algorithm_0103/algorithm_0103.htm#Winding%20Number

Return:

  • LW_INSIDE (1) if the point is inside the POINTARRAY
  • LW_BOUNDARY (0) if it is on the boundary.
  • LW_OUTSIDE (-1) if it is outside

Definition at line 755 of file ptarray.c.

756{
757 const POINT2D *seg1, *seg2;
758 int wn = 0;
759
760 seg1 = getPoint2d_cp(pa, 0);
761 seg2 = getPoint2d_cp(pa, pa->npoints-1);
762 if (!p2d_same(seg1, seg2))
763 lwerror("ptarray_contains_point called on unclosed ring");
764
765 for (uint32_t i = 1; i < pa->npoints; i++)
766 {
767 double side, ymin, ymax;
768
769 seg2 = getPoint2d_cp(pa, i);
770
771 /* Zero length segments are ignored. */
772 if (p2d_same(seg1, seg2))
773 {
774 seg1 = seg2;
775 continue;
776 }
777
778 ymin = FP_MIN(seg1->y, seg2->y);
779 ymax = FP_MAX(seg1->y, seg2->y);
780
781 /* Only test segments in our vertical range */
782 if (pt->y > ymax || pt->y < ymin)
783 {
784 seg1 = seg2;
785 continue;
786 }
787
788 side = lw_segment_side(seg1, seg2, pt);
789
790 /*
791 * A point on the boundary of a ring is not contained.
792 * WAS: if (fabs(side) < 1e-12), see #852
793 */
794 if ((side == 0) && lw_pt_in_seg(pt, seg1, seg2))
795 {
796 return LW_BOUNDARY;
797 }
798
799 /*
800 * If the point is to the left of the line, and it's rising,
801 * then the line is to the right of the point and
802 * circling counter-clockwise, so increment.
803 */
804 if ((side < 0) && (seg1->y <= pt->y) && (pt->y < seg2->y))
805 {
806 wn++;
807 }
808
809 /*
810 * If the point is to the right of the line, and it's falling,
811 * then the line is to the right of the point and circling
812 * clockwise, so decrement.
813 */
814 else if ( (side > 0) && (seg2->y <= pt->y) && (pt->y < seg1->y) )
815 {
816 wn--;
817 }
818
819 seg1 = seg2;
820 }
821
822 /* wn == 0 => Outside, wn != 0 => Inside */
823 return wn == 0 ? LW_OUTSIDE : LW_INSIDE;
824}
#define LW_INSIDE
Constants for point-in-polygon return values.
#define LW_BOUNDARY
#define FP_MAX(A, B)
#define FP_MIN(A, B)
int lw_pt_in_seg(const POINT2D *P, const POINT2D *A1, const POINT2D *A2)
Returns true if P is between A1/A2.
#define LW_OUTSIDE
int lw_segment_side(const POINT2D *p1, const POINT2D *p2, const POINT2D *q)
lw_segment_side()
Definition lwalgorithm.c:70
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition lwalgorithm.c:57
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition lwinline.h:97
double y
Definition liblwgeom.h:390
uint32_t npoints
Definition liblwgeom.h:427

References FP_MAX, FP_MIN, getPoint2d_cp(), LW_BOUNDARY, LW_INSIDE, LW_OUTSIDE, lw_pt_in_seg(), lw_segment_side(), lwerror(), POINTARRAY::npoints, p2d_same(), and POINT2D::y.

Referenced by _lwt_AddFaceSplit(), lw_dist2d_line_poly(), lw_dist2d_line_tri(), lw_dist2d_point_poly(), lw_dist2d_point_tri(), lw_dist2d_poly_poly(), lw_dist2d_tri_circstring(), lw_dist2d_tri_poly(), lw_dist2d_tri_tri(), lwgeom_contains_point(), lwgeom_solid_contains_lwgeom(), lwpoly_contains_point(), and test_ptarray_contains_point().

Here is the call graph for this function:
Here is the caller graph for this function: