PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ pt_in_ring_2d()

int pt_in_ring_2d ( const POINT2D p,
const POINTARRAY ring 
)

Definition at line 280 of file lwalgorithm.c.

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