2019-09-27 08:59:21 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
|
|
|
|
namespace Base
|
|
|
|
|
{
|
2019-10-06 17:13:10 +02:00
|
|
|
|
public struct Rect
|
2019-09-27 08:59:21 +02:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a rect from the provided data
|
|
|
|
|
/// </summary>
|
2019-10-05 17:49:56 +02:00
|
|
|
|
/// <param name="Location">Position</param>
|
|
|
|
|
/// <param name="Size">Rect's size</param>
|
|
|
|
|
/// <param name="centered">Whether the Rect should be created top-right or around the Location</param>
|
|
|
|
|
public Rect(Vector2 Location, Vector2 Size, bool centered = false) : this(Location.X, Location.Y, Size.X, Size.Y, centered) { }
|
2019-09-27 08:59:21 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a rect from the provided data
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="X">X in world-coordinates</param>
|
|
|
|
|
/// <param name="Y">Y in world-coordinates</param>
|
|
|
|
|
/// <param name="Width">Width</param>
|
|
|
|
|
/// <param name="Height">Height</param>
|
2019-10-05 17:49:56 +02:00
|
|
|
|
/// <param name="centered">Whether the Rect should be created top-right or around the Location</param>
|
2019-09-27 08:59:21 +02:00
|
|
|
|
public Rect(double X, double Y, double Width, double Height, bool centered = false)
|
|
|
|
|
{
|
2019-10-06 17:13:10 +02:00
|
|
|
|
this.X = X;
|
|
|
|
|
this.Y = Y;
|
|
|
|
|
this.Width = Width;
|
|
|
|
|
this.Height = Height;
|
|
|
|
|
if (centered)
|
2019-10-05 17:49:56 +02:00
|
|
|
|
{
|
2019-10-06 17:13:10 +02:00
|
|
|
|
this.X -= Width / 2;
|
2019-10-07 17:26:55 +02:00
|
|
|
|
this.Y -= Height / 2;
|
2019-10-05 17:49:56 +02:00
|
|
|
|
}
|
2019-09-27 08:59:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Copies the Rect's data
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="rect"></param>
|
|
|
|
|
public Rect(Rect rect)
|
|
|
|
|
{
|
2019-10-06 17:13:10 +02:00
|
|
|
|
X = rect.X;
|
|
|
|
|
Y = rect.Y;
|
|
|
|
|
Width = rect.Width;
|
|
|
|
|
Height = rect.Height;
|
2019-09-27 08:59:21 +02:00
|
|
|
|
}
|
2019-10-05 17:49:56 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Copies the Rect's data
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="rect"></param>
|
2019-10-06 17:13:10 +02:00
|
|
|
|
public Rect(Rectangle rect, bool transform = false) : this(rect.X, rect.Y, rect.Width, rect.Height, false) { }
|
2019-10-05 17:49:56 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Copies the Rect's data
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="rect"></param>
|
2019-10-06 17:13:10 +02:00
|
|
|
|
public Rect(RectangleF rect, bool transform = false) : this(rect.X, rect.Y, rect.Width, rect.Height, false) { }
|
2019-09-27 08:59:21 +02:00
|
|
|
|
|
2019-10-06 17:13:10 +02:00
|
|
|
|
public double X;
|
|
|
|
|
public double Y;
|
|
|
|
|
public double Width;
|
|
|
|
|
public double Height;
|
|
|
|
|
public Vector2 Location => new Vector2(X, Y);
|
|
|
|
|
public Vector2 Size => new Vector2(Width, Height);
|
|
|
|
|
public double Bottom => Y;
|
|
|
|
|
public double Top => Y + Height;
|
|
|
|
|
public double Left => X;
|
|
|
|
|
public double Right => X + Width;
|
|
|
|
|
public Vector2 bottomLeftPoint => new Vector2(Left, Bottom);
|
|
|
|
|
public Vector2 topLeftPoint => new Vector2(Left, Top);
|
|
|
|
|
public Vector2 bottomRightPoint => new Vector2(Right, Bottom);
|
|
|
|
|
public Vector2 topRightPoint => new Vector2(Right, Top);
|
2019-09-27 08:59:21 +02:00
|
|
|
|
public bool doOverlap(Rect other) => (Left <= other.Right && other.Left <= Right) || (Left >= other.Right && other.Left >= Right);
|
|
|
|
|
public Rectangle toRectangle() => new Rectangle(Misc.d2i(X), Misc.d2i(Y), Misc.d2i(Width), Misc.d2i(Height));
|
|
|
|
|
public RectangleF toRectangleF() => new RectangleF(Misc.d2f(X), Misc.d2f(Y), Misc.d2f(Width), Misc.d2f(Height));
|
|
|
|
|
}
|
|
|
|
|
}
|