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

69 lines
1.9 KiB
C#
Raw Permalink Normal View History

2023-12-07 10:55:49 +03:00
using System;
using System.Collections.Generic;
2024-01-23 12:16:58 +03:00
using Syntriax.Engine.Core;
2023-12-07 10:55:49 +03:00
namespace Syntriax.Engine.Physics2D.Primitives;
2024-01-23 12:16:58 +03:00
public record Shape(IList<Vector2D> Vertices)
2023-12-07 10:55:49 +03:00
{
public Triangle SuperTriangle
{
get
{
float minX = float.MaxValue, minY = float.MaxValue;
float maxX = float.MinValue, maxY = float.MinValue;
2024-01-23 12:16:58 +03:00
foreach (Vector2D point in Vertices)
2023-12-07 10:55:49 +03:00
{
2024-01-22 18:46:51 +03:00
minX = MathF.Min(minX, point.X);
minY = MathF.Min(minY, point.Y);
maxX = MathF.Max(maxX, point.X);
maxY = MathF.Max(maxY, point.Y);
2023-12-07 10:55:49 +03:00
}
float dx = maxX - minX;
float dy = maxY - minY;
2024-01-22 18:46:51 +03:00
float deltaMax = MathF.Max(dx, dy);
2023-12-07 10:55:49 +03:00
float midX = (minX + maxX) / 2;
float midY = (minY + maxY) / 2;
2024-01-23 12:16:58 +03:00
Vector2D p1 = new Vector2D((float)midX - 20f * (float)deltaMax, (float)midY - (float)deltaMax);
Vector2D p2 = new Vector2D((float)midX, (float)midY + 20 * (float)deltaMax);
Vector2D p3 = new Vector2D((float)midX + 20 * (float)deltaMax, (float)midY - (float)deltaMax);
2023-12-07 10:55:49 +03:00
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
}