Beispiel #1
0
        /// <summary>
        /// Whether the rectangle contains the rectangle.
        /// </summary>
        /// <param name="rectangle">The rectangle that should contain the other rectangle.</param>
        /// <param name="other">The other rectangle that should be contained within the rectangle.</param>
        /// <param name="includeBorder">If set to false, will return false if the rectangles share side(s).</param>
        public static bool Contains(this PdfRectangle rectangle, PdfRectangle other, bool includeBorder = false)
        {
            if (!rectangle.Contains(other.BottomLeft, includeBorder))
            {
                return(false);
            }
            if (!rectangle.Contains(other.TopRight, includeBorder))
            {
                return(false);
            }
            if (!rectangle.Contains(other.BottomRight, includeBorder))
            {
                return(false);
            }
            if (!rectangle.Contains(other.TopLeft, includeBorder))
            {
                return(false);
            }

            return(true);
        }
Beispiel #2
0
        /// <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
            {
                if (!rectangle.Normalise().IntersectsWith(other.Normalise()))
                {
                    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);
            }
        }
        /// <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);
            }
        }