MoonTools.Bonk/Bonk/Shapes/Polygon.cs

61 lines
1.7 KiB
C#
Raw Normal View History

2019-09-06 08:11:58 +00:00
using System;
using Microsoft.Xna.Framework;
using MoonTools.Core.Structs;
namespace MoonTools.Core.Bonk
{
public struct Polygon : IShape2D, IEquatable<IShape2D>
{
public Position2D[] Vertices { get; private set; }
// vertices are local to the origin
public Polygon(params Position2D[] vertices)
{
Vertices = vertices;
}
public Vector2 Support(Vector2 direction, Transform2D transform)
{
var furthest = float.NegativeInfinity;
var furthestVertex = Vector2.Transform(Vertices[0], transform.TransformMatrix);
foreach (var vertex in Vertices)
{
2019-09-06 20:00:35 +00:00
var TransformedVertex = Vector2.Transform(vertex, transform.TransformMatrix);
var distance = Vector2.Dot(TransformedVertex, direction);
2019-09-06 08:11:58 +00:00
if (distance > furthest)
{
furthest = distance;
2019-09-06 20:00:35 +00:00
furthestVertex = TransformedVertex;
2019-09-06 08:11:58 +00:00
}
}
return furthestVertex;
}
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
}
public bool Equals(IShape2D other)
{
if (other is Polygon)
{
var otherPolygon = (Polygon)other;
if (Vertices.Length != otherPolygon.Vertices.Length) { return false; }
for (int i = 0; i < Vertices.Length; i++)
{
if (Vertices[i].ToVector2() != otherPolygon.Vertices[i].ToVector2()) { return false;}
}
return true;
}
return false;
}
}
}