Syntriax.Engine/Engine.Physics2D/Primitives/LineEquation.cs

65 lines
2.8 KiB
C#
Raw Permalink Normal View History

2024-01-24 12:42:18 +03:00
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D.Primitives;
2024-02-01 12:32:58 +03:00
/// <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}")]
2024-02-01 12:32:58 +03:00
public readonly struct LineEquation(float slope, float offsetY)
{
2024-02-01 12:32:58 +03:00
/// <summary>
/// The slope of the line equation.
/// </summary>
public readonly float Slope = slope;
2024-02-01 12:32:58 +03:00
/// <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>
2024-01-23 19:16:31 +03:00
public static float Resolve(LineEquation lineEquation, float x) => lineEquation.Slope * x + lineEquation.OffsetY; // y = mx + b
2024-02-01 12:32:58 +03:00
/// <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>
2024-01-23 19:16:31 +03:00
public static bool ApproximatelyEquals(LineEquation left, LineEquation right)
=> left.Slope.ApproximatelyEquals(right.Slope) && left.OffsetY.ApproximatelyEquals(right.OffsetY);
}
2024-02-01 12:32:58 +03:00
/// <summary>
/// Provides extension methods for the LineEquation struct.
/// </summary>
2024-01-23 19:16:31 +03:00
public static class LineEquationExtensions
{
2024-02-01 12:32:58 +03:00
/// <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>
2024-01-23 19:16:31 +03:00
public static float Resolve(this LineEquation lineEquation, float x) => LineEquation.Resolve(lineEquation, x);
2024-02-01 12:32:58 +03:00
/// <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>
2024-01-23 19:16:31 +03:00
public static bool ApproximatelyEquals(this LineEquation left, LineEquation right) => LineEquation.ApproximatelyEquals(left, right);
}