예제 #1
0
 /// <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);
 }
예제 #2
0
        /// <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);
        }