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

◆ ptarray_raycast_intersections()

int ptarray_raycast_intersections ( const POINTARRAY pa,
const POINT2D p,
int *  on_boundary 
)

Definition at line 827 of file ptarray.c.

828{
829 // A valid linestring must have at least 2 point
830 if (pa->npoints < 2)
831 lwerror("%s called on invalid linestring", __func__);
832
833 int intersections = 0;
834 double px = p->x;
835 double py = p->y;
836
837 // Iterate through each edge of the polygon
838 for (uint32_t i = 0; i < pa->npoints-1; ++i)
839 {
840 const POINT2D* p1 = getPoint2d_cp(pa, i);
841 const POINT2D* p2 = getPoint2d_cp(pa, i+1);
842
843 /* Skip zero-length edges */
844 if (p2d_same(p1, p2))
845 continue;
846
847 /* --- Step 1: Check if the point is ON the boundary edge --- */
848 if (lw_pt_on_segment(p1, p2, p))
849 {
850 *on_boundary = LW_TRUE;
851 return 0;
852 }
853
854 /* --- Step 2: Perform the Ray Casting intersection test --- */
855
856 /*
857 * Check if the horizontal ray from p intersects the edge (p1, p2).
858 * This is the core condition for handling vertices correctly:
859 * - One vertex must be strictly above the ray (py < vertex.y)
860 * - The other must be on or below the ray (py >= vertex.y)
861 */
862 if (((p1->y <= py) && (py < p2->y)) || ((p2->y <= py) && (py < p1->y)))
863 {
864 /*
865 * Calculate the x-coordinate where the edge intersects the ray's horizontal line.
866 * Formula: x = x1 + (y - y1) * (x2 - x1) / (y2 - y1)
867 */
868 double x_intersection = p1->x + (py - p1->y) * (p2->x - p1->x) / (p2->y - p1->y);
869
870 /*
871 * If the intersection point is to the right of our test point,
872 * it's a valid "crossing".
873 */
874 if (x_intersection > px)
875 {
876 intersections++;
877 }
878 }
879 }
880
881 return intersections;
882}
#define LW_TRUE
Return types for functions with status returns.
Definition liblwgeom.h:93
int lw_pt_on_segment(const POINT2D *p1, const POINT2D *p2, const POINT2D *p)
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
double x
Definition liblwgeom.h:390
uint32_t npoints
Definition liblwgeom.h:427

References getPoint2d_cp(), lw_pt_on_segment(), LW_TRUE, lwerror(), POINTARRAY::npoints, p2d_same(), POINT2D::x, and POINT2D::y.

Referenced by lwcompound_contains_point().

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