perf: simplified Line2D.ClosestPointTo method
This commit is contained in:
parent
f16a7e55c9
commit
9066e11c12
@ -167,17 +167,14 @@ public readonly struct Line2D(Vector2D from, Vector2D to)
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static Vector2D ClosestPointTo(Line2D line, Vector2D point)
|
public static Vector2D ClosestPointTo(Line2D line, Vector2D point)
|
||||||
{
|
{
|
||||||
Vector2D edgeVector = line.From.FromTo(line.To);
|
Vector2D lineRelativeVector = 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);
|
Vector2D lineDirection = lineRelativeVector.Normalized;
|
||||||
|
Vector2D pointVector = line.From.FromTo(point);
|
||||||
|
|
||||||
t = Math.Max(0, Math.Min(1, t));
|
float dot = lineDirection.Dot(pointVector).Clamp(0f, lineRelativeVector.Magnitude);
|
||||||
|
|
||||||
float closestX = line.From.X + t * edgeVector.X;
|
return lineDirection * dot;
|
||||||
float closestY = line.From.Y + t * edgeVector.Y;
|
|
||||||
|
|
||||||
return new Vector2D((float)closestX, (float)closestY);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -214,6 +211,9 @@ public static class Line2DExtensions
|
|||||||
/// <inheritdoc cref="Line2D.Intersects(Line2D, Line2D, out Vector2D?)" />
|
/// <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);
|
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)" />
|
/// <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);
|
public static bool ApproximatelyEquals(this Line2D left, Line2D right, float epsilon = float.Epsilon) => Line2D.ApproximatelyEquals(left, right, epsilon);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user