PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ pt_in_ring_2d()

int pt_in_ring_2d ( const POINT2D p,
const POINTARRAY ring 
)

Definition at line 297 of file lwalgorithm.c.

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