using System; using Microsoft.Xna.Framework; namespace MonoGame.Extended; /// /// Provides extension methods for the structure. /// public static class RectangleExtensions { /// /// Gets the corners of the rectangle in a clockwise direction starting at the top left. /// /// The rectangle to get the corners of. /// An array of elements representing the corners of the rectangle. public static Point[] GetCorners(this Rectangle rectangle) { var corners = new Point[4]; corners[0] = new Point(rectangle.Left, rectangle.Top); corners[1] = new Point(rectangle.Right, rectangle.Top); corners[2] = new Point(rectangle.Right, rectangle.Bottom); corners[3] = new Point(rectangle.Left, rectangle.Bottom); return corners; } /// /// Converts the specified to a . /// /// The rectangle to convert. /// The converted . public static RectangleF ToRectangleF(this Rectangle rectangle) { return new RectangleF(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); } /// /// Clips the specified rectangle against the specified clipping rectangle. /// /// The rectangle to clip. /// The rectangle to clip against. /// The clipped rectangle, or if the rectangles do not intersect. public static Rectangle Clip(this Rectangle rectangle, Rectangle clippingRectangle) { int left = Math.Max(rectangle.Left, clippingRectangle.Left); int top = Math.Max(rectangle.Top, clippingRectangle.Top); int right = Math.Min(rectangle.Right, clippingRectangle.Right); int bottom = Math.Min(rectangle.Bottom, clippingRectangle.Bottom); int width = right - left; int height = bottom - top; if (width <= 0 || height <= 0) return Rectangle.Empty; return new Rectangle(left, top, width, height); } /// /// Gets a rectangle that is relative to the specified source rectangle, with the specified offsets and dimensions. /// /// The source rectangle. /// The x-coordinate of the relative rectangle, relative to the source rectangle. /// The y-coordinate of the relative rectangle, relative to the source rectangle. /// The width, in pixels, of the relative rectangle. /// The height, in pixels, of the relative rectangle. /// The relative rectangle, clipped to the source rectangle. public static Rectangle GetRelativeRectangle(this Rectangle source, int x, int y, int width, int height) { int absoluteX = source.X + x; int absoluteY = source.Y + y; Rectangle relative; relative.X = (int)MathHelper.Clamp(absoluteX, source.Left, source.Right); relative.Y = (int)MathHelper.Clamp(absoluteY, source.Top, source.Bottom); relative.Width = Math.Max(Math.Min(absoluteX + width, source.Right) - relative.X, 0); relative.Height = Math.Max(Math.Min(absoluteY + height, source.Bottom) - relative.Y, 0); return relative; } #if FNA // MomoGame compatibility layer /// /// Deconstruction method for Rectangle. /// public static void Deconstruct(this Rectangle rectangle, out int x, out int y, out int width, out int height) { x = rectangle.X; y = rectangle.Y; width = rectangle.Width; height = rectangle.Height; } #endif /// /// Normalizes the specified so that the and /// are positive without changing the location of the rectangle. /// /// The to normalize. /// A with positive width and height. public static Rectangle Normalize(Rectangle rectangle) { if (rectangle.Width < 0) { rectangle.X += rectangle.Width; rectangle.Width = -rectangle.Width; } if (rectangle.Height < 0) { rectangle.Y += rectangle.Height; rectangle.Height = -rectangle.Height; } return rectangle; } /// /// Normalizes a so that the and /// are positive without changing the location of the rectangle. /// /// The source . /// /// When this method returns, contains the a normalized with positive width and height. /// public static void Normalize(ref this Rectangle rectangle, out Rectangle result) { result.X = rectangle.X; result.Width = rectangle.Width; if (result.Width < 0) { result.X += result.Width; result.Width = -result.Width; } result.Y = rectangle.Y; result.Height = rectangle.Height; if (result.Height < 0) { result.Y += result.Height; result.Height = -result.Height; } } }