/* * return -1 iff point outside multipolygon * return 0 iff point on multipolygon boundary * return 1 iff point inside multipolygon */ int point_in_multipolygon(LWMPOLY *mpolygon, LWPOINT *point) { int i, j, result, in_ring; POINT2D pt; POSTGIS_DEBUG(2, "point_in_polygon called."); getPoint2d_p(point->point, 0, &pt); /* assume bbox short-circuit has already been attempted */ result = -1; for (j = 0; j < mpolygon->ngeoms; j++ ) { LWPOLY *polygon = mpolygon->geoms[j]; /* everything is outside of an empty polygon */ if ( polygon->nrings == 0 ) continue; in_ring = point_in_ring(polygon->rings[0], &pt); if ( in_ring == -1) /* outside the exterior ring */ { POSTGIS_DEBUG(3, "point_in_polygon: outside exterior ring."); continue; } if ( in_ring == 0 ) { return 0; } result = in_ring; for (i=1; i<polygon->nrings; i++) { in_ring = point_in_ring(polygon->rings[i], &pt); if (in_ring == 1) /* inside a hole => outside the polygon */ { POSTGIS_DEBUGF(3, "point_in_polygon: within hole %d.", i); result = -1; break; } if (in_ring == 0) /* on the edge of a hole */ { POSTGIS_DEBUGF(3, "point_in_polygon: on edge of hole %d.", i); return 0; } } if ( result != -1) { return result; } } return result; }
/* * return -1 iff point outside polygon * return 0 iff point on boundary * return 1 iff point inside polygon */ int point_in_polygon(LWPOLY *polygon, LWPOINT *point) { int i, result, in_ring; POINTARRAY *ring; POINT2D pt; POSTGIS_DEBUG(2, "point_in_polygon called."); getPoint2d_p(point->point, 0, &pt); /* assume bbox short-circuit has already been attempted */ ring = polygon->rings[0]; in_ring = point_in_ring(polygon->rings[0], &pt); if ( in_ring == -1) /* outside the exterior ring */ { POSTGIS_DEBUG(3, "point_in_polygon: outside exterior ring."); return -1; } result = in_ring; for (i=1; i<polygon->nrings; i++) { ring = polygon->rings[i]; in_ring = point_in_ring(polygon->rings[i], &pt); if (in_ring == 1) /* inside a hole => outside the polygon */ { POSTGIS_DEBUGF(3, "point_in_polygon: within hole %d.", i); return -1; } if (in_ring == 0) /* on the edge of a hole */ { POSTGIS_DEBUGF(3, "point_in_polygon: on edge of hole %d.", i); return 0; } } return result; /* -1 = outside, 0 = boundary, 1 = inside */ }