namespace Syntriax.Engine.Core;
///
/// Represents a 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 .
///
public readonly float Slope = slope;
///
/// The Y intercept of the .
///
public readonly float OffsetY = offsetY;
///
/// Resolves the Y coordinate for a given X coordinate using the .
///
/// The to resolve.
/// The X coordinate for which to resolve the Y coordinate.
/// The Y coordinate resolved using the .
public static float Resolve(Line2DEquation lineEquation, float x) => lineEquation.Slope * x + lineEquation.OffsetY; // y = mx + b
///
/// Checks if two are approximately equal.
///
/// The first to compare.
/// The second to compare.
/// The epsilon range.
/// True if the 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 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);
}