/// <summary> /// Returns the trimmed line at which the borders of the two rects collides /// </summary> /// <param name="secondRect"></param> /// <returns></returns> public Line2?BorderIntersection(AxisAlignedRectangle secondRect) { if (IntersectsWith(secondRect)) { throw new ArgumentException("Rects are overlapping"); } return (Borders.SelectMany(a => secondRect.Borders.Select(b => new { A = a, B = b })) .Select(pair => pair.A.GetIntersectionLine(pair.B)) .Where(line => line.HasValue && !line.Value.IsPoint) .FirstOrDefault(line => line.HasValue)); }
/// <summary> /// Returns the intersection area of both rectangles /// </summary> public AxisAlignedRectangle?Intersection(AxisAlignedRectangle secondRect) { // TODO: implement this natively to prevent the loss of precision var intersection = ToRectangleF(); intersection.Intersect(secondRect.ToRectangleF()); if (intersection.IsEmpty) { return(null); } return(new AxisAlignedRectangle() { TopLeft = new Vector2(intersection.Left, intersection.Top), BottomRight = new Vector2(intersection.Right, intersection.Bottom) }); }
/// <summary> /// True if both rectangles are intersecting /// </summary> /// <param name="secondRect"></param> /// <returns></returns> public bool IntersectsWith(AxisAlignedRectangle secondRect) { return(Intersection(secondRect) != null); }