MoonTools.Bonk/Bonk/Shapes/Polygon.cs

105 lines
3.0 KiB
C#
Raw Normal View History

2019-10-25 23:20:43 +00:00
using System.Linq;
using System;
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;
2019-09-06 08:11:58 +00:00
using MoonTools.Core.Structs;
namespace MoonTools.Core.Bonk
{
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.
2019-12-02 06:21:50 +00:00
/// NOTE: A Polygon must have more than 2 vertices, be convex, and should not have duplicate vertices.
2019-10-25 21:01:36 +00:00
/// </summary>
public struct Polygon : IShape2D, IEquatable<Polygon>
2019-09-06 08:11:58 +00:00
{
2019-12-02 05:51:52 +00:00
private ImmutableArray<Position2D> vertices;
2019-10-25 23:20:43 +00:00
public IEnumerable<Position2D> Vertices { get { return vertices; } }
2019-12-16 18:51:27 +00:00
public int VertexCount { get { return vertices.Length; } }
2019-09-06 08:11:58 +00:00
// vertices are local to the origin
public Polygon(IEnumerable<Position2D> vertices) // TODO: remove this, params is bad because it allocates an array
2019-09-06 08:11:58 +00:00
{
this.vertices = vertices.ToImmutableArray();
2019-12-02 05:51:52 +00:00
}
public Polygon(ImmutableArray<Position2D> vertices)
{
this.vertices = 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;
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;
2019-09-06 08:11:58 +00:00
}
public AABB AABB(Transform2D Transform2D)
{
2019-09-06 20:00:35 +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-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)
{
return (other is Polygon otherPolygon && Equals(otherPolygon)) || (other is Rectangle rectangle && Equals(rectangle));
}
public bool Equals(Polygon other)
{
var q = from a in vertices
join b in other.Vertices on a equals b
select a;
return vertices.Length == other.VertexCount && q.Count() == vertices.Length;
}
public bool Equals(Rectangle rectangle)
{
return RectanglePolygonComparison.Equals(this, rectangle);
2019-09-06 08:11:58 +00:00
}
2019-10-25 23:20:43 +00:00
public override int GetHashCode()
{
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
}
}