using System; using System.Diagnostics.CodeAnalysis; namespace Syntriax.Engine.Core; /// /// Represents a 2D line segment defined by two endpoints. /// /// The starting point of the segment. /// The ending point of the segment. /// /// Initializes a new instance of the struct with the specified endpoints. /// [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) { /// /// The starting point of the segment. /// public readonly Vector2D From = from; /// /// The ending point of the segment. /// public readonly Vector2D To = to; /// /// The reversed segment. /// public readonly Line2D Reversed => new(To, From); /// /// The normalized direction of the segment. /// public readonly Vector2D Direction => From.FromTo(To).Normalize(); /// /// The length of the segment. /// public readonly float Length => From.FromTo(To).Length(); /// /// The squared length of the segment. /// public readonly float LengthSquared => From.FromTo(To).LengthSquared(); /// /// The equation of the defined by this segment. /// public static Line2DEquation GetLineEquation(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); } /// /// Determines whether the specified lies on the . /// public static bool Intersects(Line2D line, Vector2D point) => Line2DEquation.Resolve(GetLineEquation(line), point.X).ApproximatelyEquals(point.Y); /// /// Calculates the parameter 't' representing the point's position on the segment. /// public static float GetT(Line2D line, Vector2D point) { 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. if (!Lerp(line, t).ApproximatelyEquals(point)) return 1f - t; return t; } /// /// Checks if the segment intersects with another segment. /// 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; } /// /// Checks if the point lies within the segment. /// public static bool OnSegment(Line2D line, Vector2D point) { 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; } /// /// Determines whether two segments intersect. /// 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; } /// /// Finds the point of intersection between two segments. /// public static Vector2D IntersectionPoint(Line2D left, Line2D right) => Vector2D.Lerp(left.From, left.To, IntersectionParameterT(left, right)); /// /// Calculates the parameter 't' representing the intersection point's position on the segment. /// 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; } /// /// Linearly interpolates between the two endpoints of the segment using parameter 't'. /// public static Vector2D Lerp(Line2D line, float t) => Vector2D.Lerp(line.From, line.To, t); /// /// Calculates the closest point on the segment to the specified point. /// public static Vector2D ClosestPointTo(Line2D line, Vector2D point) { Vector2D edgeVector = line.From.FromTo(line.To); Vector2D pointVector = point - line.From; float t = (pointVector.X * edgeVector.X + pointVector.Y * edgeVector.Y) / (edgeVector.X * edgeVector.X + edgeVector.Y * edgeVector.Y); t = MathF.Max(0, MathF.Min(1, t)); float closestX = line.From.X + t * edgeVector.X; float closestY = line.From.Y + t * edgeVector.Y; return new Vector2D((float)closestX, (float)closestY); } /// /// Checks if two segments are approximately equal. /// /// The first . /// The second . /// The epsilon range. /// if the s are approximately equal; otherwise, . public static bool ApproximatelyEquals(Line2D left, Line2D right, float epsilon = float.Epsilon) => left.From.ApproximatelyEquals(right.From, epsilon) && left.To.ApproximatelyEquals(right.To, epsilon); } /// /// Provides extension methods for the struct. /// public static class Line2DExtensions { /// public static Vector2D Lerp(this Line2D line, float t) => Line2D.Lerp(line, t); /// public static Line2DEquation ToLineEquation(this Line2D line) => Line2D.GetLineEquation(line); /// public static bool Intersects(this Line2D line, Vector2D point) => Line2D.Intersects(line, point); /// public static float GetT(this Line2D line, Vector2D point) => Line2D.GetT(line, point); /// public static bool Intersects(this Line2D left, Line2D right) => Line2D.Intersects(left, right); /// public static bool Intersects(this Line2D left, Line2D right, [NotNullWhen(returnValue: true)] out Vector2D? point) => Line2D.Intersects(left, right, out point); /// public static bool ApproximatelyEquals(this Line2D left, Line2D right, float epsilon = float.Epsilon) => Line2D.ApproximatelyEquals(left, right, epsilon); }