2019-09-06 08:11:58 +00:00
|
|
|
|
using System;
|
2019-10-25 10:46:47 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-10-31 23:19:30 +00:00
|
|
|
|
using System.Numerics;
|
2020-02-21 02:07:59 +00:00
|
|
|
|
using MoonTools.Structs;
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
2020-02-21 02:07:59 +00:00
|
|
|
|
namespace MoonTools.Bonk
|
2019-09-06 08:11:58 +00:00
|
|
|
|
{
|
2019-10-25 21:01:36 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A rectangle is a shape defined by a minimum and maximum X value and a minimum and maximum Y value.
|
|
|
|
|
/// </summary>
|
2019-12-09 03:46:08 +00:00
|
|
|
|
public struct Rectangle : IShape2D, IEquatable<Rectangle>
|
2019-09-06 08:11:58 +00:00
|
|
|
|
{
|
2020-01-02 04:01:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The minimum position of the rectangle. Note that we assume y-down coordinates.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value></value>
|
|
|
|
|
public Position2D Min { get; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The maximum position of the rectangle. Note that we assume y-down coordinates.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value></value>
|
|
|
|
|
public Position2D Max { get; }
|
2019-12-30 06:19:10 +00:00
|
|
|
|
public AABB AABB { get; }
|
|
|
|
|
|
2020-01-02 04:01:05 +00:00
|
|
|
|
public int Left { get { return Min.X; } }
|
|
|
|
|
public int Right { get { return Max.X; } }
|
|
|
|
|
public int Top { get { return Min.Y; } }
|
|
|
|
|
public int Bottom { get { return Max.Y; } }
|
|
|
|
|
|
|
|
|
|
public int Width { get; }
|
|
|
|
|
public int Height { get; }
|
|
|
|
|
|
|
|
|
|
public Position2D TopRight { get { return new Position2D(Right, Top); } }
|
|
|
|
|
public Position2D BottomLeft { get { return new Position2D(Left, Bottom); } }
|
|
|
|
|
|
2019-12-02 05:51:52 +00:00
|
|
|
|
public IEnumerable<Position2D> Vertices
|
2019-10-25 10:46:47 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-01-02 04:01:05 +00:00
|
|
|
|
yield return new Position2D(Min.X, Min.Y);
|
|
|
|
|
yield return new Position2D(Min.X, Max.Y);
|
|
|
|
|
yield return new Position2D(Max.X, Min.Y);
|
|
|
|
|
yield return new Position2D(Max.X, Max.Y);
|
2019-10-25 10:46:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
|
|
|
|
public Rectangle(int minX, int minY, int maxX, int maxY)
|
|
|
|
|
{
|
2020-01-02 04:01:05 +00:00
|
|
|
|
Min = new Position2D(minX, minY);
|
|
|
|
|
Max = new Position2D(maxX, maxY);
|
2019-12-30 06:19:10 +00:00
|
|
|
|
AABB = new AABB(minX, minY, maxX, maxY);
|
2020-01-02 04:01:05 +00:00
|
|
|
|
Width = Max.X - Min.X;
|
|
|
|
|
Height = Max.Y - Min.Y;
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-30 06:54:21 +00:00
|
|
|
|
private Vector2 Support(Vector2 direction)
|
2019-09-06 08:11:58 +00:00
|
|
|
|
{
|
2019-12-30 06:54:21 +00:00
|
|
|
|
if (direction.X >= 0 && direction.Y >= 0)
|
|
|
|
|
{
|
2020-01-02 04:01:05 +00:00
|
|
|
|
return Max;
|
2019-12-30 06:54:21 +00:00
|
|
|
|
}
|
|
|
|
|
else if (direction.X >= 0 && direction.Y < 0)
|
|
|
|
|
{
|
2020-01-02 04:01:05 +00:00
|
|
|
|
return new Vector2(Max.X, Min.Y);
|
2019-12-30 06:54:21 +00:00
|
|
|
|
}
|
|
|
|
|
else if (direction.X < 0 && direction.Y >= 0)
|
|
|
|
|
{
|
2020-01-02 04:01:05 +00:00
|
|
|
|
return new Vector2(Min.X, Max.Y);
|
2019-12-30 06:54:21 +00:00
|
|
|
|
}
|
|
|
|
|
else if (direction.X < 0 && direction.Y < 0)
|
2019-12-16 18:51:27 +00:00
|
|
|
|
{
|
2020-01-02 04:01:05 +00:00
|
|
|
|
return new Vector2(Min.X, Min.Y);
|
2019-12-16 18:51:27 +00:00
|
|
|
|
}
|
2019-12-30 06:54:21 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Support vector direction cannot be zero.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vector2 Support(Vector2 direction, Transform2D transform)
|
|
|
|
|
{
|
2020-01-02 07:08:14 +00:00
|
|
|
|
Matrix3x2 inverseTransform;
|
|
|
|
|
Matrix3x2.Invert(transform.TransformMatrix, out inverseTransform);
|
2019-12-30 06:54:21 +00:00
|
|
|
|
var inverseDirection = Vector2.TransformNormal(direction, inverseTransform);
|
|
|
|
|
return Vector2.Transform(Support(inverseDirection), transform.TransformMatrix);
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-30 06:19:10 +00:00
|
|
|
|
public AABB TransformedAABB(Transform2D transform)
|
2019-09-06 08:11:58 +00:00
|
|
|
|
{
|
2019-12-30 06:19:10 +00:00
|
|
|
|
return AABB.Transformed(AABB, transform);
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 23:20:43 +00:00
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
2019-12-09 03:46:08 +00:00
|
|
|
|
return obj is IShape2D other && Equals(other);
|
2019-10-25 23:20:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 08:11:58 +00:00
|
|
|
|
public bool Equals(IShape2D other)
|
|
|
|
|
{
|
2019-12-09 03:46:08 +00:00
|
|
|
|
return (other is Rectangle rectangle && Equals(rectangle)) || (other is Polygon polygon && Equals(polygon));
|
|
|
|
|
}
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
2019-12-09 03:46:08 +00:00
|
|
|
|
public bool Equals(Rectangle other)
|
|
|
|
|
{
|
2020-01-02 04:01:05 +00:00
|
|
|
|
return Min == other.Min && Max == other.Max;
|
2019-12-09 03:46:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Equals(Polygon other)
|
|
|
|
|
{
|
|
|
|
|
return RectanglePolygonComparison.Equals(other, this);
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
2019-10-25 23:20:43 +00:00
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
2020-01-02 04:01:05 +00:00
|
|
|
|
return HashCode.Combine(Min, Max);
|
2019-10-25 23:20:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator ==(Rectangle a, Rectangle b)
|
|
|
|
|
{
|
|
|
|
|
return a.Equals(b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator !=(Rectangle a, Rectangle b)
|
|
|
|
|
{
|
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
2020-01-02 04:01:05 +00:00
|
|
|
|
|
|
|
|
|
public static bool operator ==(Rectangle a, Polygon b)
|
|
|
|
|
{
|
|
|
|
|
return a.Equals(b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator !=(Rectangle a, Polygon b)
|
|
|
|
|
{
|
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|