using System.Diagnostics; using System.Diagnostics.CodeAnalysis; namespace RhythmBase.Components { /// /// Represents a rectangle defined by its left, top, right, and bottom edges. /// [DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")] public struct RDRect(float? left, float? top, float? right, float? bottom) : IEquatable { /// /// Gets or sets the left edge of the rectangle. /// public float? Left { get; set; } = left; /// /// Gets or sets the right edge of the rectangle. /// public float? Right { get; set; } = right; /// /// Gets or sets the top edge of the rectangle. /// public float? Top { get; set; } = top; /// /// Gets or sets the bottom edge of the rectangle. /// public float? Bottom { get; set; } = bottom; /// /// Gets the point at the left-bottom corner of the rectangle. /// public readonly RDPoint LeftBottom { get => new(Left, Bottom); } /// /// Gets the point at the right-bottom corner of the rectangle. /// public readonly RDPoint RightBottom { get => new(Right, Bottom); } /// /// Gets the point at the left-top corner of the rectangle. /// public readonly RDPoint LeftTop { get => new(Left, Top); } /// /// Gets the point at the right-top corner of the rectangle. /// public readonly RDPoint RightTop { get => new(Right, Top); } /// /// Gets the width of the rectangle. /// public readonly float? Width => Right - Left; /// /// Gets the height of the rectangle. /// public readonly float? Height => Top - Bottom; /// /// Initializes a new instance of the struct with the specified location and size. /// /// The location of the rectangle. /// The size of the rectangle. public RDRect(RDPoint? location, RDSize? size) : this(location?.X, location?.Y + size?.Height, location?.X + size?.Width, location?.Y) { } /// /// Initializes a new instance of the struct with the specified size. /// /// The size of the rectangle. public RDRect(RDSize size) : this(new float?(0f), size.Height, size.Width, new float?(0f)) { } /// /// Initializes a new instance of the struct with the specified width and height. /// /// The width of the rectangle. /// The height of the rectangle. public RDRect(float? width, float? height) : this(new float?(0f), height, width, new float?(0f)) { } /// /// Gets the location of the rectangle. /// public readonly RDPoint Location => new(Left, Bottom); /// /// Gets the size of the rectangle. /// public readonly RDSize Size => new(Width, Height); /// /// Inflates the specified rectangle by the specified size. /// /// The rectangle to inflate. /// The size to inflate by. /// The inflated rectangle. public static RDRect Inflate(RDRect rect, RDSize size) { RDRect result = new(rect.Left, rect.Top, rect.Right, rect.Bottom); result.Inflate(size); return result; } /// /// Inflates the specified rectangle by the specified width and height. /// /// The rectangle to inflate. /// The width to inflate by. /// The height to inflate by. /// The inflated rectangle. public static RDRect Inflate(RDRect rect, float? x, float? y) { RDRect result = new(rect.Left, rect.Top, rect.Right, rect.Bottom); result.Inflate(x, y); return result; } /// /// Returns the union of two rectangles. /// /// The first rectangle. /// The second rectangle. /// The union of the two rectangles. public static RDRect Union(RDRect rect1, RDRect rect2) { RDRect Union = new(new float?((rect1.Left == null || rect2.Left == null) ? 0f : Math.Min(rect1.Left.Value, rect2.Left.Value)), new float?((rect1.Top == null || rect2.Top == null) ? 0f : Math.Min(rect1.Top.Value, rect2.Top.Value)), new float?((rect1.Right == null || rect2.Right == null) ? 0f : Math.Min(rect1.Right.Value, rect2.Right.Value)), new float?((rect1.Bottom == null || rect2.Bottom == null) ? 0f : Math.Min(rect1.Bottom.Value, rect2.Bottom.Value))); return Union; } /// /// Returns the intersection of two rectangles. /// /// The first rectangle. /// The second rectangle. /// The intersection of the two rectangles. public static RDRect Intersect(RDRect rect1, RDRect rect2) => rect1.IntersectsWithInclusive(rect2) ? new RDRect(new float?((rect1.Left == null || rect2.Left == null) ? 0f : Math.Max(rect1.Left.Value, rect2.Left.Value)), new float?((rect1.Top == null || rect2.Top == null) ? 0f : Math.Max(rect1.Top.Value, rect2.Top.Value)), new float?((rect1.Right == null || rect2.Right == null) ? 0f : Math.Min(rect1.Right.Value, rect2.Right.Value)), new float?((rect1.Bottom == null || rect2.Bottom == null) ? 0f : Math.Min(rect1.Bottom.Value, rect2.Bottom.Value))) : default; /// /// Truncates the edges of the specified rectangle. /// /// The rectangle to truncate. /// The truncated rectangle. public static RDRect Truncate(RDRect rect) { RDRect Truncate = new( rect.Left == null ? null : (float)Math.Truncate((double)rect.Left), rect.Top == null ? null : (float)Math.Truncate((double)rect.Top), rect.Right == null ? null : (float)Math.Truncate((double)rect.Right), rect.Bottom == null ? null : (float)Math.Truncate((double)rect.Bottom)); return Truncate; } /// /// Offsets the rectangle by the specified width and height. /// /// The width to offset by. /// The height to offset by. public void Offset(float? x, float? y) { Left += x; Top += y; Right += x; Bottom += y; } /// /// Offsets the rectangle by the specified point. /// /// The point to offset by. public void Offset(RDPoint p) => Offset(p.X, p.Y); /// /// Inflates the rectangle by the specified size. /// /// The size to inflate by. public void Inflate(RDSize size) { Left -= size.Width; Top += size.Height; Right += size.Width; Bottom -= size.Height; } /// /// Inflates the rectangle by the specified width and height. /// /// The width to inflate by. /// The height to inflate by. public void Inflate(float? width, float? height) { Left -= width; Top += height; Right += width; Bottom -= height; } /// /// Determines whether the rectangle contains the specified point. /// /// The x-coordinate of the point. /// The y-coordinate of the point. /// true if the rectangle contains the point; otherwise, false. public readonly bool Contains(float? x, float? y) => Left < x && x < Right && Bottom < y && y < Top; /// /// Determines whether the rectangle contains the specified point. /// /// The point to check. /// true if the rectangle contains the point; otherwise, false. public readonly bool Contains(RDPoint p) => Contains(p.X, p.Y); /// /// Determines whether the rectangle contains the specified rectangle. /// /// The rectangle to check. /// true if the rectangle contains the specified rectangle; otherwise, false. public readonly bool Contains(RDRect rect) => Left < rect.Left && rect.Right < Right && Bottom < rect.Bottom && rect.Top < Top; /// /// Returns the union of this rectangle and the specified rectangle. /// /// The rectangle to union with. /// The union of the two rectangles. public readonly RDRect Union(RDRect rect) => Union(this, rect); /// /// Returns the intersection of this rectangle and the specified rectangle. /// /// The rectangle to intersect with. /// The intersection of the two rectangles. public readonly object Intersect(RDRect rect) => Intersect(this, rect); /// /// Determines whether this rectangle intersects with the specified rectangle. /// /// The rectangle to check. /// true if the rectangles intersect; otherwise, false. public readonly bool IntersectsWith(RDRect rect) => Left < rect.Right && Right > rect.Left && Top < rect.Bottom && Bottom > rect.Top; /// /// Determines whether this rectangle intersects with the specified rectangle, including edges. /// /// The rectangle to check. /// true if the rectangles intersect; otherwise, false. public readonly bool IntersectsWithInclusive(RDRect rect) => Left <= rect.Right && Right >= rect.Left && Top <= rect.Bottom && Bottom >= rect.Top; /// public static bool operator ==(RDRect rect1, RDRect rect2) => rect1.Equals(rect2); /// public static bool operator !=(RDRect rect1, RDRect rect2) => !rect1.Equals(rect2); /// public override readonly bool Equals([NotNullWhen(true)] object? obj) => obj is RDRect e && Equals(e); /// public override readonly int GetHashCode() => HashCode.Combine(Left, Top, Right, Bottom); /// public override readonly string ToString() => $"{{Location=[{Left?.ToString() ?? "null"},{Bottom?.ToString() ?? "null"}],Size=[{Width?.ToString() ?? "null"},{Height?.ToString() ?? "null"}]}}"; /// public readonly bool Equals(RDRect other) => Left == other.Left && Top == other.Top && Right == other.Right && Bottom == other.Bottom; /// /// Implicitly converts an to an . /// /// The to convert. /// The converted . public static implicit operator RDRectE(RDRect rect) => new(rect.Left, rect.Top, rect.Right, rect.Bottom); private readonly string GetDebuggerDisplay() => ToString(); } }