public static bool PolygonPointTest(Vertices vertices, Point point) { // Crossing Test // Source: Real Time Rendering 3rd Edition, p. 754 bool inside = false; Point t = point; Point e0 = vertices[vertices.Count - 1]; bool y0 = e0.Y >= t.Y; for (int i = 0; i < vertices.Count - 1; i++) { Point e1 = vertices[i]; bool y1 = e1.Y >= t.Y; if (y0 != y1) { if (((e1.Y - t.Y) * (e0.X - e1.X) >= (e1.X - t.X) * (e0.Y - e1.Y)) == y1) { inside = !inside; } } y0 = y1; e0 = e1; } return(inside); }
public bool Contains(Point point) { Matrix3x2 inverse = Matrix3x2.Invert(Transform); Point p = Matrix3x2.TransformPoint(inverse, point); return(PolygonPointTest(Vertices, p)); }
public static IEnumerable <Point> CalculateVertices(Point center, Real radiusX, Real radiusY, int slices) { Point[] vertices = new Point[slices]; const float radTo = MathHelper.TwoPi; float delta = radTo / slices; for (int i = 0; i < slices; i++) { Real theta = i * delta; vertices[i] = CreateEllipseVertex(center, radiusX, radiusY, theta); } return(vertices); }
public static Point ComputeCentroid(Polygon polygon) { Point centroid = new Point(0, 0); Real signedArea = 0.0f; Real x0; // Current vertex X Real y0; // Current vertex Y Real x1; // Next vertex X Real y1; // Next vertex Y Real a; // Partial signed area Point[] vertices = polygon.Vertices.ToArray(); // For all vertices except last int i; for (i = 0; i < polygon.Vertices.Count - 1; ++i) { x0 = vertices[i].X; y0 = vertices[i].Y; x1 = vertices[i + 1].X; y1 = vertices[i + 1].Y; a = x0 * y1 - x1 * y0; signedArea += a; centroid.X += (x0 + x1) * a; centroid.Y += (y0 + y1) * a; } // Do last vertex x0 = vertices[i].X; y0 = vertices[i].Y; x1 = vertices[0].X; y1 = vertices[0].Y; a = x0 * y1 - x1 * y0; signedArea += a; centroid.X += (x0 + x1) * a; centroid.Y += (y0 + y1) * a; signedArea /= 2; centroid.X /= (6 * signedArea); centroid.Y /= (6 * signedArea); return(centroid); }
public static Polygon New(Point center, Real circumCircleRadius, int sides) { float sideLength = ComputeSideLength(circumCircleRadius, sides); var polygonPoints = new Vector2[sides]; for (int i = 0; i < sides; i++) { float theta = 2 * MathUtil.Pi / sides; float x = center.X + (float)Math.Cos(i * theta) * sideLength; float y = center.Y + (float)Math.Sin(i * theta) * sideLength; polygonPoints[i] = new Vector2(x, y); } return(new Polygon(polygonPoints) { SideLength = sideLength, CircumCircleRadius = circumCircleRadius }); }
/// <summary> /// Winding number test for a point in a polygon. /// </summary> /// See more info about the algorithm here: http://softsurfer.com/Archive/algorithm_0103/algorithm_0103.htm /// <param name="polygon">The polygon.</param> /// <param name="point">The point to be tested.</param> /// <returns>-1 if the winding number is zero and the point is outside /// the polygon, 1 if the point is inside the polygon, and 0 if the point /// is on the polygons edge.</returns> public static int PointInPolygon(Polygon polygon, Point point) { // Winding number int wn = 0; Vertices polyVertices = polygon.Vertices; // Iterate through polygon's edges for (int i = 0; i < polyVertices.Count; i++) { // Get points Point p1 = polyVertices[i]; Point p2 = polyVertices[polyVertices.NextIndex(i)]; // Test if a point is directly on the edge Point edge = p2 - p1; double area = GeometryHelper.Area(ref p1, ref p2, ref point); if (Math.Abs(area - 0f) < MathHelper.EpsilonD && Point.Dot(point - p1, edge) >= 0 && Point.Dot(point - p2, edge) <= 0) { return(0); } // Test edge for intersection with ray from point if (p1.Y <= point.Y) { if (p2.Y > point.Y && area > 0) { ++wn; } } else { if (p2.Y <= point.Y && area < 0) { --wn; } } } return(wn); }
internal static Point CreateEllipseVertex(Point center, Real radiusX, Real radiusY, Real theta, Real offset = 1) { return(new Point(center.X + (Real)Math.Cos(theta) * radiusX * offset, center.Y + (Real)Math.Sin(theta) * radiusY * offset)); }
public Ellipse(Point center, Real radiusX, Real radiusY) { Center = center; RadiusX = radiusX; RadiusY = radiusY; }
public static Point4 ToVector4(this Point vector2, float z = 0f, float w = 0f) { return(new Vector4(vector2, z, w)); }
public static Point3 ToVector3(this Point vector2, float z = 0f) { return(new Vector3(vector2, z)); }
/// <summary> /// Calculates a vector perpendicular to the source vector. /// </summary> /// <param name="value">The source vector.</param> /// <returns>The resulting perpendicular vector.</returns> public static Point Perp(this Point value) { return(new Point(-value.Y, value.X)); }
internal static void Offset(this Point point, Real xOffset, Single yOffset) { point.X += xOffset; point.Y += yOffset; }
public Rectangle(Point topLeft, Real width, Real height) { TopLeft = topLeft; Width = width; Height = height; }
/// <summary> /// ranslates the vertices using the specified vector. /// </summary> /// <param name="vector">Translation vector.</param> public void Translate(Point vector) { Transform = Matrix3x2.Translation(vector); }