PostGIS  3.2.2dev-r@@SVN_REVISION@@

◆ ptarray_raycast_intersections()

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

Definition at line 818 of file ptarray.c.

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

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

Referenced by lwcompound_contains_point().

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