namespace Syntriax.Engine.Core; /// /// Represents a range of values along a single axis. /// /// The minimum value of the projection. /// The maximum value of the projection. /// /// Initializes a new instance of the struct with the specified minimum and maximum values. /// [System.Diagnostics.DebuggerDisplay("Min: {Min}, Max: {Max}")] public readonly struct Projection1D(float min, float max) { /// /// Gets the minimum value of the projection. /// public readonly float Min = min; /// /// Gets the maximum value of the projection. /// public readonly float Max = max; /// /// Checks if two projections overlap. /// /// The first projection to check. /// The second projection to check. /// if the projections overlap; otherwise, . public static bool Overlaps(Projection1D left, Projection1D right) => Overlaps(left, right, out float _); /// /// Checks if two projections overlap and calculates the depth of the overlap. /// /// The first projection to check. /// The second projection to check. /// The depth of the overlap, if any. /// if the projections overlap; otherwise, . public static bool Overlaps(Projection1D left, Projection1D right, out float depth) { // TODO Try to improve this bool rightMinInLeft = right.Min > left.Min && right.Min < left.Max; if (rightMinInLeft) { depth = left.Max - right.Min; return true; } bool rightMaxInLeft = right.Max < left.Max && right.Max > left.Min; if (rightMaxInLeft) { depth = left.Min - right.Max; return true; } bool leftMinInRight = left.Min > right.Min && left.Min < right.Max; if (leftMinInRight) { depth = right.Max - left.Min; return true; } bool leftMaxInRight = left.Max < right.Max && left.Max > right.Min; if (leftMaxInRight) { depth = right.Min - left.Max; return true; } depth = 0f; return false; } /// /// Checks if two s are approximately equal within a specified epsilon range. /// /// The first . /// The second . /// The epsilon range. /// if the s are approximately equal; otherwise, . public static bool ApproximatelyEquals(Projection1D left, Projection1D right, float epsilon = float.Epsilon) => left.Min.ApproximatelyEquals(right.Min, epsilon) && left.Max.ApproximatelyEquals(right.Max, epsilon); /// /// Determines whether the specified object is equal to the current . /// /// The object to compare with the current . /// if the specified object is equal to the current ; otherwise, . public override bool Equals(object? obj) => obj is Projection1D projection1D && Min.Equals(projection1D.Min) && Max.Equals(projection1D.Max); /// /// Generates a hash code for the . /// /// A hash code for the . public override int GetHashCode() => System.HashCode.Combine(Min, Max); /// /// Converts the to its string representation. /// /// A string representation of the . public override string ToString() => $"{nameof(Projection1D)}({Min}, {Max})"; } /// /// Provides extension methods for the struct. /// public static class Projection1DExtensions { /// public static bool Overlaps(this Projection1D left, Projection1D right) => Projection1D.Overlaps(left, right); /// public static bool Overlaps(this Projection1D left, Projection1D right, out float depth) => Projection1D.Overlaps(left, right, out depth); /// public static bool ApproximatelyEquals(this Projection1D left, Projection1D right, float epsilon = float.Epsilon) => Projection1D.ApproximatelyEquals(left, right, epsilon); }