PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ pt_in_ring_2d()

int pt_in_ring_2d ( const POINT2D p,
const POINTARRAY ring 
)

Definition at line 279 of file lwalgorithm.c.

280 {
281  int cn = 0; /* the crossing number counter */
282  uint32_t i;
283  const POINT2D *v1, *v2;
284  const POINT2D *first, *last;
285 
286  first = getPoint2d_cp(ring, 0);
287  last = getPoint2d_cp(ring, ring->npoints-1);
288  if ( memcmp(first, last, sizeof(POINT2D)) )
289  {
290  lwerror("pt_in_ring_2d: V[n] != V[0] (%g %g != %g %g)",
291  first->x, first->y, last->x, last->y);
292  return LW_FALSE;
293 
294  }
295 
296  LWDEBUGF(2, "pt_in_ring_2d called with point: %g %g", p->x, p->y);
297  /* printPA(ring); */
298 
299  /* loop through all edges of the polygon */
300  v1 = getPoint2d_cp(ring, 0);
301  for (i=0; i<ring->npoints-1; i++)
302  {
303  double vt;
304  v2 = getPoint2d_cp(ring, i+1);
305 
306  /* edge from vertex i to vertex i+1 */
307  if
308  (
309  /* an upward crossing */
310  ((v1->y <= p->y) && (v2->y > p->y))
311  /* a downward crossing */
312  || ((v1->y > p->y) && (v2->y <= p->y))
313  )
314  {
315 
316  vt = (double)(p->y - v1->y) / (v2->y - v1->y);
317 
318  /* P->x <intersect */
319  if (p->x < v1->x + vt * (v2->x - v1->x))
320  {
321  /* a valid crossing of y=p->y right of p->x */
322  ++cn;
323  }
324  }
325  v1 = v2;
326  }
327 
328  LWDEBUGF(3, "pt_in_ring_2d returning %d", cn&1);
329 
330  return (cn&1); /* 0 if even (out), and 1 if odd (in) */
331 }
#define LW_FALSE
Definition: liblwgeom.h:94
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
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:390
double x
Definition: liblwgeom.h:390
uint32_t npoints
Definition: liblwgeom.h:427

References getPoint2d_cp(), LW_FALSE, LWDEBUGF, lwerror(), POINTARRAY::npoints, POINT2D::x, and POINT2D::y.

Here is the call graph for this function: