2020-01-02 04:01:05 +00:00
|
|
|
|
using System;
|
2019-10-25 23:20:43 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-12-02 05:51:52 +00:00
|
|
|
|
using System.Collections.Immutable;
|
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>
|
2019-12-02 05:51:52 +00:00
|
|
|
|
/// A Shape defined by an arbitrary collection of vertices.
|
2020-01-02 04:01:05 +00:00
|
|
|
|
/// NOTE: A Polygon must be defined in clockwise order, have more than 2 vertices, be convex, and have no duplicate vertices.
|
2019-10-25 21:01:36 +00:00
|
|
|
|
/// </summary>
|
2019-12-09 03:46:08 +00:00
|
|
|
|
public struct Polygon : IShape2D, IEquatable<Polygon>
|
2019-09-06 08:11:58 +00:00
|
|
|
|
{
|
2020-01-02 04:01:05 +00:00
|
|
|
|
public ImmutableArray<Position2D> Vertices { get; private set; }
|
2019-12-30 06:19:10 +00:00
|
|
|
|
public AABB AABB { get; }
|
2019-10-25 23:20:43 +00:00
|
|
|
|
|
2020-01-02 04:01:05 +00:00
|
|
|
|
public int VertexCount { get { return Vertices.Length; } }
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
|
|
|
|
// vertices are local to the origin
|
2019-12-30 06:19:10 +00:00
|
|
|
|
public Polygon(IEnumerable<Position2D> vertices)
|
2019-09-06 08:11:58 +00:00
|
|
|
|
{
|
2020-01-02 04:01:05 +00:00
|
|
|
|
Vertices = vertices.ToImmutableArray();
|
2019-12-30 06:19:10 +00:00
|
|
|
|
AABB = AABB.FromVertices(vertices);
|
2019-12-02 05:51:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Polygon(ImmutableArray<Position2D> vertices)
|
|
|
|
|
{
|
2020-01-02 04:01:05 +00:00
|
|
|
|
Vertices = vertices;
|
2019-12-30 06:19:10 +00:00
|
|
|
|
AABB = AABB.FromVertices(vertices);
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vector2 Support(Vector2 direction, Transform2D transform)
|
|
|
|
|
{
|
2019-12-16 18:51:27 +00:00
|
|
|
|
var maxDotProduct = float.NegativeInfinity;
|
2020-01-02 04:01:05 +00:00
|
|
|
|
var maxVertex = Vertices[0].ToVector2();
|
2019-12-16 18:51:27 +00:00
|
|
|
|
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;
|
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-09-06 08:11:58 +00:00
|
|
|
|
{
|
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
|
|
|
|
|
2019-10-25 23:20:43 +00:00
|
|
|
|
public bool Equals(IShape2D other)
|
|
|
|
|
{
|
2020-01-02 09:40:03 +00:00
|
|
|
|
return (other is Polygon otherPolygon && Equals(otherPolygon));
|
2019-12-09 03:46:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Equals(Polygon other)
|
|
|
|
|
{
|
2020-01-02 04:01:05 +00:00
|
|
|
|
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; }
|
|
|
|
|
}
|
2019-12-09 03:46:08 +00:00
|
|
|
|
|
2020-01-02 04:01:05 +00:00
|
|
|
|
return true;
|
2019-12-09 03:46:08 +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(Vertices);
|
2019-10-25 23:20:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator ==(Polygon a, Polygon b)
|
|
|
|
|
{
|
|
|
|
|
return a.Equals(b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator !=(Polygon a, Polygon b)
|
|
|
|
|
{
|
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
2019-12-02 05:51:52 +00:00
|
|
|
|
|
|
|
|
|
public static bool operator ==(Polygon a, Rectangle b)
|
|
|
|
|
{
|
|
|
|
|
return a.Equals(b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator !=(Polygon a, Rectangle b)
|
|
|
|
|
{
|
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|