2019-09-06 08:11:58 +00:00
|
|
|
|
using System;
|
2019-10-25 10:46:47 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2019-10-31 23:19:30 +00:00
|
|
|
|
using System.Numerics;
|
2019-09-06 08:11:58 +00:00
|
|
|
|
using MoonTools.Core.Structs;
|
2019-10-25 10:46:47 +00:00
|
|
|
|
using MoreLinq;
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
|
|
|
|
namespace MoonTools.Core.Bonk
|
|
|
|
|
{
|
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
|
|
|
|
{
|
|
|
|
|
public int MinX { get; }
|
|
|
|
|
public int MinY { get; }
|
|
|
|
|
public int MaxX { get; }
|
|
|
|
|
public int MaxY { get; }
|
|
|
|
|
|
2019-12-02 05:51:52 +00:00
|
|
|
|
public IEnumerable<Position2D> Vertices
|
2019-10-25 10:46:47 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
yield return new Position2D(MinX, MinY);
|
|
|
|
|
yield return new Position2D(MinX, MaxY);
|
|
|
|
|
yield return new Position2D(MaxX, MinY);
|
|
|
|
|
yield return new Position2D(MaxX, MaxY);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
|
|
|
|
public Rectangle(int minX, int minY, int maxX, int maxY)
|
|
|
|
|
{
|
|
|
|
|
MinX = minX;
|
|
|
|
|
MinY = minY;
|
|
|
|
|
MaxX = maxX;
|
|
|
|
|
MaxY = maxY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vector2 Support(Vector2 direction, Transform2D transform)
|
|
|
|
|
{
|
2019-12-02 05:51:52 +00:00
|
|
|
|
return Vertices.Select(vertex => Vector2.Transform(vertex, transform.TransformMatrix)).MaxBy(transformed => Vector2.Dot(transformed, direction)).First();
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AABB AABB(Transform2D Transform2D)
|
|
|
|
|
{
|
2019-12-02 05:51:52 +00:00
|
|
|
|
return Bonk.AABB.FromTransformedVertices(Vertices, Transform2D);
|
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)
|
|
|
|
|
{
|
|
|
|
|
return MinX == other.MinX &&
|
|
|
|
|
MinY == other.MinY &&
|
|
|
|
|
MaxX == other.MaxX &&
|
|
|
|
|
MaxY == other.MaxY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
2019-12-09 03:46:08 +00:00
|
|
|
|
return HashCode.Combine(MinX, MinY, MaxX, MaxY);
|
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);
|
|
|
|
|
}
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|