System.Drawing.RectangleF.Intersect C# (CSharp) Method

Intersect() private method

private Intersect ( RectangleF a, RectangleF b ) : RectangleF
a RectangleF
b RectangleF
return RectangleF
        public static RectangleF Intersect(RectangleF a, RectangleF b)
        {
            float x1 = Math.Max(a.X, b.X);
            float x2 = Math.Min(a.X + a.Width, b.X + b.Width);
            float y1 = Math.Max(a.Y, b.Y);
            float y2 = Math.Min(a.Y + a.Height, b.Y + b.Height);

            if (x2 >= x1 && y2 >= y1)
            {
                return new RectangleF(x1, y1, x2 - x1, y2 - y1);
            }

            return Empty;
        }

Same methods

RectangleF::Intersect ( System a, System b ) : System.Drawing.RectangleF
RectangleF::Intersect ( RectangleF rect ) : void
RectangleF::Intersect ( System rect ) : void

Usage Example

        public static float CalculateIntersectPercentage(RectangleF rect, RectangleF referenceRect)
        {
            if (rect.IsEmpty || referenceRect.IsEmpty) return 0;

            referenceRect.Intersect(rect); // replace referenceRect with intersect
            return referenceRect.IsEmpty ? 0 : (referenceRect.Width * referenceRect.Height) / (rect.Width * rect.Height);
        }
All Usage Examples Of System.Drawing.RectangleF::Intersect