using System; using System.Collections.Generic; using Microsoft.Xna.Framework; namespace Syntriax.Engine.Physics2D.Primitives; public record Shape(IList 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 = Math.Min(minX, point.X); minY = Math.Min(minY, point.Y); maxX = Math.Max(maxX, point.X); maxY = Math.Max(maxY, point.Y); } float dx = maxX - minX; float dy = maxY - minY; float deltaMax = Math.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 Lines { get { List lines = new List(Vertices.Count - 1); GetLinesNonAlloc(lines); return lines; } } public void GetLinesNonAlloc(IList 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; } }