int LabelPosition::getNumPointsInPolygon( int npol, double *xp, double *yp ) { int a, k, count = 0; double px, py; // cheack each corner for ( k = 0; k < 4; k++ ) { px = x[k]; py = y[k]; for ( a = 0; a < 2; a++ ) // and each middle of segment { if ( isPointInPolygon( npol, xp, yp, px, py ) ) count++; px = ( x[k] + x[( k+1 ) %4] ) / 2.0; py = ( y[k] + y[( k+1 ) %4] ) / 2.0; } } px = ( x[0] + x[2] ) / 2.0; py = ( y[0] + y[2] ) / 2.0; // and the label center if ( isPointInPolygon( npol, xp, yp, px, py ) ) count += 4; // virtually 4 points // TODO: count with nextFeature return count; }
bool Polygon::isLineInterior(const Vertex &a, const Vertex &b) const { // Both points have to be in the polygon if (!isPointInPolygon(a, true) || !isPointInPolygon(b, true)) return false; // If the points are identical, the line is trivially within the polygon if (a == b) return true; // Test whether the line intersects a line segment strictly (proper intersection) for (int i = 0; i < vertexCount; i++) { int j = (i + 1) % vertexCount; const Vertex &vs = vertices[i]; const Vertex &ve = vertices[j]; // If the line intersects a line segment strictly (proper cross section) the line is not in the polygon if (Line::doesIntersectProperly(a, b, vs, ve)) return false; // If one of the two line items is on the edge and the other is to the right of the edge, // then the line is not completely within the polygon if (Line::isOnLineStrict(vs, ve, a) && Line::isVertexRight(vs, ve, b)) return false; if (Line::isOnLineStrict(vs, ve, b) && Line::isVertexRight(vs, ve, a)) return false; // If one of the two line items is on a vertex, the line traces into the polygon if ((a == vs) && !isLineInCone(i, b, true)) return false; if ((b == vs) && !isLineInCone(i, a, true)) return false; } return true; }
bool Polygon::isLineExterior(const Vertex &a, const Vertex &b) const { // Neither of the two points must be strictly in the polygon (on the edge is allowed) if (isPointInPolygon(a, false) || isPointInPolygon(b, false)) return false; // If the points are identical, the line is trivially outside of the polygon if (a == b) return true; // Test whether the line intersects a line segment strictly (proper intersection) for (int i = 0; i < vertexCount; i++) { int j = (i + 1) % vertexCount; const Vertex &vs = vertices[i]; const Vertex &ve = vertices[j]; // If the line intersects a line segment strictly (proper intersection), then // the line is partially inside the polygon if (Line::doesIntersectProperly(a, b, vs, ve)) return false; // If one of the two line items is on the edge and the other is to the right of the edge, // the line is not completely outside the polygon if (Line::isOnLineStrict(vs, ve, a) && Line::isVertexLeft(vs, ve, b)) return false; if (Line::isOnLineStrict(vs, ve, b) && Line::isVertexLeft(vs, ve, a)) return false; // If one of the lwo line items is on a vertex, the line must not run into the polygon if ((a == vs) && isLineInCone(i, b, false)) return false; if ((b == vs) && isLineInCone(i, a, false)) return false; // If the vertex with start and end point is collinear, (a vs) and (b, vs) is not in the polygon if (Line::isOnLine(a, b, vs)) { if (isLineInCone(i, a, false)) return false; if (isLineInCone(i, b, false)) return false; } } return true; }
/// Tries to find point in polygon. static bool findPointInPolygon(const std::vector<Vector2> &contour, Vector2 &point) { Rectangle bounds; bounds.expand(contour); auto length = contour.size(); int limit = 8; Vector2 a, b; // Current edge. if (contour.size()==3) { point = Vector2((contour[0].x + contour[1].x + contour[2].x)/3, (contour[0].y + contour[1].y + contour[2].y)/3); return true; } for (std::size_t i = 0; i < length; i++) { a = contour[i]; b = contour[(i + 1)%length]; double cx = (a.x + b.x)/2; double cy = (a.y + b.y)/2; double dx = (b.y - a.y)/1.374; double dy = (a.x - b.x)/1.374; for (int j = 1; j <= limit; j++) { // Search to the right of the segment. point.x = cx + dx/j; point.y = cy + dy/j; if (bounds.contains(point) && isPointInPolygon(point, contour)) return true; // Search on the other side of the segment. point.x = cx - dx/j; point.y = cy - dy/j; if (bounds.contains(point) && isPointInPolygon(point, contour)) return true; } } return false; }
int Image::indexForPoint(const Common::Point &point) const { int index = -1; for (uint32 i = 0; i < _polygons.size(); i++) { if (isPointInPolygon(_polygons[i], point)) { index = i; } } return index; }
bool isCircleInBox(const vector<cd> &points, cd target, double r) { if (!isPointInPolygon(points, target)) { // DEBUG; return false; } /* if (!isPointInTriangle2D(points[0], points[1], points[2], target)){ return false; } if (!isPointInTriangle2D(points[2], points[3], points[0], target)){ return false; } */ for (int i = 0; i < 4; i++) { int index0 = i; int index1 = (i+1) % 4; if (crossSegmentAndCircle(points[index0], points[index1], target, r)) { return false; } } return true; }
int vg_draw_polygon(vector2D_t polygon[], unsigned n, unsigned long color) { size_t i, j; vector2D_t min = polygon[0]; vector2D_t max = polygon[0]; for (i = 1; i < n; ++i) { if (polygon[i].x < min.x) { min.x = polygon[i].x; } else if (polygon[i].x > max.x) { max.x = polygon[i].x; } if (polygon[i].y < min.y) { min.y = polygon[i].y; } else if (polygon[i].y > max.y) { max.x = polygon[i].y; } } vector2D_t point; for (i = min.x; i < max.x; ++i) { for (j = min.y; j < max.y; ++j) { point = vectorCreate(i, j); if (isPointInPolygon(polygon, n, point)) { vg_set_pixel(i, j, color); } } } }
bool Polygon::isPointInPolygon(int x, int y, bool borderBelongsToPolygon) const { return isPointInPolygon(Vertex(x, y), borderBelongsToPolygon); }
static int nodeBelongsCountry(osm2olm* self, LCountry* country) { //printf("Check in Country with polygon: %x\n", country->polygon); return isPointInPolygon(self->node.lat, self->node.lon, country->polygon); }