/// <summary> /// Test if and where the RotRect intersects with a line. /// </summary> /// <param name="line">The line to test.</param> /// <returns>The intersection point closest to the line's A point if it was hit, or null if it didn't hit.</returns> public Vector2?IntersectsAt(Line line) { Vector2[] points = GetRotatedPosArray(); Vector2[] otherPoints = line.GetPosArray(); Vector2 a = line.A; float closest = float.PositiveInfinity; float dist; Vector2?ret = null; Vector2?test = null; test = SpecialMath.LineSegmentsIntersectAt(otherPoints[0], otherPoints[1], points[0], points[1]); if (test.HasValue) { dist = Vector2.Distance(a, test.Value); if (dist < closest) { closest = dist; ret = test; } } test = SpecialMath.LineSegmentsIntersectAt(otherPoints[0], otherPoints[1], points[1], points[2]); if (test.HasValue) { dist = Vector2.Distance(a, test.Value); if (dist < closest) { closest = dist; ret = test; } } test = SpecialMath.LineSegmentsIntersectAt(otherPoints[0], otherPoints[1], points[2], points[3]); if (test.HasValue) { dist = Vector2.Distance(a, test.Value); if (dist < closest) { closest = dist; ret = test; } } test = SpecialMath.LineSegmentsIntersectAt(otherPoints[0], otherPoints[1], points[3], points[0]); if (test.HasValue) { dist = Vector2.Distance(a, test.Value); if (dist < closest) { closest = dist; ret = test; } } return(ret); }
/// <summary> /// Test if the RotRect intersects with another RotRect. /// </summary> /// <param name="other">The other RotRect to test.</param> /// <returns>True if they intersect, false if they don't.</returns> public bool Intersects(RotRect other) { Vector2[] points = GetRotatedPosArray(); Vector2[] otherPoints = other.GetRotatedPosArray(); if (PointInPolygon(points[0], otherPoints) || PointInPolygon(points[1], otherPoints) || PointInPolygon(points[2], otherPoints) || PointInPolygon(points[3], otherPoints) || PointInPolygon(otherPoints[0], points) || PointInPolygon(otherPoints[1], points) || PointInPolygon(otherPoints[2], points) || PointInPolygon(otherPoints[3], points) ) { return(true); } if (SpecialMath.LineSegmentsIntersect(points[0], points[1], otherPoints[0], otherPoints[1]) || SpecialMath.LineSegmentsIntersect(points[1], points[2], otherPoints[0], otherPoints[1]) || SpecialMath.LineSegmentsIntersect(points[2], points[3], otherPoints[0], otherPoints[1]) || SpecialMath.LineSegmentsIntersect(points[3], points[0], otherPoints[0], otherPoints[1]) || SpecialMath.LineSegmentsIntersect(points[0], points[1], otherPoints[1], otherPoints[2]) || SpecialMath.LineSegmentsIntersect(points[1], points[2], otherPoints[1], otherPoints[2]) || SpecialMath.LineSegmentsIntersect(points[2], points[3], otherPoints[1], otherPoints[2]) || SpecialMath.LineSegmentsIntersect(points[3], points[0], otherPoints[1], otherPoints[2]) || SpecialMath.LineSegmentsIntersect(points[0], points[1], otherPoints[2], otherPoints[3]) || SpecialMath.LineSegmentsIntersect(points[1], points[2], otherPoints[2], otherPoints[3]) || SpecialMath.LineSegmentsIntersect(points[2], points[3], otherPoints[2], otherPoints[3]) || SpecialMath.LineSegmentsIntersect(points[3], points[0], otherPoints[2], otherPoints[3]) || SpecialMath.LineSegmentsIntersect(points[0], points[1], otherPoints[3], otherPoints[0]) || SpecialMath.LineSegmentsIntersect(points[1], points[2], otherPoints[3], otherPoints[0]) || SpecialMath.LineSegmentsIntersect(points[2], points[3], otherPoints[3], otherPoints[0]) || SpecialMath.LineSegmentsIntersect(points[3], points[0], otherPoints[3], otherPoints[0]) ) { return(true); } return(false); }
public bool Intersects(Line line) { return(SpecialMath.LineSegmentsIntersect(A, B, line.A, line.B)); }
public Vector2 IntersectionPoint(Line line) { return(SpecialMath.IntersectionPoint(A, B, line.A, line.B)); }