Syntriax.Engine/Engine.Physics2D/Primitives/AABB.cs

37 lines
1.2 KiB
C#
Raw Normal View History

2024-01-24 12:46:31 +03:00
using System.Collections.Generic;
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D.Primitives;
public record AABB(Vector2D LowerBoundary, Vector2D UpperBoundary)
{
2024-01-24 12:46:31 +03:00
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);
}
2024-01-23 19:13:51 +03:00
public static bool ApproximatelyEquals(AABB left, AABB right)
=> left.LowerBoundary.ApproximatelyEquals(right.LowerBoundary) && left.UpperBoundary.ApproximatelyEquals(right.UpperBoundary);
}
2024-01-23 19:13:51 +03:00
public static class AABBExtensions
{
2024-01-24 12:46:31 +03:00
public static AABB ToAABB(this IList<Vector2D> vectors) => AABB.FromVector2Ds(vectors);
2024-01-23 19:13:51 +03:00
public static bool ApproximatelyEquals(this AABB left, AABB right) => AABB.ApproximatelyEquals(left, right);
}