From 468615e4cbeb93b63cd274ef5433eb3bf1a3c530 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Wed, 24 Jan 2024 12:46:31 +0300 Subject: [PATCH] feat: AABB.FromVectors --- Engine.Physics2D/Primitives/AABB.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Engine.Physics2D/Primitives/AABB.cs b/Engine.Physics2D/Primitives/AABB.cs index 5ffef2b..3d54e17 100644 --- a/Engine.Physics2D/Primitives/AABB.cs +++ b/Engine.Physics2D/Primitives/AABB.cs @@ -1,14 +1,36 @@ +using System.Collections.Generic; + using Syntriax.Engine.Core; namespace Syntriax.Engine.Physics2D.Primitives; public record AABB(Vector2D LowerBoundary, Vector2D UpperBoundary) { + public static AABB FromVector2Ds(IList vectors) + { + if (vectors.Count < 2) + throw new System.ArgumentException($"Parameter {nameof(vectors)} must have at least 2 items."); + + Vector2D lowerBoundary = vectors[0]; + Vector2D upperBoundary = vectors[0]; + + for (int i = 1; i < vectors.Count; i++) + { + Vector2D vector = vectors[i]; + lowerBoundary = Vector2D.Min(lowerBoundary, vector); + upperBoundary = Vector2D.Max(upperBoundary, vector); + } + + return new(lowerBoundary, upperBoundary); + } + public static bool ApproximatelyEquals(AABB left, AABB right) => left.LowerBoundary.ApproximatelyEquals(right.LowerBoundary) && left.UpperBoundary.ApproximatelyEquals(right.UpperBoundary); } public static class AABBExtensions { + public static AABB ToAABB(this IList vectors) => AABB.FromVector2Ds(vectors); + public static bool ApproximatelyEquals(this AABB left, AABB right) => AABB.ApproximatelyEquals(left, right); }