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

111 lines
3.1 KiB
C#
Raw Normal View History

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>
2020-09-07 02:38:51 +00:00
public struct Polygon2D : IShape2D, IEquatable<Polygon2D>
2019-09-06 08:11:58 +00:00
{
2020-01-02 04:01:05 +00:00
public ImmutableArray<Position2D> Vertices { get; private set; }
2020-09-07 02:38:51 +00:00
public AABB2D 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
2020-09-07 02:38:51 +00:00
public Polygon2D(IEnumerable<Position2D> vertices)
2019-09-06 08:11:58 +00:00
{
2020-01-02 04:01:05 +00:00
Vertices = vertices.ToImmutableArray();
2020-09-07 02:38:51 +00:00
AABB = AABB2D.FromVertices(vertices);
2019-12-02 05:51:52 +00:00
}
2020-09-07 02:38:51 +00:00
public Polygon2D(ImmutableArray<Position2D> vertices)
2019-12-02 05:51:52 +00:00
{
2020-01-02 04:01:05 +00:00
Vertices = vertices;
2020-09-07 02:38:51 +00:00
AABB = AABB2D.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
}
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)
2019-09-06 08:11:58 +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-09-07 02:38:51 +00:00
return other is Polygon2D otherPolygon && Equals(otherPolygon);
}
2020-09-07 02:38:51 +00:00
public bool Equals(Polygon2D 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; }
}
2020-01-02 04:01:05 +00:00
return true;
}
2019-10-25 23:20:43 +00:00
public override int GetHashCode()
{
return HashCode.Combine(Vertices);
2019-10-25 23:20:43 +00:00
}
2020-09-07 02:38:51 +00:00
public static bool operator ==(Polygon2D a, Polygon2D b)
2019-10-25 23:20:43 +00:00
{
return a.Equals(b);
}
2020-09-07 02:38:51 +00:00
public static bool operator !=(Polygon2D a, Polygon2D b)
2019-10-25 23:20:43 +00:00
{
return !(a == b);
}
2019-12-02 05:51:52 +00:00
2020-09-07 02:38:51 +00:00
public static bool operator ==(Polygon2D a, Rectangle b)
2019-12-02 05:51:52 +00:00
{
return a.Equals(b);
}
2020-09-07 02:38:51 +00:00
public static bool operator !=(Polygon2D a, Rectangle b)
2019-12-02 05:51:52 +00:00
{
return !(a == b);
}
2019-09-06 08:11:58 +00:00
}
}