PostGIS  3.2.2dev-r@@SVN_REVISION@@

◆ pt_in_ring_2d()

int pt_in_ring_2d ( const POINT2D p,
const POINTARRAY ring 
)

Definition at line 290 of file lwalgorithm.c.

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

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

Here is the call graph for this function: