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;
|
2019-10-25 23:20:43 +00:00
|
|
|
|
using MoreLinq;
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
|
|
|
|
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>
|
2019-09-06 08:11:58 +00:00
|
|
|
|
public struct Polygon : IShape2D, IEquatable<IShape2D>
|
|
|
|
|
{
|
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 == null ? Enumerable.Empty<Position2D>() : vertices; } }
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
|
|
|
|
// vertices are local to the origin
|
|
|
|
|
public Polygon(params Position2D[] vertices)
|
|
|
|
|
{
|
2019-12-02 05:51:52 +00:00
|
|
|
|
this.vertices = ImmutableArray.Create<Position2D>(vertices);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Polygon(ImmutableArray<Position2D> vertices)
|
|
|
|
|
{
|
|
|
|
|
this.vertices = vertices;
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vector2 Support(Vector2 direction, Transform2D transform)
|
|
|
|
|
{
|
2019-10-25 23:20:43 +00:00
|
|
|
|
return Vertices.Select(vertex => Vector2.Transform(vertex, transform.TransformMatrix)).MaxBy(transformed => Vector2.Dot(transformed, direction)).First();
|
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
|
|
|
|
{
|
2019-10-25 23:20:43 +00:00
|
|
|
|
if (obj is IShape2D other)
|
2019-09-06 08:11:58 +00:00
|
|
|
|
{
|
2019-10-25 23:20:43 +00:00
|
|
|
|
return Equals(other);
|
|
|
|
|
}
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
2019-10-25 23:20:43 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
2019-10-25 23:20:43 +00:00
|
|
|
|
public bool Equals(IShape2D other)
|
|
|
|
|
{
|
|
|
|
|
if (other is Polygon otherPolygon)
|
|
|
|
|
{
|
2019-12-02 05:51:52 +00:00
|
|
|
|
var q = from a in vertices
|
|
|
|
|
join b in otherPolygon.vertices on a equals b
|
|
|
|
|
select a;
|
|
|
|
|
|
|
|
|
|
return vertices.Length == otherPolygon.vertices.Length && q.Count() == vertices.Length;
|
|
|
|
|
}
|
|
|
|
|
else if (other is Rectangle rectangle)
|
|
|
|
|
{
|
|
|
|
|
var q = from a in vertices
|
|
|
|
|
join b in rectangle.Vertices on a equals b
|
|
|
|
|
select a;
|
|
|
|
|
|
|
|
|
|
return vertices.Length == 4 && q.Count() == vertices.Length;
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-10-25 23:20:43 +00:00
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
2019-12-02 05:51:52 +00:00
|
|
|
|
return HashCode.Combine(vertices, 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
|
|
|
|
}
|
|
|
|
|
}
|