using System;
using System.Drawing;
using Newtonsoft.Json;
namespace SBRL.Utilities
{
///
/// Represents a rectangle in 2D space.
///
/// v0.1
///
/// v0.1 - 1st April 2017
/// - Added this changelog!
/// v0.2 - 4th May 2017
/// - Fixed Overlap(Rectangle otherRectangle) method
///
public struct Rectangle
{
///
/// A rectangle with all it's properties initialised to zero.
///
public static Rectangle Zero = new Rectangle() { X = 0, Y = 0, Width = 0, Height = 0 };
#region Core Data
///
/// The X coordinate of the rectangle.
///
public int X { get; set; }
///
/// The Ycoordinateof the rectangle.
///
/// The y.
public int Y { get; set; }
///
/// The width of the rectangle.
///
public int Width { get; set; }
///
/// The height of the rectangle.
///
public int Height { get; set; }
#endregion
#region Corners
///
/// The top-left corner of the rectangle.
///
[JsonIgnore]
public Vector2 TopLeft {
get {
return new Vector2(X, Y);
}
}
///
/// The top-right corner of the rectangle.
///
[JsonIgnore]
public Vector2 TopRight {
get {
return new Vector2(X + Width, Y);
}
}
///
/// The bottom-left corner of the rectangle.
///
[JsonIgnore]
public Vector2 BottomLeft {
get {
return new Vector2(X, Y + Height);
}
}
///
/// The bottom-right corner of the rectangle.
///
[JsonIgnore]
public Vector2 BottomRight {
get {
return new Vector2(X + Width, Y + Height);
}
}
#endregion
#region Edges
///
/// The Y coordinate of the top of the rectangle.
///
[JsonIgnore]
public int Top {
get {
return Y;
}
set {
Y = value;
}
}
///
/// The Y coordinate of the bottom of the rectangle.
///
[JsonIgnore]
public int Bottom {
get {
return Y + Height;
}
set {
Height = value - Y;
}
}
///
/// The X coordinate of the left side of the rectangle.
///
[JsonIgnore]
public int Left {
get {
return X;
}
set {
X = value;
}
}
///
/// The X coordinate of the right side of the rectangle.
///
[JsonIgnore]
public int Right {
get {
return X + Width;
}
set {
Width = value - X;
}
}
#endregion
public Rectangle(int x, int y, int width, int height)
{
X = x;
Y = y;
Width = width;
Height = height;
}
///
/// Figures out whether this rectangle overlaps another rectangle.
///
/// The other rectangle to check the overlap of.
/// Whether this rectangle overlaps another rectangle.
public bool Overlap(Rectangle otherRectangle)
{
if(Top > otherRectangle.Bottom ||
Bottom < otherRectangle.Top ||
Left > otherRectangle.Right ||
Right < otherRectangle.Left)
return false;
return true;
}
///
/// Returns a Rectangle representing the area that this rectangle overlaps with another.
/// Returns an empty rectangle if the two don't overlap at all.
///
/// The other rectangle that overlaps this one.
/// The area that this rectanagle overlaps with another.
public Rectangle OverlappingArea(Rectangle otherRectangle)
{
if(!Overlap(otherRectangle))
return Rectangle.Zero;
Rectangle result = new Rectangle();
result.Top = Math.Max(Top, otherRectangle.Top);
result.Left = Math.Max(Left, otherRectangle.Left);
result.Bottom = Math.Max(Bottom, otherRectangle.Bottom);
result.Right = Math.Max(Right, otherRectangle.Right);
return result;
}
}
}