MoonTools.Bonk/Bonk/Shapes/2D/Rectangle.cs

119 lines
3.3 KiB
C#
Raw Normal View History

2019-09-06 08:11:58 +00:00
using System;
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>
2020-01-05 00:13:07 +00:00
/// A rectangle is a shape defined by a width and height. The origin is the center of the rectangle.
2019-10-25 21:01:36 +00:00
/// </summary>
public struct Rectangle : IShape2D, IEquatable<Rectangle>
2019-09-06 08:11:58 +00:00
{
2020-09-07 02:38:51 +00:00
public AABB2D AABB { get; }
2020-01-02 04:01:05 +00:00
public int Width { get; }
public int Height { get; }
2020-01-02 09:40:03 +00:00
public float Right { get; }
public float Left { get; }
public float Top { get; }
public float Bottom { get; }
public Vector2 BottomLeft { get; }
public Vector2 TopRight { get; }
2020-01-02 04:01:05 +00:00
2020-01-02 09:40:03 +00:00
public Vector2 Min { get; }
public Vector2 Max { get; }
2019-09-06 08:11:58 +00:00
public Rectangle(int left, int top, int width, int height)
2019-09-06 08:11:58 +00:00
{
2020-01-02 09:40:03 +00:00
Width = width;
Height = height;
Left = left;
Right = left + width;
Top = top;
Bottom = top + height;
2020-09-07 02:38:51 +00:00
AABB = new AABB2D(left, top, Right, Bottom);
2020-01-02 09:40:03 +00:00
BottomLeft = new Vector2(Left, Bottom);
TopRight = new Vector2(Top, Right);
Min = AABB.Min;
Max = AABB.Max;
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)
{
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
}
2020-09-07 02:38:51 +00:00
public AABB2D TransformedAABB(Transform2D transform)
2019-09-06 08:11:58 +00:00
{
2020-09-07 02:38:51 +00:00
return AABB2D.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)
{
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)
{
2020-01-02 09:40:03 +00:00
return (other is Rectangle rectangle && Equals(rectangle));
}
2019-09-06 08:11:58 +00:00
public bool Equals(Rectangle other)
{
2020-01-02 04:01:05 +00:00
return Min == other.Min && Max == other.Max;
}
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
2020-09-07 02:38:51 +00:00
public static bool operator ==(Rectangle a, Polygon2D b)
2020-01-02 04:01:05 +00:00
{
return a.Equals(b);
}
2020-09-07 02:38:51 +00:00
public static bool operator !=(Rectangle a, Polygon2D b)
2020-01-02 04:01:05 +00:00
{
return !(a == b);
}
2019-09-06 08:11:58 +00:00
}
}