namespace Syntriax.Engine.Core; /// /// Represents a line equation in the form y = mx + b. /// /// The slope of the line. /// The y-intercept of the line. /// /// Initializes a new instance of the struct with the specified slope and y-intercept. /// [System.Diagnostics.DebuggerDisplay("y = {Slope}x + {OffsetY}")] public readonly struct Line2DEquation(float slope, float offsetY) { /// /// The slope of the line equation. /// public readonly float Slope = slope; /// /// The y-intercept of the line equation. /// public readonly float OffsetY = offsetY; /// /// Resolves the y-coordinate for a given x-coordinate using the line equation. /// /// The line equation to resolve. /// The x-coordinate for which to resolve the y-coordinate. /// The y-coordinate resolved using the line equation. public static float Resolve(Line2DEquation lineEquation, float x) => lineEquation.Slope * x + lineEquation.OffsetY; // y = mx + b /// /// Checks if two line equations are approximately equal. /// /// The first line equation to compare. /// The second line equation to compare. /// The epsilon range. /// True if the line equations are approximately equal; otherwise, false. public static bool ApproximatelyEquals(Line2DEquation left, Line2DEquation right, float epsilon = float.Epsilon) => left.Slope.ApproximatelyEquals(right.Slope, epsilon) && left.OffsetY.ApproximatelyEquals(right.OffsetY, 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 Line2DEquation lineEquation && Slope.Equals(lineEquation.Slope) && OffsetY.Equals(lineEquation.OffsetY); /// /// Generates a hash code for the . /// /// A hash code for the . public override int GetHashCode() => System.HashCode.Combine(Slope, OffsetY); /// /// Converts the to its string representation. /// /// A string representation of the . public override string ToString() => $"{nameof(Line2DEquation)}({Slope}, {OffsetY})"; } /// /// Provides extension methods for the LineEquation struct. /// public static class Line2DEquationExtensions { /// public static float Resolve(this Line2DEquation lineEquation, float x) => Line2DEquation.Resolve(lineEquation, x); /// public static bool ApproximatelyEquals(this Line2DEquation left, Line2DEquation right, float epsilon = float.Epsilon) => Line2DEquation.ApproximatelyEquals(left, right, epsilon); }