/// <summary> /// Gets intersection percent of two rectangles. /// </summary> /// <param name="rect1">First rectangle.</param> /// <param name="rect2">Second rectangle.</param> /// <returns>Intersection percent (1 - full intersection, 0 - no intersection).</returns> public static float IntersectionPercent(this RectangleF rect1, RectangleF rect2) { float rect1Area = rect1.Width * rect1.Height; float rect2Area = rect2.Width * rect2.Height; RectangleF interesectRect = RectangleF.Intersect(rect1, rect2); float intersectRectArea = interesectRect.Width * interesectRect.Height; float minRectArea = System.Math.Min(rect1Area, rect2Area); return((float)intersectRectArea / minRectArea); }
/// <summary> /// Inflates the rectangle by specified width and height (can be negative) and automatically clamps rectangle coordinates. /// </summary> /// <param name="rect">Rectangle to inflate.</param> /// <param name="widthScale">Horizontal scale.</param> /// <param name="heightScale">Vertical scale.</param> /// <param name="constrainedArea">If specified rectangle region will be clamped.</param> /// <returns>Inflated rectangle.</returns> public static RectangleF Inflate(this RectangleF rect, double widthScale, double heightScale, SizeF constrainedArea = default(SizeF)) { RectangleF newRect = new RectangleF { X = (float)(rect.X - rect.Width * widthScale / 2), Y = (float)(rect.Y - rect.Height * heightScale / 2), Width = (float)(rect.Width + rect.Width * widthScale), Height = (float)(rect.Height + rect.Height * heightScale) }; if (constrainedArea.IsEmpty == false) { newRect.Intersect(new RectangleF(new PointF(), constrainedArea)); } return(newRect); }
/// <summary> /// Inflates the rectangle by specified width and height (can be negative) and automatically clamps rectangle coordinates. /// </summary> /// <param name="rect">Rectangle to inflate.</param> /// <param name="widthScale">Horizontal scale.</param> /// <param name="heightScale">Vertical scale.</param> /// <param name="constrainedArea">If specified rectangle region will be clamped.</param> /// <returns>Inflated rectangle.</returns> public static RectangleF Inflate(this RectangleF rect, double widthScale, double heightScale, SizeF constrainedArea = default(SizeF)) { RectangleF newRect = new RectangleF { X = (float)(rect.X - rect.Width * widthScale / 2), Y = (float)(rect.Y - rect.Height * heightScale / 2), Width = (float)(rect.Width + rect.Width * widthScale), Height = (float)(rect.Height + rect.Height * heightScale) }; if (constrainedArea.IsEmpty == false) newRect.Intersect(new RectangleF(new PointF(), constrainedArea)); return newRect; }
/// <summary> /// Replaces this rectangle with the intersection of itself and the specified rectangle. /// </summary> /// <param name="rect">The rectangle with which to intersect.</param> public void Intersect(RectangleF rect) { this = RectangleF.Intersect(this, rect); }