/// <summary> /// Determines whether two sets of vertices intersect along a set of axes. /// </summary> /// <param name="axes">The axes to check for intersections along.</param> /// <param name="firstVertices">The first set of vertices.</param> /// <param name="secondVertices">The second set of vertices.</param> /// <returns>Whether there is an intersection between <paramref name="firstVertices"/> and <see cref="secondVertices"/> along any of <paramref name="axes"/>.</returns> private static bool intersects(ReadOnlySpan <Vector2> axes, ReadOnlySpan <Vector2> firstVertices, ReadOnlySpan <Vector2> secondVertices) { foreach (Vector2 axis in axes) { ProjectionRange firstRange = new ProjectionRange(axis, firstVertices); ProjectionRange secondRange = new ProjectionRange(axis, secondVertices); if (!firstRange.Overlaps(secondRange)) { return(false); } } return(true); }
private static bool intersects(Vector2[][] bothAxes, Vector2[][] bothVertices) { foreach (Vector2[] axes in bothAxes) { foreach (Vector2 axis in axes) { ProjectionRange firstRange = new ProjectionRange(axis, bothVertices[0]); ProjectionRange secondRange = new ProjectionRange(axis, bothVertices[1]); if (!firstRange.Overlaps(secondRange)) { return(false); } } } return(true); }
/// <summary> /// Determines whether two convex polygons intersect. /// </summary> /// <param name="first">The first polygon.</param> /// <param name="second">The second polygon.</param> /// <returns>Whether the two polygons intersect.</returns> public static bool Intersects(this IConvexPolygon first, Rectangle second) { Vector2[][] bothAxes = { first.GetAxes(), second.GetAxes() }; Vector2[] firstVertices = first.Vertices; Vector2[] secondVertices = second.GetVertices(); foreach (Vector2[] axes in bothAxes) { foreach (Vector2 axis in axes) { ProjectionRange firstRange = new ProjectionRange(axis, firstVertices); ProjectionRange secondRange = new ProjectionRange(axis, secondVertices); if (!firstRange.Overlaps(secondRange)) { return(false); } } } return(true); }