/// <summary> /// Checks to see if two polygons intersect /// </summary> /// <param name="a">The first polygon.</param> /// <param name="b">The second polygon.</param> /// <returns>True if the two polygons intersect. False otherwise.</returns> public static bool Intersects(IPolygon2D a, IPolygon2D b) { // See if this polygon's points are contained in the other polygon for (int i = 0; i < a.NumSides; i++) { if (b.Contains(a.Vertices[i])) { return(true); } } // See if this polygon contains the other polygon's points for (int i = 0; i < b.NumSides; i++) { if (a.Contains(b.Vertices[i])) { return(true); } } // See if any of the polygon edges intersect each other for (int i = 0; i < a.NumSides; i++) { Vector2 edgeStart = a.Vertices[i]; Vector2 edgeEnd = (i == a.NumSides - 1 ? a.Vertices[0] : a.Vertices[i + 1]); for (int j = 0; j < b.NumSides; j++) { Vector2 otherEdgeStart = a.Vertices[j]; Vector2 otherEdgeEnd = (j == a.NumSides - 1 ? a.Vertices[0] : a.Vertices[j + 1]); if (ShapeUtility.AreEdgesIntersecting(edgeStart, edgeEnd, otherEdgeStart, otherEdgeEnd)) { return(true); } } } return(false); }
/// <summary> /// Checks to see whether the specified point lies in the specified complex polygon. Complex polygon here /// means that the polygon may contain convex vertices. /// </summary> /// <param name="point">The point to check containment for.</param> /// <param name="vertices">The vertices describing the polygon in clockwise order.</param> /// <param name="numVertices">The number of vertices in the vertex collection.</param> /// <returns>True if the point lies within the polygon, false otherwise.</returns> public static bool ContainsForComplexPolygon(Vector2 point, Vector2[] vertices, int numVertices) { //Vector3 checkPoint = new Vector3( point.X, point.Y, 0.0f ); int leftSide = 0, rightSide = 0; for (int i = 0; i < numVertices; i++) { int nextIndex = (i + 1) % numVertices; Vector2?intersection = ShapeUtility.FindYIntersectionPoint(vertices[i], vertices[nextIndex], point.Y); if (intersection != null) { if (intersection.Value.X < point.X) { leftSide++; } else { rightSide++; } } } // The point is inside if both the number of intersecting points on the left and right sides of the point are odd return(((leftSide & 1) == 1) && ((rightSide & 1) == 1)); }
private void ResetInternal() { _center = ShapeUtility.CalculateCenterPoint(this); _innerRadius = ShapeUtility.CalculateInnerRadius(this); _outerRadius = ShapeUtility.CalculateOuterRadius(this); _onReset.Notify(); }
public override void RecalculateBounds() { Matrix transform = TranslateFrom; bool firstIteration = true; OuterRadiusVector = VectorUtility.Zero; float outerRadius = 0; Vector2 tempOuterRadius; ActualCenter = Vector2.Transform(ReferenceRegion.Center, transform); for (int i = 0; i < NumSides; i++) { _actualVertices[i] = Vector2.Transform(ReferenceRegion.Vertices[i], transform); if (firstIteration) { LowestX = _actualVertices[i].X; LowestY = _actualVertices[i].Y; HighestX = _actualVertices[i].X; HighestY = _actualVertices[i].Y; firstIteration = false; } else { LowestX = MathHelper.Min(LowestX, _actualVertices[i].X); LowestY = MathHelper.Min(LowestY, _actualVertices[i].Y); HighestX = MathHelper.Max(HighestX, _actualVertices[i].X); HighestY = MathHelper.Max(HighestY, _actualVertices[i].Y); } tempOuterRadius = (_actualVertices[i] - ActualCenter); if (tempOuterRadius.Length() > outerRadius) { outerRadius = tempOuterRadius.Length(); OuterRadiusVector = tempOuterRadius; } } InnerRadiusVector = ShapeUtility.FindClosestPointOnPolygon(ActualCenter, _actualVertices, NumSides) - ActualCenter; }
/// <summary> /// Finds the closets point on the polygon to the specified point. /// </summary> /// <param name="point">The point to find the closest point to.</param> /// <param name="vertices">The vertices describing the polygon.</param> /// <param name="numVertices">The number of actual vertices in the vertices array.</param> /// <returns>The point on the polygon that is closest to the specified point.</returns> public static Vector2 FindClosestPointOnPolygon(Vector2 point, Vector2[] vertices, int numVertices) { // Find the closest point Vector2 closestPoint = ShapeUtility.CalculateClosestPointOnEdge(point, vertices[0], vertices[1]); float closestDistance = (closestPoint - point).LengthSquared(); for (int i = 1; i < numVertices; i++) { Vector2 closestPointOfEdge = (i == numVertices - 1 ? ShapeUtility.CalculateClosestPointOnEdge(point, vertices[i], vertices[0]) : ShapeUtility.CalculateClosestPointOnEdge(point, vertices[i], vertices[i + 1])); float currentDistance = (closestPointOfEdge - point).LengthSquared(); if (currentDistance < closestDistance) { closestPoint = closestPointOfEdge; closestDistance = currentDistance; } } return(closestPoint); }
/// <summary> /// Checks for intersection between a polygon and a circle. /// </summary> /// <param name="polygon">The circle.</param> /// <param name="circle">The polygon.</param> /// <returns>True if the two shapes intersect. False othwerise.</returns> public static bool Intersects(IPolygon2D polygon, ICircle2D circle) { // See if the closest point of each edge to the circle's center is inside the center Vector2 circleCenter = circle.Center; for (int i = 0; i < polygon.NumSides; i++) { Vector2 closestPointOfEdge = (i == polygon.NumSides - 1 ? ShapeUtility.CalculateClosestPointOnEdge(circleCenter, polygon.Vertices[i], polygon.Vertices[0]) : ShapeUtility.CalculateClosestPointOnEdge(circleCenter, polygon.Vertices[i], polygon.Vertices[i + 1])); if (circle.Contains(closestPointOfEdge)) { return(true); } } // See if this polygon contains the other circle if (polygon.Contains(circle.Center)) { return(true); } return(false); }
public Vector2 FindClosestPoint(Vector2 point) { return(ShapeUtility.FindClosestPointOnCircle(point, this)); }
public bool Intersects(IPolygon2D other) { return(ShapeUtility.Intersects(other, this)); }
public bool Contains(Vector2 point) { return(ShapeUtility.ContainsForCircle(point, this)); }
public bool Intersects(ICircle2D other) { return(ShapeUtility.Intersects(this, other)); }
public override bool Intersects(ICircle2D other) { return(ShapeUtility.Intersects(other, this)); }
public override bool Intersects(ICircularExtent other) { return(ShapeUtility.Intersects(other, this)); }
public override bool Intersects(IPolygonExtent other) { return(ShapeUtility.Intersects(other, this)); }
public override bool Contains(Vector2 point) { return(ShapeUtility.ContainsForSimplePolygon(point, this)); }
public override bool Intersects(IPolygon2D other) { return(ShapeUtility.Intersects(this, other)); }
public override Vector2 FindClosestPoint(Vector2 point) { return(ShapeUtility.FindClosestPointOnPolygon(point, this)); }