/// <summary> /// Gets the axis-aligned rectangle that completely containing the original rectangle, with no rotation. /// </summary> /// <param name="rectangle"></param> public static PdfRectangle Normalise(this PdfRectangle rectangle) { var points = new[] { rectangle.BottomLeft, rectangle.BottomRight, rectangle.TopLeft, rectangle.TopRight }; return(new PdfRectangle(points.Min(p => p.X), points.Min(p => p.Y), points.Max(p => p.X), points.Max(p => p.Y))); }
/// <summary> /// Whether two rectangles overlap. /// <para>Returns false if the two rectangles only share a border.</para> /// </summary> public static bool IntersectsWith(this PdfRectangle rectangle, PdfRectangle other) { if (Math.Abs(rectangle.Rotation) < epsilon && Math.Abs(other.Rotation) < epsilon) { if (rectangle.Left > other.Right || other.Left > rectangle.Right) { return(false); } if (rectangle.Top < other.Bottom || other.Top < rectangle.Bottom) { return(false); } return(true); } else { var r1 = rectangle.Normalise(); var r2 = other.Normalise(); if (Math.Abs(r1.Rotation) < epsilon && Math.Abs(r2.Rotation) < epsilon) { // check rotation to avoid stackoverflow if (!r1.IntersectsWith(r2)) { return(false); } } if (rectangle.Contains(other.BottomLeft)) { return(true); } if (rectangle.Contains(other.TopRight)) { return(true); } if (rectangle.Contains(other.TopLeft)) { return(true); } if (rectangle.Contains(other.BottomRight)) { return(true); } if (other.Contains(rectangle.BottomLeft)) { return(true); } if (other.Contains(rectangle.TopRight)) { return(true); } if (other.Contains(rectangle.TopLeft)) { return(true); } if (other.Contains(rectangle.BottomRight)) { return(true); } if (IntersectsWith(rectangle.BottomLeft, rectangle.BottomRight, other.BottomLeft, other.BottomRight)) { return(true); } if (IntersectsWith(rectangle.BottomLeft, rectangle.BottomRight, other.BottomRight, other.TopRight)) { return(true); } if (IntersectsWith(rectangle.BottomLeft, rectangle.BottomRight, other.TopRight, other.TopLeft)) { return(true); } if (IntersectsWith(rectangle.BottomLeft, rectangle.BottomRight, other.TopLeft, other.BottomLeft)) { return(true); } if (IntersectsWith(rectangle.BottomRight, rectangle.TopRight, other.BottomLeft, other.BottomRight)) { return(true); } if (IntersectsWith(rectangle.BottomRight, rectangle.TopRight, other.BottomRight, other.TopRight)) { return(true); } if (IntersectsWith(rectangle.BottomRight, rectangle.TopRight, other.TopRight, other.TopLeft)) { return(true); } if (IntersectsWith(rectangle.BottomRight, rectangle.TopRight, other.TopLeft, other.BottomLeft)) { return(true); } if (IntersectsWith(rectangle.TopRight, rectangle.TopLeft, other.BottomLeft, other.BottomRight)) { return(true); } if (IntersectsWith(rectangle.TopRight, rectangle.TopLeft, other.BottomRight, other.TopRight)) { return(true); } if (IntersectsWith(rectangle.TopRight, rectangle.TopLeft, other.TopRight, other.TopLeft)) { return(true); } if (IntersectsWith(rectangle.TopRight, rectangle.TopLeft, other.TopLeft, other.BottomLeft)) { return(true); } if (IntersectsWith(rectangle.TopLeft, rectangle.BottomLeft, other.BottomLeft, other.BottomRight)) { return(true); } if (IntersectsWith(rectangle.TopLeft, rectangle.BottomLeft, other.BottomRight, other.TopRight)) { return(true); } if (IntersectsWith(rectangle.TopLeft, rectangle.BottomLeft, other.TopRight, other.TopLeft)) { return(true); } if (IntersectsWith(rectangle.TopLeft, rectangle.BottomLeft, other.TopLeft, other.BottomLeft)) { return(true); } return(false); } }