MoonTools.Bonk/Bonk/Shapes/Polygon.cs

108 lines
3.1 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-30 06:19:10 +00:00
private ImmutableArray<Position2D> _vertices;
public AABB AABB { get; }
2019-10-25 23:20:43 +00:00
2019-12-30 06:19:10 +00:00
public IEnumerable<Position2D> Vertices { get { return _vertices; } }
2019-12-30 06:19:10 +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
{
2019-12-30 06:19:10 +00:00
_vertices = vertices.ToImmutableArray();
AABB = AABB.FromVertices(vertices);
2019-12-02 05:51:52 +00:00
}
public Polygon(ImmutableArray<Position2D> vertices)
{
2019-12-30 06:19:10 +00:00
_vertices = vertices;
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;
2019-12-30 06:19:10 +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
{
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)
{
2019-12-30 06:19:10 +00:00
var q = from a in _vertices
join b in other.Vertices on a equals b
select a;
2019-12-30 06:19:10 +00:00
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
}
}