// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.ComponentModel;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace SixLabors.Primitives
{
///
/// Stores a set of four single precision floating points that represent the location and size of a rectangle.
///
///
/// This struct is fully mutable. This is done (against the guidelines) for the sake of performance,
/// as it avoids the need to create new values for modification operations.
///
public struct RectangleF : IEquatable
{
///
/// Represents a that has X, Y, Width, and Height values set to zero.
///
public static readonly RectangleF Empty = default;
///
/// Initializes a new instance of the struct.
///
/// The horizontal position of the rectangle.
/// The vertical position of the rectangle.
/// The width of the rectangle.
/// The height of the rectangle.
public RectangleF(float x, float y, float width, float height)
{
this.X = x;
this.Y = y;
this.Width = width;
this.Height = height;
}
///
/// Initializes a new instance of the struct.
///
///
/// The which specifies the rectangles point in a two-dimensional plane.
///
///
/// The which specifies the rectangles height and width.
///
public RectangleF(PointF point, SizeF size)
{
this.X = point.X;
this.Y = point.Y;
this.Width = size.Width;
this.Height = size.Height;
}
///
/// Gets or sets the x-coordinate of this .
///
public float X { get; set; }
///
/// Gets or sets the y-coordinate of this .
///
public float Y { get; set; }
///
/// Gets or sets the width of this .
///
public float Width { get; set; }
///
/// Gets or sets the height of this .
///
public float Height { get; set; }
///
/// Gets or sets the coordinates of the upper-left corner of the rectangular region represented by this .
///
[EditorBrowsable(EditorBrowsableState.Never)]
public PointF Location
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new PointF(this.X, this.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
this.X = value.X;
this.Y = value.Y;
}
}
///
/// Gets or sets the size of this .
///
[EditorBrowsable(EditorBrowsableState.Never)]
public SizeF Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new SizeF(this.Width, this.Height);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
this.Width = value.Width;
this.Height = value.Height;
}
}
///
/// Gets a value indicating whether this is empty.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsEmpty => (this.Width <= 0) || (this.Height <= 0);
///
/// Gets the y-coordinate of the top edge of this .
///
public float Top => this.Y;
///
/// Gets the x-coordinate of the right edge of this .
///
public float Right
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => this.X + this.Width;
}
///
/// Gets the y-coordinate of the bottom edge of this .
///
public float Bottom
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => this.Y + this.Height;
}
///
/// Gets the x-coordinate of the left edge of this .
///
public float Left => this.X;
///
/// Creates a with the coordinates of the specified by truncating each coordinate.
///
/// The rectangle.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator Rectangle(RectangleF rectangle) => Rectangle.Truncate(rectangle);
///
/// Compares two objects for equality.
///
/// The on the left side of the operand.
/// The on the right side of the operand.
///
/// True if the current left is equal to the parameter; otherwise, false.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(RectangleF left, RectangleF right) => left.Equals(right);
///
/// Compares two objects for inequality.
///
/// The on the left side of the operand.
/// The on the right side of the operand.
///
/// True if the current left is unequal to the parameter; otherwise, false.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(RectangleF left, RectangleF right) => !left.Equals(right);
///
/// Creates a new with the specified location and size.
/// The left coordinate of the rectangle.
/// The top coordinate of the rectangle.
/// The right coordinate of the rectangle.
/// The bottom coordinate of the rectangle.
/// The .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
// ReSharper disable once InconsistentNaming
public static RectangleF FromLTRB(float left, float top, float right, float bottom) => new RectangleF(left, top, right - left, bottom - top);
///
/// Returns the center point of the given .
///
/// The rectangle.
/// The .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PointF Center(RectangleF rectangle) => new PointF(rectangle.Left + (rectangle.Width / 2), rectangle.Top + (rectangle.Height / 2));
///
/// Creates a rectangle that represents the intersection between and
/// . If there is no intersection, an empty rectangle is returned.
///
/// The first rectangle.
/// The second rectangle.
/// The .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RectangleF Intersect(RectangleF a, RectangleF b)
{
float x1 = MathF.Max(a.X, b.X);
float x2 = MathF.Min(a.Right, b.Right);
float y1 = MathF.Max(a.Y, b.Y);
float y2 = MathF.Min(a.Bottom, b.Bottom);
if (x2 >= x1 && y2 >= y1)
{
return new RectangleF(x1, y1, x2 - x1, y2 - y1);
}
return Empty;
}
///
/// Creates a that is inflated by the specified amount.
///
/// The rectangle.
/// The amount to inflate the width by.
/// The amount to inflate the height by.
/// A new .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RectangleF Inflate(RectangleF rectangle, float x, float y)
{
RectangleF r = rectangle;
r.Inflate(x, y);
return r;
}
///
/// Transforms a rectangle by the given matrix.
///
/// The source rectangle.
/// The transformation matrix.
/// A transformed .
public static RectangleF Transform(RectangleF rectangle, Matrix3x2 matrix)
{
PointF bottomRight = PointF.Transform(new PointF(rectangle.Right, rectangle.Bottom), matrix);
PointF topLeft = PointF.Transform(rectangle.Location, matrix);
return new RectangleF(topLeft, new SizeF(bottomRight - topLeft));
}
///
/// Creates a rectangle that represents the union between and .
///
/// The first rectangle.
/// The second rectangle.
/// The .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RectangleF Union(RectangleF a, RectangleF b)
{
float x1 = MathF.Min(a.X, b.X);
float x2 = MathF.Max(a.Right, b.Right);
float y1 = MathF.Min(a.Y, b.Y);
float y2 = MathF.Max(a.Bottom, b.Bottom);
return new RectangleF(x1, y1, x2 - x1, y2 - y1);
}
///
/// Deconstructs this rectangle into four floats.
///
/// The out value for X.
/// The out value for Y.
/// The out value for the width.
/// The out value for the height.
public void Deconstruct(out float x, out float y, out float width, out float height)
{
x = this.X;
y = this.Y;
width = this.Width;
height = this.Height;
}
///
/// Creates a RectangleF that represents the intersection between this RectangleF and the .
///
/// The rectangle.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Intersect(RectangleF rectangle)
{
RectangleF result = Intersect(rectangle, this);
this.X = result.X;
this.Y = result.Y;
this.Width = result.Width;
this.Height = result.Height;
}
///
/// Inflates this by the specified amount.
///
/// The width.
/// The height.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Inflate(float width, float height)
{
this.X -= width;
this.Y -= height;
this.Width += 2 * width;
this.Height += 2 * height;
}
///
/// Inflates this by the specified amount.
///
/// The size.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Inflate(SizeF size) => this.Inflate(size.Width, size.Height);
///
/// Determines if the specfied point is contained within the rectangular region defined by
/// this .
///
/// The x-coordinate of the given point.
/// The y-coordinate of the given point.
/// The .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(float x, float y) => this.X <= x && x < this.Right && this.Y <= y && y < this.Bottom;
///
/// Determines if the specified point is contained within the rectangular region defined by this .
///
/// The point.
/// The .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(PointF point) => this.Contains(point.X, point.Y);
///
/// Determines if the rectangular region represented by is entirely contained
/// within the rectangular region represented by this .
///
/// The rectangle.
/// The .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(RectangleF rectangle) =>
(this.X <= rectangle.X) && (rectangle.Right <= this.Right) &&
(this.Y <= rectangle.Y) && (rectangle.Bottom <= this.Bottom);
///
/// Determines if the specfied intersects the rectangular region defined by
/// this .
///
/// The other Rectange.
/// The .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IntersectsWith(RectangleF rectangle) =>
(rectangle.X < this.Right) && (this.X < rectangle.Right) &&
(rectangle.Y < this.Bottom) && (this.Y < rectangle.Bottom);
///
/// Adjusts the location of this rectangle by the specified amount.
///
/// The point.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Offset(PointF point) => this.Offset(point.X, point.Y);
///
/// Adjusts the location of this rectangle by the specified amount.
///
/// The amount to offset the x-coordinate.
/// The amount to offset the y-coordinate.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Offset(float dx, float dy)
{
this.X += dx;
this.Y += dy;
}
///
public override int GetHashCode()
{
return HashCode.Combine(this.X, this.Y, this.Width, this.Height);
}
///
public override string ToString()
{
return $"RectangleF [ X={this.X}, Y={this.Y}, Width={this.Width}, Height={this.Height} ]";
}
///
public override bool Equals(object obj) => obj is RectangleF other && this.Equals(other);
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(RectangleF other) =>
this.X.Equals(other.X) &&
this.Y.Equals(other.Y) &&
this.Width.Equals(other.Width) &&
this.Height.Equals(other.Height);
}
}