using System;
using System.Text;
using UnityEngine;
namespace Framework.Geometry.Areas {
/// Axis aligned rectangle in 2D
public struct Rectangle2 {
/// A rectangle with no area
public static Rectangle2 Empty = Rectangle2.FromMinMax(0.0f, 0.0f, 0.0f, 0.0f);
/// Coordinates of the rectangle's lesser corner
public Vector2 Min;
/// Coordinates of the rectangle's larger corner
public Vector2 Max;
/// Initializes a new rectangle using the specified coordinates
/// Minimum X coordinate of the rectangle
/// Minimum Y coordinate of the rectangle
/// Maximum X coordinate of the rectangle
/// Maximum Y coordinate of the rectangle
/// The rectangle constructed from the specified boundaries
public static Rectangle2 FromMinMax(float minX, float minY, float maxX, float maxY) {
return new Rectangle2() {
Min = new Vector2(minX, minY),
Max = new Vector2(maxX, maxY)
};
}
/// Initializes a rectangle from a starting point, width and height
/// Starting point X coordinate
/// Starting point Y coordinate
/// Width of the rectangle
/// Height of the rectangle
/// A rectangle with the specified starting point, width and height
public static Rectangle2 FromPointAndSize(float x, float y, float width, float height) {
return new Rectangle2() {
Min = new Vector2(x, y),
Max = new Vector2(x + width, y + height)
};
}
/// Initializes a new rectangle using the specified min max points
/// Minimum coordinates of the rectangle
/// Maximum coordinates of the rectangle
/// The rectangle constructed from the specified boundaries
public static Rectangle2 FromMinMax(Vector2 min, Vector2 max) {
return new Rectangle2() {
Min = min,
Max = max
};
}
/// Initializes a rectangle from a starting corner, width and height
/// Starting point
/// Size of the rectangle
/// A rectangle with the specified starting point, width and height
public static Rectangle2 FromPointAndSize(Vector2 min, Vector2 size) {
return new Rectangle2() {
Min = min,
Max = min + size
};
}
/// Width of the rectangle
public float Width {
get { return Math.Abs(this.Max.x - this.Min.x); }
}
/// Height of the rectangle
public float Height {
get { return Math.Abs(this.Max.y - this.Min.y); }
}
/// Area contained within the rectangle
public float Area {
get { return Width * Height; }
}
/// Circumference of the rectangle's outline
public float Circumference {
get { return (Width * 2) + (Height * 2); }
}
/// Checks two rectangles for inequality
/// First instance to be compared
/// Second instance fo tbe compared
/// True if the instances differ or exactly one reference is set to null
public static bool operator !=(Rectangle2 first, Rectangle2 second) {
return (
(first.Min.x != second.Min.x) ||
(first.Min.y != second.Min.y) ||
(first.Max.x != second.Max.x) ||
(first.Max.y != second.Max.y)
);
}
/// Checks two rectangles for equality
/// First instance to be compared
/// Second instance fo tbe compared
/// True if both instances are equal or both references are null
public static bool operator ==(Rectangle2 first, Rectangle2 second) {
return (
(first.Min.x == second.Min.x) &&
(first.Min.y == second.Min.y) &&
(first.Max.x == second.Max.x) &&
(first.Max.y == second.Max.y)
);
}
/// Checks whether this rectangle is equal to another rectangle
/// Other rectangle, boxed into an object
/// True if the other object is a rectangle with the same attributes
public override bool Equals(object otherAsObject) {
if(otherAsObject is Rectangle2) {
return (this == (Rectangle2)otherAsObject);
} else {
return false;
}
}
///
/// Generates a hash code that is identical for instances with the same state
///
/// A hash code for the instance
public override int GetHashCode() {
return (
(this.Min.x.GetHashCode()) ^
(this.Min.y.GetHashCode() << 8) ^
(this.Max.x.GetHashCode() << 16) ^
(this.Max.y.GetHashCode() << 24)
);
}
/// Describes the 2D rectangle as a string
/// A string describing the 2D rectangle
public override string ToString() {
var builder = new StringBuilder();
builder.Append('{');
builder.Append(this.Min.x);
builder.Append(';');
builder.Append(this.Min.y);
builder.Append("} - {");
builder.Append(this.Max.x);
builder.Append(';');
builder.Append(this.Max.y);
builder.Append('}');
return builder.ToString();
}
}
} // namespace Framework.Geometry.Areas