Syntriax.Engine/Engine.Physics2D/Primitives/Line.cs

210 lines
7.7 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics.CodeAnalysis;
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D.Primitives;
2024-02-01 12:32:58 +03:00
/// <summary>
/// Represents a 2D line segment defined by two endpoints.
/// </summary>
/// <remarks>
/// Initializes a new instance of the Line struct with the specified endpoints.
/// </remarks>
/// <param name="from">The starting point of the <see cref="Line"/> segment.</param>
/// <param name="to">The ending point of the <see cref="Line"/> segment.</param>
[System.Diagnostics.DebuggerDisplay("From: {From.ToString(),nq}, To: {To.ToString(),nq}, Direction: {Direction.ToString(),nq}, Length: {Length}")]
public readonly struct Line(Vector2D from, Vector2D to)
{
2024-02-01 12:32:58 +03:00
/// <summary>
/// The starting point of the <see cref="Line"/> segment.
/// </summary>
public readonly Vector2D From = from;
/// <summary>
/// The ending point of the <see cref="Line"/> segment.
/// </summary>
public readonly Vector2D To = to;
/// <summary>
/// The reversed <see cref="Line"/> segment.
/// </summary>
public readonly Line Reversed => new(To, From);
2024-02-01 12:32:58 +03:00
/// <summary>
/// The normalized direction <see cref="Vector2D"/> of the <see cref="Line"/> segment.
/// </summary>
public readonly Vector2D Direction => From.FromTo(To).Normalize();
2024-02-01 12:32:58 +03:00
/// <summary>
/// The length of the <see cref="Line"/> segment.
/// </summary>
public readonly float Length => From.FromTo(To).Length();
2024-02-01 12:32:58 +03:00
/// <summary>
/// The squared length of the <see cref="Line"/> segment.
/// </summary>
public readonly float LengthSquared => From.FromTo(To).LengthSquared();
2024-02-01 12:32:58 +03:00
/// <summary>
/// The equation of the <see cref="Line"/> defined by this <see cref="Line"/> segment.
/// </summary>
2024-01-23 19:15:09 +03:00
public static LineEquation GetLineEquation(Line line)
{
2024-01-23 19:15:09 +03:00
Vector2D slopeVector = line.From.FromTo(line.To);
float slope = slopeVector.Y / slopeVector.X;
2024-01-23 19:15:09 +03:00
float yOffset = line.From.Y - (slope * line.From.X);
2024-01-23 19:15:09 +03:00
return new LineEquation(slope, yOffset);
}
2024-02-01 12:32:58 +03:00
/// <summary>
/// Determines whether the specified <see cref="Vector2D"/> lies on the <see cref="Line"/>.
/// </summary>
2024-01-23 19:15:09 +03:00
public static bool Intersects(Line line, Vector2D point)
=> LineEquation.Resolve(GetLineEquation(line), point.X).ApproximatelyEquals(point.Y);
2024-02-01 12:32:58 +03:00
/// <summary>
/// Calculates the parameter 't' representing the point's position on the <see cref="Line"/> segment.
/// </summary>
2024-01-23 19:15:09 +03:00
public static float GetT(Line line, Vector2D point)
{
2024-01-23 19:15:09 +03:00
float fromX = MathF.Abs(line.From.X);
float toX = MathF.Abs(line.To.X);
float pointX = MathF.Abs(point.X);
float min = MathF.Min(fromX, toX);
float max = MathF.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.
2024-01-23 19:15:09 +03:00
if (!Lerp(line, t).ApproximatelyEquals(point))
return 1f - t;
return t;
}
2024-02-01 12:32:58 +03:00
/// <summary>
/// Checks if the <see cref="Line"/> segment intersects with another <see cref="Line"/> segment.
/// </summary>
public static bool Intersects(Line left, Line 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="Line"/> segment.
/// </summary>
public static bool OnSegment(Line line, Vector2D point)
{
2024-02-01 12:32:58 +03:00
if (point.X <= MathF.Max(line.From.X, line.To.X) && point.X >= MathF.Min(line.From.X, line.To.X) &&
point.Y <= MathF.Max(line.From.Y, line.To.Y) && point.Y >= MathF.Min(line.From.Y, line.To.Y))
return true;
return false;
}
2024-02-01 12:32:58 +03:00
/// <summary>
/// Determines whether two <see cref="Line"/> segments intersect.
/// </summary>
public static bool Intersects(Line left, Line 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="Line"/> segments.
/// </summary>
public static Vector2D IntersectionPoint(Line left, Line 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="Line"/> segment.
/// </summary>
2024-01-23 19:15:09 +03:00
public static float IntersectionParameterT(Line left, Line right)
{
2024-01-23 19:15:09 +03:00
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;
}
2024-02-01 12:32:58 +03:00
/// <summary>
/// Linearly interpolates between the two endpoints of the <see cref="Line"/> segment using parameter 't'.
/// </summary>
2024-01-23 19:15:09 +03:00
public static Vector2D Lerp(Line line, float t)
2024-02-01 12:32:58 +03:00
=> new(
2024-01-23 19:15:09 +03:00
line.From.X + (line.To.X - line.From.X) * t,
line.From.Y + (line.To.Y - line.From.Y) * t
);
2024-02-01 12:32:58 +03:00
/// <summary>
/// Calculates the closest point on the <see cref="Line"/> segment to the specified point.
/// </summary>
2024-01-23 19:15:09 +03:00
public static Vector2D ClosestPointTo(Line line, Vector2D point)
{
// Convert edge points to vectors
2024-01-23 19:15:09 +03:00
var edgeVector = new Vector2D(line.To.X - line.From.X, line.To.Y - line.From.Y);
var pointVector = new Vector2D(point.X - line.From.X, point.Y - line.From.Y);
// Calculate the projection of pointVector onto edgeVector
float t = (pointVector.X * edgeVector.X + pointVector.Y * edgeVector.Y) / (edgeVector.X * edgeVector.X + edgeVector.Y * edgeVector.Y);
// Clamp t to the range [0, 1] to ensure the closest point is on the edge
t = MathF.Max(0, MathF.Min(1, t));
// Calculate the closest point on the edge
2024-01-23 19:15:09 +03:00
float closestX = line.From.X + t * edgeVector.X;
float closestY = line.From.Y + t * edgeVector.Y;
return new Vector2D((float)closestX, (float)closestY);
}
2024-02-01 12:32:58 +03:00
/// <summary>
/// Checks if two <see cref="Line"/> segments are approximately equal.
/// </summary>
2024-01-23 19:15:09 +03:00
public static bool ApproximatelyEquals(Line left, Line right)
=> left.From.ApproximatelyEquals(right.From) && left.To.ApproximatelyEquals(right.To);
}
2024-02-01 12:32:58 +03:00
/// <summary>
/// Provides extension methods for the Line struct.
/// </summary>
2024-01-23 19:15:09 +03:00
public static class LineExtensions
{
2024-02-01 12:32:58 +03:00
/// <summary>
/// Checks if two <see cref="Line"/>s are approximately equal.
/// </summary>
2024-01-23 19:15:09 +03:00
public static bool ApproximatelyEquals(this Line left, Line right) => Line.ApproximatelyEquals(left, right);
}