Пример #1
0
// Return whether a point is inside, on, or outside the cell:
//   -1: point is outside the perimeter of the cell
//    0: point is on the perimeter of the cell
//    1: point is inside the perimeter of the cell
//
int Cell::pointIntersection(double x, double y) {
	// Check if point in polygon. Since all polygons of a Voronoi
	// diagram are convex, then:
	// http://paulbourke.net/geometry/polygonmesh/
	// Solution 3 (2D):
	//   "If the polygon is convex then one can consider the polygon
	//   "as a 'path' from the first vertex. A point is on the interior
	//   "of this polygons if it is always on the same side of all the
	//   "line segments making up the path. ...
	//   "(y - y0) (x1 - x0) - (x - x0) (y1 - y0)
	//   "if it is less than 0 then P is to the right of the line segment,
	//   "if greater than 0 it is to the left, if equal to 0 then it lies
	//   "on the line segment"
	HalfEdge* he;
	size_t edgeCount = halfEdges.size();
	Point2 p0;
	Point2 p1;
	double r;

	while (edgeCount--) {
		he = halfEdges[edgeCount];
		p0 = *he->startPoint();
		p1 = *he->endPoint();
		r = (y - p0.y)*(p1.x - p0.x) - (x - p0.x)*(p1.y - p0.y);

		if (r == 0) {
			return 0;
		}
		if (r > 0) {
			return -1;
		}
	}
	return 1;
}