fix: AABB.FromVectors
This commit is contained in:
parent
bfab35c27e
commit
326bcfca61
|
@ -6,21 +6,23 @@ namespace Syntriax.Engine.Physics2D.Primitives;
|
||||||
|
|
||||||
public record AABB(Vector2D LowerBoundary, Vector2D UpperBoundary)
|
public record AABB(Vector2D LowerBoundary, Vector2D UpperBoundary)
|
||||||
{
|
{
|
||||||
public static AABB FromVector2Ds(IList<Vector2D> vectors)
|
public static AABB FromVectors(IEnumerable<Vector2D> vectors)
|
||||||
{
|
{
|
||||||
if (vectors.Count < 2)
|
int counter = 0;
|
||||||
throw new System.ArgumentException($"Parameter {nameof(vectors)} must have at least 2 items.");
|
|
||||||
|
|
||||||
Vector2D lowerBoundary = vectors[0];
|
Vector2D lowerBoundary = new(float.MaxValue, float.MaxValue);
|
||||||
Vector2D upperBoundary = vectors[0];
|
Vector2D upperBoundary = new(float.MinValue, float.MinValue);
|
||||||
|
|
||||||
for (int i = 1; i < vectors.Count; i++)
|
foreach (Vector2D vector in vectors)
|
||||||
{
|
{
|
||||||
Vector2D vector = vectors[i];
|
|
||||||
lowerBoundary = Vector2D.Min(lowerBoundary, vector);
|
lowerBoundary = Vector2D.Min(lowerBoundary, vector);
|
||||||
upperBoundary = Vector2D.Max(upperBoundary, vector);
|
upperBoundary = Vector2D.Max(upperBoundary, vector);
|
||||||
|
counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (counter < 2)
|
||||||
|
throw new System.ArgumentException($"Parameter {nameof(vectors)} must have at least 2 items.");
|
||||||
|
|
||||||
return new(lowerBoundary, upperBoundary);
|
return new(lowerBoundary, upperBoundary);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +32,7 @@ public record AABB(Vector2D LowerBoundary, Vector2D UpperBoundary)
|
||||||
|
|
||||||
public static class AABBExtensions
|
public static class AABBExtensions
|
||||||
{
|
{
|
||||||
public static AABB ToAABB(this IList<Vector2D> vectors) => AABB.FromVector2Ds(vectors);
|
public static AABB ToAABB(this IEnumerable<Vector2D> vectors) => AABB.FromVectors(vectors);
|
||||||
|
|
||||||
public static bool ApproximatelyEquals(this AABB left, AABB right) => AABB.ApproximatelyEquals(left, right);
|
public static bool ApproximatelyEquals(this AABB left, AABB right) => AABB.ApproximatelyEquals(left, right);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue