PostGIS  2.2.7dev-r@@SVN_REVISION@@
int pt_in_ring_2d ( const POINT2D p,
const POINTARRAY ring 
)

Definition at line 266 of file lwalgorithm.c.

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

267 {
268  int cn = 0; /* the crossing number counter */
269  int i;
270  const POINT2D *v1, *v2;
271  const POINT2D *first, *last;
272 
273  first = getPoint2d_cp(ring, 0);
274  last = getPoint2d_cp(ring, ring->npoints-1);
275  if ( memcmp(first, last, sizeof(POINT2D)) )
276  {
277  lwerror("pt_in_ring_2d: V[n] != V[0] (%g %g != %g %g)",
278  first->x, first->y, last->x, last->y);
279  return LW_FALSE;
280 
281  }
282 
283  LWDEBUGF(2, "pt_in_ring_2d called with point: %g %g", p->x, p->y);
284  /* printPA(ring); */
285 
286  /* loop through all edges of the polygon */
287  v1 = getPoint2d_cp(ring, 0);
288  for (i=0; i<ring->npoints-1; i++)
289  {
290  double vt;
291  v2 = getPoint2d_cp(ring, i+1);
292 
293  /* edge from vertex i to vertex i+1 */
294  if
295  (
296  /* an upward crossing */
297  ((v1->y <= p->y) && (v2->y > p->y))
298  /* a downward crossing */
299  || ((v1->y > p->y) && (v2->y <= p->y))
300  )
301  {
302 
303  vt = (double)(p->y - v1->y) / (v2->y - v1->y);
304 
305  /* P->x <intersect */
306  if (p->x < v1->x + vt * (v2->x - v1->x))
307  {
308  /* a valid crossing of y=p->y right of p->x */
309  ++cn;
310  }
311  }
312  v1 = v2;
313  }
314 
315  LWDEBUGF(3, "pt_in_ring_2d returning %d", cn&1);
316 
317  return (cn&1); /* 0 if even (out), and 1 if odd (in) */
318 }
int npoints
Definition: liblwgeom.h:355
double x
Definition: liblwgeom.h:312
#define LW_FALSE
Definition: liblwgeom.h:62
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:472
double y
Definition: liblwgeom.h:312
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:74

Here is the call graph for this function: