89 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			89 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System;
 | 
						|
 | 
						|
namespace Engine.Core;
 | 
						|
 | 
						|
/// <summary>
 | 
						|
/// Represents a <see cref="Line2DEquation"/> 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="Line2DEquation"/> struct with the specified slope and Y intercept.
 | 
						|
/// </remarks>
 | 
						|
[System.Diagnostics.DebuggerDisplay("y = {Slope}x + {OffsetY}")]
 | 
						|
public readonly struct Line2DEquation(float slope, float offsetY) : IEquatable<Line2DEquation>
 | 
						|
{
 | 
						|
    /// <summary>
 | 
						|
    /// The slope of the <see cref="Line2DEquation"/>.
 | 
						|
    /// </summary>
 | 
						|
    public readonly float Slope = slope;
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// The Y intercept of the <see cref="Line2DEquation"/>.
 | 
						|
    /// </summary>
 | 
						|
    public readonly float OffsetY = offsetY;
 | 
						|
 | 
						|
    public static bool operator ==(Line2DEquation left, Line2DEquation right) => left.Slope == right.Slope && left.OffsetY == right.OffsetY;
 | 
						|
    public static bool operator !=(Line2DEquation left, Line2DEquation right) => left.Slope != right.Slope || left.OffsetY != right.OffsetY;
 | 
						|
 | 
						|
    public static implicit operator Line2DEquation(Line2D line)
 | 
						|
    {
 | 
						|
        Vector2D slopeVector = line.From.FromTo(line.To);
 | 
						|
        float slope = slopeVector.Y / slopeVector.X;
 | 
						|
 | 
						|
        float yOffset = line.From.Y - (slope * line.From.X);
 | 
						|
 | 
						|
        return new Line2DEquation(slope, yOffset);
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Resolves the Y coordinate for a given X coordinate using the <see cref="Line2DEquation"/>.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="lineEquation">The <see cref="Line2DEquation"/> to resolve.</param>
 | 
						|
    /// <param name="x">The X coordinate for which to resolve the Y coordinate.</param>
 | 
						|
    /// <returns>The Y coordinate resolved using the <see cref="Line2DEquation"/>.</returns>
 | 
						|
    public static float Resolve(Line2DEquation lineEquation, float x) => lineEquation.Slope * x + lineEquation.OffsetY; // y = mx + b
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Checks if two <see cref="Line2DEquation"/> are approximately equal.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="left">The first <see cref="Line2DEquation"/> to compare.</param>
 | 
						|
    /// <param name="right">The second <see cref="Line2DEquation"/> to compare.</param>
 | 
						|
    /// <param name="epsilon">The epsilon range.</param>
 | 
						|
    /// <returns>True if the <see cref="Line2DEquation"/> are approximately equal; otherwise, false.</returns>
 | 
						|
    public static bool ApproximatelyEquals(Line2DEquation left, Line2DEquation right, float epsilon = float.Epsilon)
 | 
						|
        => left.Slope.ApproximatelyEquals(right.Slope, epsilon) && left.OffsetY.ApproximatelyEquals(right.OffsetY, epsilon);
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Determines whether the specified object is equal to the current <see cref="Line2DEquation"/>.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="obj">The object to compare with the current <see cref="Line2DEquation"/>.</param>
 | 
						|
    /// <returns><see cref="true"/> if the specified object is equal to the current <see cref="Line2DEquation"/>; otherwise, <see cref="false"/>.</returns>
 | 
						|
    public override bool Equals(object? obj) => obj is Line2DEquation lineEquation && this == lineEquation;
 | 
						|
    public bool Equals(Line2DEquation other) => this == other;
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Generates a hash code for the <see cref="Line2DEquation"/>.
 | 
						|
    /// </summary>
 | 
						|
    /// <returns>A hash code for the <see cref="Line2DEquation"/>.</returns>
 | 
						|
    public override int GetHashCode() => System.HashCode.Combine(Slope, OffsetY);
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Converts the <see cref="Line2DEquation"/> to its string representation.
 | 
						|
    /// </summary>
 | 
						|
    /// <returns>A string representation of the <see cref="Line2DEquation"/>.</returns>
 | 
						|
    public override string ToString() => $"{nameof(Line2DEquation)}({Slope}, {OffsetY})";
 | 
						|
}
 | 
						|
 | 
						|
/// <summary>
 | 
						|
/// Provides extension methods for the <see cref="Line2DEquation"/> struct.
 | 
						|
/// </summary>
 | 
						|
public static class Line2DEquationExtensions
 | 
						|
{
 | 
						|
    /// <inheritdoc cref="Line2DEquation.Resolve(Line2DEquation, float)" />
 | 
						|
    public static float Resolve(this Line2DEquation lineEquation, float x) => Line2DEquation.Resolve(lineEquation, x);
 | 
						|
 | 
						|
    /// <inheritdoc cref="Line2DEquation.ApproximatelyEquals(Line2DEquation, Line2DEquation, float)" />
 | 
						|
    public static bool ApproximatelyEquals(this Line2DEquation left, Line2DEquation right, float epsilon = float.Epsilon) => Line2DEquation.ApproximatelyEquals(left, right, epsilon);
 | 
						|
}
 |