69 lines
1.9 KiB
C#
69 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
namespace Syntriax.Engine.Physics2D.Primitives;
|
|
|
|
public record Shape(IList<Vector2> Vertices)
|
|
{
|
|
public Triangle SuperTriangle
|
|
{
|
|
get
|
|
{
|
|
float minX = float.MaxValue, minY = float.MaxValue;
|
|
float maxX = float.MinValue, maxY = float.MinValue;
|
|
|
|
foreach (Vector2 point in Vertices)
|
|
{
|
|
minX = MathF.Min(minX, point.X);
|
|
minY = MathF.Min(minY, point.Y);
|
|
maxX = MathF.Max(maxX, point.X);
|
|
maxY = MathF.Max(maxY, point.Y);
|
|
}
|
|
|
|
float dx = maxX - minX;
|
|
float dy = maxY - minY;
|
|
float deltaMax = MathF.Max(dx, dy);
|
|
float midX = (minX + maxX) / 2;
|
|
float midY = (minY + maxY) / 2;
|
|
|
|
Vector2 p1 = new Vector2((float)midX - 20f * (float)deltaMax, (float)midY - (float)deltaMax);
|
|
Vector2 p2 = new Vector2((float)midX, (float)midY + 20 * (float)deltaMax);
|
|
Vector2 p3 = new Vector2((float)midX + 20 * (float)deltaMax, (float)midY - (float)deltaMax);
|
|
|
|
return new Triangle(p1, p2, p3);
|
|
}
|
|
}
|
|
|
|
public List<Line> Lines
|
|
{
|
|
get
|
|
{
|
|
List<Line> lines = new List<Line>(Vertices.Count - 1);
|
|
GetLinesNonAlloc(lines);
|
|
return lines;
|
|
}
|
|
}
|
|
|
|
public void GetLinesNonAlloc(IList<Line> lines)
|
|
{
|
|
lines.Clear();
|
|
for (int i = 0; i < Vertices.Count - 1; i++)
|
|
lines.Add(new(Vertices[i], Vertices[i + 1]));
|
|
lines.Add(new(Vertices[^1], Vertices[0]));
|
|
}
|
|
|
|
public bool ApproximatelyEquals(Shape other)
|
|
{
|
|
if (Vertices.Count != other.Vertices.Count)
|
|
return false;
|
|
|
|
for (int i = 0; i < Vertices.Count; i++)
|
|
if (!Vertices[i].ApproximatelyEquals(other.Vertices[i]))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
}
|