using System; namespace RGB.NET.Core { public static class RectangleExtensions { #region Methods /// /// Sets the of the given rectangle. /// /// The rectangle to modify. /// The new location of the rectangle. /// The modified . public static Rectangle SetLocation(this Rectangle rect, Point location) => new Rectangle(location, rect.Size); /// /// Sets the of the of the given rectangle. /// /// The rectangle to modify. /// The new x-location of the rectangle. /// The modified . public static Rectangle SetX(this Rectangle rect, double x) => new Rectangle(new Point(x, rect.Location.Y), rect.Size); /// /// Sets the of the of the given rectangle. /// /// The rectangle to modify. /// The new y-location of the rectangle. /// The modified . public static Rectangle SetY(this Rectangle rect, double y) => new Rectangle(new Point(rect.Location.X, y), rect.Size); /// /// Sets the of the given rectangle. /// /// The rectangle to modify. /// The new size of the rectangle. /// The modified . public static Rectangle SetSize(this Rectangle rect, Size size) => new Rectangle(rect.Location, size); /// /// Sets the of the of the given rectangle. /// /// The rectangle to modify. /// The new width of the rectangle. /// The modified . public static Rectangle SetWidth(this Rectangle rect, double width) => new Rectangle(rect.Location, new Size(width, rect.Size.Height)); /// /// Sets the of the of the given rectangle. /// /// The rectangle to modify. /// The new height of the rectangle. /// The modified . public static Rectangle SetHeight(this Rectangle rect, double height) => new Rectangle(rect.Location, new Size(rect.Size.Width, height)); /// /// Calculates the percentage of intersection of a rectangle. /// /// The intersecting rectangle. /// The percentage of intersection. public static double CalculateIntersectPercentage(this Rectangle rect, Rectangle intersectingRect) { if (rect.IsEmpty || intersectingRect.IsEmpty) return 0; Rectangle intersection = rect.CalculateIntersection(intersectingRect); return (intersection.Size.Width * intersection.Size.Height) / (rect.Size.Width * rect.Size.Height); } /// /// Calculates the representing the intersection of this and the one provided as parameter. /// /// The intersecting /// A new representing the intersection this and the one provided as parameter. public static Rectangle CalculateIntersection(this Rectangle rect, Rectangle intersectingRectangle) { double x1 = Math.Max(rect.Location.X, intersectingRectangle.Location.X); double x2 = Math.Min(rect.Location.X + rect.Size.Width, intersectingRectangle.Location.X + intersectingRectangle.Size.Width); double y1 = Math.Max(rect.Location.Y, intersectingRectangle.Location.Y); double y2 = Math.Min(rect.Location.Y + rect.Size.Height, intersectingRectangle.Location.Y + intersectingRectangle.Size.Height); if ((x2 >= x1) && (y2 >= y1)) return new Rectangle(x1, y1, x2 - x1, y2 - y1); return new Rectangle(); } /// /// Determines if the specified is contained within this . /// /// The to test. /// public static bool Contains(this Rectangle rect, Point point) => rect.Contains(point.X, point.Y); /// /// Determines if the specified location is contained within this . /// /// The X-location to test. /// The Y-location to test. /// public static bool Contains(this Rectangle rect, double x, double y) => (rect.Location.X <= x) && (x < (rect.Location.X + rect.Size.Width)) && (rect.Location.Y <= y) && (y < (rect.Location.Y + rect.Size.Height)); /// /// Determines if the specified is contained within this . /// /// The to test. /// public static bool Contains(this Rectangle rect, Rectangle rect2) => (rect.Location.X <= rect2.Location.X) && ((rect2.Location.X + rect2.Size.Width) <= (rect.Location.X + rect.Size.Width)) && (rect.Location.Y <= rect2.Location.Y) && ((rect2.Location.Y + rect2.Size.Height) <= (rect.Location.Y + rect.Size.Height)); #endregion } }