fix: AABB.FromVectors

This commit is contained in:
Syntriax 2024-01-24 12:58:51 +03:00
parent bfab35c27e
commit 326bcfca61
1 changed files with 10 additions and 8 deletions

View File

@ -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);
} }