using System; namespace RGB.NET.Core; /// /// Offers some extensions and helper-methods for the work with rectangles. /// public static class RectangleExtensions { #region Methods /// /// Sets the of the specified rectangle. /// /// The rectangle to modify. /// The new location of the rectangle. /// The modified . public static Rectangle SetLocation(this Rectangle rect, Point location) => new(location, rect.Size); /// /// Sets the of the of the specified rectangle. /// /// The rectangle to modify. /// The new x-location of the rectangle. /// The modified . public static Rectangle SetX(this Rectangle rect, float x) => new(new Point(x, rect.Location.Y), rect.Size); /// /// Sets the of the of the specified rectangle. /// /// The rectangle to modify. /// The new y-location of the rectangle. /// The modified . public static Rectangle SetY(this Rectangle rect, float y) => new(new Point(rect.Location.X, y), rect.Size); /// /// Sets the of the specified rectangle. /// /// The rectangle to modify. /// The new size of the rectangle. /// The modified . public static Rectangle SetSize(this Rectangle rect, Size size) => new(rect.Location, size); /// /// Sets the of the of the specified rectangle. /// /// The rectangle to modify. /// The new width of the rectangle. /// The modified . public static Rectangle SetWidth(this Rectangle rect, float width) => new(rect.Location, new Size(width, rect.Size.Height)); /// /// Sets the of the of the specified rectangle. /// /// The rectangle to modify. /// The new height of the rectangle. /// The modified . public static Rectangle SetHeight(this Rectangle rect, float height) => new(rect.Location, new Size(rect.Size.Width, height)); /// /// Calculates the percentage of intersection of a rectangle. /// /// The rectangle to calculate the intersection for. /// The intersecting rectangle. /// The percentage of intersection. public static float 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 rectangle to calculate the intersection for. /// The intersecting . /// A new representing the intersection this and the one provided as parameter. public static Rectangle CalculateIntersection(this Rectangle rect, Rectangle intersectingRectangle) { float x1 = Math.Max(rect.Location.X, intersectingRectangle.Location.X); float x2 = Math.Min(rect.Location.X + rect.Size.Width, intersectingRectangle.Location.X + intersectingRectangle.Size.Width); float y1 = Math.Max(rect.Location.Y, intersectingRectangle.Location.Y); float 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 containing rectangle. /// The to test. /// true if the rectangle contains the specified point; otherwise false. 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 containing rectangle. /// The X-location to test. /// The Y-location to test. /// true if the rectangle contains the specified coordinates; otherwise false. public static bool Contains(this Rectangle rect, float x, float 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 containing rectangle. /// The to test. /// true if the rectangle contains the specified rect; otherwise false. 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)); /// /// Moves the specified by the specified amount. /// /// The to move. /// The amount to move. /// The moved rectangle. public static Rectangle Translate(this Rectangle rect, Point point) => rect.Translate(point.X, point.Y); /// /// Moves the specified by the specified amount. /// /// The to move. /// The x-ammount to move. /// The y-ammount to move. /// The moved rectangle. public static Rectangle Translate(this Rectangle rect, float x = 0, float y = 0) => new(rect.Location.Translate(x, y), rect.Size); /// /// Rotates the specified by the specified amuont around the specified origin. /// /// /// The returned array of is filled with the new locations of the rectangle clockwise starting from the top left: /// [0] = top left /// [1] = top right /// [2] = bottom right /// [3] = bottom left /// /// The to rotate. /// The rotation. /// The origin to rotate around. [0,0] if not set. /// A array of containing the new locations of the corners of the original rectangle. public static Point[] Rotate(this Rectangle rect, Rotation rotation, Point origin = new()) { Point[] points = [ rect.Location, // top left new(rect.Location.X + rect.Size.Width, rect.Location.Y), // top right new(rect.Location.X + rect.Size.Width, rect.Location.Y + rect.Size.Height), // bottom right new(rect.Location.X, rect.Location.Y + rect.Size.Height), // bottom right ]; float sin = MathF.Sin(rotation.Radians); float cos = MathF.Cos(rotation.Radians); for (int i = 0; i < points.Length; i++) { Point point = points[i]; point = new Point(point.X - origin.X, point.Y - origin.Y); point = new Point((point.X * cos) - (point.Y * sin), (point.X * sin) + (point.Y * cos)); points[i] = new Point(point.X + origin.X, point.Y + origin.Y); } return points; } #endregion }