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

111 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Numerics;
using MoonTools.Structs;
namespace MoonTools.Bonk
{
/// <summary>
/// A Shape defined by an arbitrary collection of vertices.
/// NOTE: A Polygon must be defined in clockwise order, have more than 2 vertices, be convex, and have no duplicate vertices.
/// </summary>
public struct Polygon2D : IShape2D, IEquatable<Polygon2D>
{
public ImmutableArray<Position2D> Vertices { get; private set; }
public AABB2D AABB { get; }
public int VertexCount { get { return Vertices.Length; } }
// vertices are local to the origin
public Polygon2D(IEnumerable<Position2D> vertices)
{
Vertices = vertices.ToImmutableArray();
AABB = AABB2D.FromVertices(vertices);
}
public Polygon2D(ImmutableArray<Position2D> vertices)
{
Vertices = vertices;
AABB = AABB2D.FromVertices(vertices);
}
public Vector2 Support(Vector2 direction, Transform2D transform)
{
var maxDotProduct = float.NegativeInfinity;
var maxVertex = Vertices[0].ToVector2();
foreach (var vertex in Vertices)
{
var transformed = Vector2.Transform(vertex, transform.TransformMatrix);
var dot = Vector2.Dot(transformed, direction);
if (dot > maxDotProduct)
{
maxVertex = transformed;
maxDotProduct = dot;
}
}
return maxVertex;
}
public AABB2D TransformedAABB(Transform2D transform)
{
return AABB2D.Transformed(AABB, transform);
}
public override bool Equals(object obj)
{
return obj is IShape2D other && Equals(other);
}
public bool Equals(IShape2D other)
{
return other is Polygon2D otherPolygon && Equals(otherPolygon);
}
public bool Equals(Polygon2D other)
{
if (VertexCount != other.VertexCount) { return false; }
int? offset = null;
for (var i = 0; i < VertexCount; i++)
{
if (Vertices[0] == other.Vertices[i]) { offset = i; break; }
}
if (!offset.HasValue) { return false; }
for (var i = 0; i < VertexCount; i++)
{
if (Vertices[i] != other.Vertices[(i + offset.Value) % VertexCount]) { return false; }
}
return true;
}
public override int GetHashCode()
{
return HashCode.Combine(Vertices);
}
public static bool operator ==(Polygon2D a, Polygon2D b)
{
return a.Equals(b);
}
public static bool operator !=(Polygon2D a, Polygon2D b)
{
return !(a == b);
}
public static bool operator ==(Polygon2D a, Rectangle b)
{
return a.Equals(b);
}
public static bool operator !=(Polygon2D a, Rectangle b)
{
return !(a == b);
}
}
}