refactor: Shape
This commit is contained in:
parent
3b299c947c
commit
bd03d036aa
|
@ -7,14 +7,12 @@ namespace Syntriax.Engine.Physics2D.Primitives;
|
||||||
|
|
||||||
public record Shape(IList<Vector2D> Vertices)
|
public record Shape(IList<Vector2D> Vertices)
|
||||||
{
|
{
|
||||||
public Triangle SuperTriangle
|
public static Triangle GetSuperTriangle(Shape shape)
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
{
|
||||||
float minX = float.MaxValue, minY = float.MaxValue;
|
float minX = float.MaxValue, minY = float.MaxValue;
|
||||||
float maxX = float.MinValue, maxY = float.MinValue;
|
float maxX = float.MinValue, maxY = float.MinValue;
|
||||||
|
|
||||||
foreach (Vector2D point in Vertices)
|
foreach (Vector2D point in shape.Vertices)
|
||||||
{
|
{
|
||||||
minX = MathF.Min(minX, point.X);
|
minX = MathF.Min(minX, point.X);
|
||||||
minY = MathF.Min(minY, point.Y);
|
minY = MathF.Min(minY, point.Y);
|
||||||
|
@ -28,41 +26,46 @@ public record Shape(IList<Vector2D> Vertices)
|
||||||
float midX = (minX + maxX) / 2;
|
float midX = (minX + maxX) / 2;
|
||||||
float midY = (minY + maxY) / 2;
|
float midY = (minY + maxY) / 2;
|
||||||
|
|
||||||
Vector2D p1 = new Vector2D((float)midX - 20f * (float)deltaMax, (float)midY - (float)deltaMax);
|
Vector2D p1 = new((float)midX - 20f * (float)deltaMax, (float)midY - (float)deltaMax);
|
||||||
Vector2D p2 = new Vector2D((float)midX, (float)midY + 20 * (float)deltaMax);
|
Vector2D p2 = new((float)midX, (float)midY + 20 * (float)deltaMax);
|
||||||
Vector2D p3 = new Vector2D((float)midX + 20 * (float)deltaMax, (float)midY - (float)deltaMax);
|
Vector2D p3 = new((float)midX + 20 * (float)deltaMax, (float)midY - (float)deltaMax);
|
||||||
|
|
||||||
return new Triangle(p1, p2, p3);
|
return new Triangle(p1, p2, p3);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public List<Line> Lines
|
public static void GetLines(Shape shape, IList<Line> lines)
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
List<Line> lines = new List<Line>(Vertices.Count - 1);
|
|
||||||
GetLinesNonAlloc(lines);
|
|
||||||
return lines;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void GetLinesNonAlloc(IList<Line> lines)
|
|
||||||
{
|
{
|
||||||
lines.Clear();
|
lines.Clear();
|
||||||
for (int i = 0; i < Vertices.Count - 1; i++)
|
for (int i = 0; i < shape.Vertices.Count - 1; i++)
|
||||||
lines.Add(new(Vertices[i], Vertices[i + 1]));
|
lines.Add(new(shape.Vertices[i], shape.Vertices[i + 1]));
|
||||||
lines.Add(new(Vertices[^1], Vertices[0]));
|
lines.Add(new(shape.Vertices[^1], shape.Vertices[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool ApproximatelyEquals(Shape other)
|
public static List<Line> GetLines(Shape shape)
|
||||||
{
|
{
|
||||||
if (Vertices.Count != other.Vertices.Count)
|
List<Line> lines = new(shape.Vertices.Count - 1);
|
||||||
|
GetLines(shape, lines);
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool ApproximatelyEquals(Shape left, Shape right)
|
||||||
|
{
|
||||||
|
if (left.Vertices.Count != right.Vertices.Count)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (int i = 0; i < Vertices.Count; i++)
|
for (int i = 0; i < left.Vertices.Count; i++)
|
||||||
if (!Vertices[i].ApproximatelyEquals(other.Vertices[i]))
|
if (!left.Vertices[i].ApproximatelyEquals(right.Vertices[i]))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class ShapeExtensions
|
||||||
|
{
|
||||||
|
public static Triangle ToSuperTriangle(this Shape shape) => Shape.GetSuperTriangle(shape);
|
||||||
|
public static void ToLines(this Shape shape, IList<Line> lines) => Shape.GetLines(shape, lines);
|
||||||
|
public static List<Line> ToLines(this Shape shape) => Shape.GetLines(shape);
|
||||||
|
|
||||||
|
public static bool ApproximatelyEquals(this Shape left, Shape right) => Shape.ApproximatelyEquals(left, right);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue