65 lines
2.8 KiB
C#
65 lines
2.8 KiB
C#
using Syntriax.Engine.Core;
|
|
|
|
namespace Syntriax.Engine.Physics2D.Primitives;
|
|
|
|
/// <summary>
|
|
/// Represents a line equation in the form y = mx + b.
|
|
/// </summary>
|
|
/// <param name="slope">The slope of the line.</param>
|
|
/// <param name="offsetY">The y-intercept of the line.</param>
|
|
/// <remarks>
|
|
/// Initializes a new instance of the <see cref="LineEquation"/> struct with the specified slope and y-intercept.
|
|
/// </remarks>
|
|
[System.Diagnostics.DebuggerDisplay("y = {Slope}x + {OffsetY}")]
|
|
public readonly struct LineEquation(float slope, float offsetY)
|
|
{
|
|
/// <summary>
|
|
/// The slope of the line equation.
|
|
/// </summary>
|
|
public readonly float Slope = slope;
|
|
|
|
/// <summary>
|
|
/// The y-intercept of the line equation.
|
|
/// </summary>
|
|
public readonly float OffsetY = offsetY;
|
|
|
|
/// <summary>
|
|
/// Resolves the y-coordinate for a given x-coordinate using the line equation.
|
|
/// </summary>
|
|
/// <param name="lineEquation">The line equation to resolve.</param>
|
|
/// <param name="x">The x-coordinate for which to resolve the y-coordinate.</param>
|
|
/// <returns>The y-coordinate resolved using the line equation.</returns>
|
|
public static float Resolve(LineEquation lineEquation, float x) => lineEquation.Slope * x + lineEquation.OffsetY; // y = mx + b
|
|
|
|
/// <summary>
|
|
/// Checks if two line equations are approximately equal.
|
|
/// </summary>
|
|
/// <param name="left">The first line equation to compare.</param>
|
|
/// <param name="right">The second line equation to compare.</param>
|
|
/// <returns>True if the line equations are approximately equal; otherwise, false.</returns>
|
|
public static bool ApproximatelyEquals(LineEquation left, LineEquation right)
|
|
=> left.Slope.ApproximatelyEquals(right.Slope) && left.OffsetY.ApproximatelyEquals(right.OffsetY);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Provides extension methods for the LineEquation struct.
|
|
/// </summary>
|
|
public static class LineEquationExtensions
|
|
{
|
|
/// <summary>
|
|
/// Resolves the y-coordinate for a given x-coordinate using the line equation.
|
|
/// </summary>
|
|
/// <param name="lineEquation">The line equation to resolve.</param>
|
|
/// <param name="x">The x-coordinate for which to resolve the y-coordinate.</param>
|
|
/// <returns>The y-coordinate resolved using the line equation.</returns>
|
|
public static float Resolve(this LineEquation lineEquation, float x) => LineEquation.Resolve(lineEquation, x);
|
|
|
|
/// <summary>
|
|
/// Checks if two line equations are approximately equal.
|
|
/// </summary>
|
|
/// <param name="left">The first line equation to compare.</param>
|
|
/// <param name="right">The second line equation to compare.</param>
|
|
/// <returns>True if the line equations are approximately equal; otherwise, false.</returns>
|
|
public static bool ApproximatelyEquals(this LineEquation left, LineEquation right) => LineEquation.ApproximatelyEquals(left, right);
|
|
}
|