using System.Collections.Generic; using Syntriax.Engine.Core; namespace Syntriax.Engine.Physics2D.Primitives; /// /// Represents an Axis-Aligned Bounding Box (AABB) in 2D space. /// /// The lower boundary of the . /// The upper boundary of the . /// /// Initializes a new instance of the struct with the specified lower and upper boundaries. /// [System.Diagnostics.DebuggerDisplay("LowerBoundary: {LowerBoundary.ToString(), nq}, UpperBoundary: {UpperBoundary.ToString(), nq}")] public readonly struct AABB(Vector2D lowerBoundary, Vector2D upperBoundary) { /// /// The lower boundary of the . /// public readonly Vector2D LowerBoundary = lowerBoundary; /// /// The upper boundary of the . /// public readonly Vector2D UpperBoundary = upperBoundary; /// /// Gets the center point of the . /// public readonly Vector2D Center => (LowerBoundary + UpperBoundary) * .5f; /// /// Gets the size of the . /// public readonly Vector2D Size => LowerBoundary.FromTo(UpperBoundary).Abs(); /// /// Gets half the size of the . /// public readonly Vector2D SizeHalf => Size * .5f; /// /// Creates an from a collection of s. /// /// The collection of s. /// An that bounds all the s. public static AABB FromVectors(IEnumerable vectors) { int counter = 0; Vector2D lowerBoundary = new(float.MaxValue, float.MaxValue); Vector2D upperBoundary = new(float.MinValue, float.MinValue); foreach (Vector2D vector in vectors) { 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); } /// /// Checks if two s are approximately equal. /// /// The first . /// The second . /// if the s are approximately equal; otherwise, . public static bool ApproximatelyEquals(AABB left, AABB right) => left.LowerBoundary.ApproximatelyEquals(right.LowerBoundary) && left.UpperBoundary.ApproximatelyEquals(right.UpperBoundary); } /// /// Provides extension methods for the struct. /// public static class AABBExtensions { /// /// Converts a collection of s to an . /// /// The collection of s. /// An that bounds all the s. public static AABB ToAABB(this IEnumerable vectors) => AABB.FromVectors(vectors); /// /// Checks if two s are approximately equal. /// /// The first . /// The second . /// if the s are approximately equal; otherwise, . public static bool ApproximatelyEquals(this AABB left, AABB right) => AABB.ApproximatelyEquals(left, right); }