239 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			239 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System;
 | 
						|
using System.Diagnostics.CodeAnalysis;
 | 
						|
 | 
						|
namespace Engine.Core;
 | 
						|
 | 
						|
/// <summary>
 | 
						|
/// Represents a 2D line segment defined by two endpoints.
 | 
						|
/// </summary>
 | 
						|
/// <param name="from">The starting point of the <see cref="Line2D"/> segment.</param>
 | 
						|
/// <param name="to">The ending point of the <see cref="Line2D"/> segment.</param>
 | 
						|
/// <remarks>
 | 
						|
/// Initializes a new instance of the <see cref="Line2D"/> struct with the specified endpoints.
 | 
						|
/// </remarks>
 | 
						|
[System.Diagnostics.DebuggerDisplay("From: {From.ToString(),nq}, To: {To.ToString(),nq}, Direction: {Direction.ToString(),nq}, Length: {Length}")]
 | 
						|
public readonly struct Line2D(Vector2D from, Vector2D to) : IEquatable<Line2D>
 | 
						|
{
 | 
						|
    /// <summary>
 | 
						|
    /// The starting point of the <see cref="Line2D"/> segment.
 | 
						|
    /// </summary>
 | 
						|
    public readonly Vector2D From = from;
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// The ending point of the <see cref="Line2D"/> segment.
 | 
						|
    /// </summary>
 | 
						|
    public readonly Vector2D To = to;
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// The reversed <see cref="Line2D"/> segment.
 | 
						|
    /// </summary>
 | 
						|
    public readonly Line2D Reversed => new(To, From);
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// The normalized direction <see cref="Vector2D"/> of the <see cref="Line2D"/> segment.
 | 
						|
    /// </summary>
 | 
						|
    public readonly Vector2D Direction => From.FromTo(To).Normalize();
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// The length of the <see cref="Line2D"/> segment.
 | 
						|
    /// </summary>
 | 
						|
    public readonly float Length => From.FromTo(To).Length();
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// The squared length of the <see cref="Line2D"/> segment.
 | 
						|
    /// </summary>
 | 
						|
    public readonly float LengthSquared => From.FromTo(To).LengthSquared();
 | 
						|
 | 
						|
    public static bool operator ==(Line2D left, Line2D right) => left.From == right.From && left.To == right.To;
 | 
						|
    public static bool operator !=(Line2D left, Line2D right) => left.From != right.From || left.To != right.To;
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// The equation of the <see cref="Line2D"/> defined by this <see cref="Line2D"/> segment.
 | 
						|
    /// </summary>
 | 
						|
    public static Line2DEquation GetLineEquation(Line2D line) => line;
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Determines whether the specified <see cref="Vector2D"/> lies on the <see cref="Line2D"/>.
 | 
						|
    /// </summary>
 | 
						|
    public static bool Intersects(Line2D line, Vector2D point)
 | 
						|
        => Line2DEquation.Resolve(GetLineEquation(line), point.X).ApproximatelyEquals(point.Y);
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Calculates the parameter 't' representing the point's position on the <see cref="Line2D"/> segment.
 | 
						|
    /// </summary>
 | 
						|
    public static float GetT(Line2D line, Vector2D point)
 | 
						|
    {
 | 
						|
        float fromX = Math.Abs(line.From.X);
 | 
						|
        float toX = Math.Abs(line.To.X);
 | 
						|
        float pointX = Math.Abs(point.X);
 | 
						|
 | 
						|
        float min = Math.Min(fromX, toX);
 | 
						|
        float max = Math.Max(fromX, toX) - min;
 | 
						|
 | 
						|
        pointX -= min;
 | 
						|
 | 
						|
        float t = pointX / max;
 | 
						|
 | 
						|
        // FIXME
 | 
						|
        // I don't even know, apparently whatever I wrote up there doesn't take into account of the direction of the line
 | 
						|
        // Which... I can see how, but I am also not sure how I can make it take into account. Or actually I'm for some reason
 | 
						|
        // too unmotivated to find a solution. Future me, find a better way if possible, please.
 | 
						|
        if (!Lerp(line, t).ApproximatelyEquals(point))
 | 
						|
            return 1f - t;
 | 
						|
        return t;
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Checks if the <see cref="Line2D"/> segment intersects with another <see cref="Line2D"/> segment.
 | 
						|
    /// </summary>
 | 
						|
    public static bool Intersects(Line2D left, Line2D right)
 | 
						|
    {
 | 
						|
        int o1 = Vector2D.Orientation(left.From, left.To, right.From);
 | 
						|
        int o2 = Vector2D.Orientation(left.From, left.To, right.To);
 | 
						|
        int o3 = Vector2D.Orientation(right.From, right.To, left.From);
 | 
						|
        int o4 = Vector2D.Orientation(right.From, right.To, left.To);
 | 
						|
 | 
						|
        if (o1 != o2 && o3 != o4)
 | 
						|
            return true;
 | 
						|
 | 
						|
        if (o1 == 0 && OnSegment(left, right.From)) return true;
 | 
						|
        if (o2 == 0 && OnSegment(left, right.To)) return true;
 | 
						|
        if (o3 == 0 && OnSegment(right, left.From)) return true;
 | 
						|
        if (o4 == 0 && OnSegment(right, left.To)) return true;
 | 
						|
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Checks if the point lies within the <see cref="Line2D"/> segment.
 | 
						|
    /// </summary>
 | 
						|
    public static bool OnSegment(Line2D line, Vector2D point)
 | 
						|
    {
 | 
						|
        if (point.X <= Math.Max(line.From.X, line.To.X) && point.X >= Math.Min(line.From.X, line.To.X) &&
 | 
						|
            point.Y <= Math.Max(line.From.Y, line.To.Y) && point.Y >= Math.Min(line.From.Y, line.To.Y))
 | 
						|
            return true;
 | 
						|
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Determines whether two <see cref="Line2D"/> segments intersect.
 | 
						|
    /// </summary>
 | 
						|
    public static bool Intersects(Line2D left, Line2D right, [NotNullWhen(returnValue: true)] out Vector2D? point)
 | 
						|
    {
 | 
						|
        point = null;
 | 
						|
 | 
						|
        bool result = Intersects(left, right);
 | 
						|
 | 
						|
        if (result)
 | 
						|
            point = IntersectionPoint(left, right);
 | 
						|
 | 
						|
        return result;
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Finds the point of intersection between two <see cref="Line2D"/> segments.
 | 
						|
    /// </summary>
 | 
						|
    public static Vector2D IntersectionPoint(Line2D left, Line2D right)
 | 
						|
        => Vector2D.Lerp(left.From, left.To, IntersectionParameterT(left, right));
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Calculates the parameter 't' representing the intersection point's position on the <see cref="Line2D"/> segment.
 | 
						|
    /// </summary>
 | 
						|
    public static float IntersectionParameterT(Line2D left, Line2D right)
 | 
						|
    {
 | 
						|
        float numerator = (left.From.X - right.From.X) * (right.From.Y - right.To.Y) - (left.From.Y - right.From.Y) * (right.From.X - right.To.X);
 | 
						|
        float denominator = (left.From.X - left.To.X) * (right.From.Y - right.To.Y) - (left.From.Y - left.To.Y) * (right.From.X - right.To.X);
 | 
						|
 | 
						|
        // Lines are parallel
 | 
						|
        if (denominator == 0)
 | 
						|
            return float.NaN;
 | 
						|
 | 
						|
        return numerator / denominator;
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Linearly interpolates between the two endpoints of the <see cref="Line2D"/> segment using parameter 't'.
 | 
						|
    /// </summary>
 | 
						|
    public static Vector2D Lerp(Line2D line, float t)
 | 
						|
        => Vector2D.Lerp(line.From, line.To, t);
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Calculates the closest point on the <see cref="Line2D"/> segment to the specified point.
 | 
						|
    /// </summary>
 | 
						|
    public static Vector2D ClosestPointTo(Line2D line, Vector2D point)
 | 
						|
    {
 | 
						|
        Vector2D lineRelativeVector = line.From.FromTo(line.To);
 | 
						|
 | 
						|
        Vector2D lineDirection = lineRelativeVector.Normalized;
 | 
						|
        Vector2D pointVector = line.From.FromTo(point);
 | 
						|
 | 
						|
        float dot = lineDirection.Dot(pointVector).Clamp(0f, lineRelativeVector.Magnitude);
 | 
						|
 | 
						|
        return lineDirection * dot;
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Checks if two <see cref="Line2D"/> segments are approximately equal.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="left">The first <see cref="Line2D"/>.</param>
 | 
						|
    /// <param name="right">The second <see cref="Line2D"/>.</param>
 | 
						|
    /// <param name="epsilon">The epsilon range.</param>
 | 
						|
    /// <returns><see cref="true"/> if the <see cref="Line2D"/>s are approximately equal; otherwise, <see cref="false"/>.</returns>
 | 
						|
    public static bool ApproximatelyEquals(Line2D left, Line2D right, float epsilon = float.Epsilon)
 | 
						|
        => left.From.ApproximatelyEquals(right.From, epsilon) && left.To.ApproximatelyEquals(right.To, epsilon);
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Determines whether the specified object is equal to the current <see cref="Line2D"/>.
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="obj">The object to compare with the current <see cref="Line2D"/>.</param>
 | 
						|
    /// <returns><see cref="true"/> if the specified object is equal to the current <see cref="Line2D"/>; otherwise, <see cref="false"/>.</returns>
 | 
						|
    public override bool Equals(object? obj) => obj is Line2D line2D && this == line2D;
 | 
						|
    public bool Equals(Line2D other) => this == other;
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Generates a hash code for the <see cref="Line2D"/>.
 | 
						|
    /// </summary>
 | 
						|
    /// <returns>A hash code for the <see cref="Line2D"/>.</returns>
 | 
						|
    public override int GetHashCode() => System.HashCode.Combine(From, To);
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Converts the <see cref="Line2D"/> to its string representation.
 | 
						|
    /// </summary>
 | 
						|
    /// <returns>A string representation of the <see cref="Line2D"/>.</returns>
 | 
						|
    public override string ToString() => $"{nameof(Line2D)}({From}, {To})";
 | 
						|
}
 | 
						|
 | 
						|
/// <summary>
 | 
						|
/// Provides extension methods for the <see cref="Line2D"/> struct.
 | 
						|
/// </summary>
 | 
						|
public static class Line2DExtensions
 | 
						|
{
 | 
						|
    /// <inheritdoc cref="Line2D.Lerp(Line2D, float)" />
 | 
						|
    public static Vector2D Lerp(this Line2D line, float t) => Line2D.Lerp(line, t);
 | 
						|
 | 
						|
    /// <inheritdoc cref="Line2D.GetLineEquation(Line2D)" />
 | 
						|
    public static Line2DEquation ToLineEquation(this Line2D line) => Line2D.GetLineEquation(line);
 | 
						|
 | 
						|
    /// <inheritdoc cref="Line2D.Intersects(Line2D, Vector2D)" />
 | 
						|
    public static bool Intersects(this Line2D line, Vector2D point) => Line2D.Intersects(line, point);
 | 
						|
 | 
						|
    /// <inheritdoc cref="Line2D.IntersectionPoint(Line2D, Line2D)" />
 | 
						|
    public static Vector2D IntersectionPoint(this Line2D left, Line2D right) => Line2D.IntersectionPoint(left, right);
 | 
						|
 | 
						|
    /// <inheritdoc cref="Line2D.GetT(Line2D, Vector2D)" />
 | 
						|
    public static float GetT(this Line2D line, Vector2D point) => Line2D.GetT(line, point);
 | 
						|
 | 
						|
    /// <inheritdoc cref="Line2D.Intersects(Line2D, Line2D)" />
 | 
						|
    public static bool Intersects(this Line2D left, Line2D right) => Line2D.Intersects(left, right);
 | 
						|
 | 
						|
    /// <inheritdoc cref="Line2D.Intersects(Line2D, Line2D, out Vector2D?)" />
 | 
						|
    public static bool Intersects(this Line2D left, Line2D right, [NotNullWhen(returnValue: true)] out Vector2D? point) => Line2D.Intersects(left, right, out point);
 | 
						|
 | 
						|
    /// <inheritdoc cref="Line2D.ClosestPointTo(Line2D, Vector2D)" />
 | 
						|
    public static Vector2D ClosestPointTo(this Line2D line, Vector2D point) => Line2D.ClosestPointTo(line, point);
 | 
						|
 | 
						|
    /// <inheritdoc cref="Line2D.ApproximatelyEquals(Line2D, Line2D, float)" />
 | 
						|
    public static bool ApproximatelyEquals(this Line2D left, Line2D right, float epsilon = float.Epsilon) => Line2D.ApproximatelyEquals(left, right, epsilon);
 | 
						|
}
 |