37 lines
1.2 KiB
C#
37 lines
1.2 KiB
C#
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);
|
|
}
|