From 326bcfca61f57937b67472bc12c10036429082d4 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Wed, 24 Jan 2024 12:58:51 +0300 Subject: [PATCH] fix: AABB.FromVectors --- Engine.Physics2D/Primitives/AABB.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Engine.Physics2D/Primitives/AABB.cs b/Engine.Physics2D/Primitives/AABB.cs index 3d54e17..6c48d36 100644 --- a/Engine.Physics2D/Primitives/AABB.cs +++ b/Engine.Physics2D/Primitives/AABB.cs @@ -6,21 +6,23 @@ namespace Syntriax.Engine.Physics2D.Primitives; public record AABB(Vector2D LowerBoundary, Vector2D UpperBoundary) { - public static AABB FromVector2Ds(IList vectors) + public static AABB FromVectors(IEnumerable vectors) { - if (vectors.Count < 2) - throw new System.ArgumentException($"Parameter {nameof(vectors)} must have at least 2 items."); + int counter = 0; - Vector2D lowerBoundary = vectors[0]; - Vector2D upperBoundary = vectors[0]; + Vector2D lowerBoundary = new(float.MaxValue, float.MaxValue); + 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); 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); } @@ -30,7 +32,7 @@ public record AABB(Vector2D LowerBoundary, Vector2D UpperBoundary) public static class AABBExtensions { - public static AABB ToAABB(this IList vectors) => AABB.FromVector2Ds(vectors); + public static AABB ToAABB(this IEnumerable vectors) => AABB.FromVectors(vectors); public static bool ApproximatelyEquals(this AABB left, AABB right) => AABB.ApproximatelyEquals(left, right); }