feat: AABB.FromVectors

This commit is contained in:
Syntriax 2024-01-24 12:46:31 +03:00
parent 56b46b93eb
commit 468615e4cb
1 changed files with 22 additions and 0 deletions

View File

@ -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<Vector2D> 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<Vector2D> vectors) => AABB.FromVector2Ds(vectors);
public static bool ApproximatelyEquals(this AABB left, AABB right) => AABB.ApproximatelyEquals(left, right);
}