Engine-Pong/Game/Physics2D/Primitives/Shape.cs

69 lines
1.9 KiB
C#
Raw Normal View History

2023-12-07 10:55:49 +03:00
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 = 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);
}
}
2023-12-07 11:14:18 +03:00
public List<Line> Lines
2023-12-07 10:55:49 +03:00
{
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]));
}
2023-12-07 11:14:18 +03:00
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;
}
2023-12-07 10:55:49 +03:00
}