Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbradleym committed May 30, 2024
1 parent d20b740 commit 15a9948
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
13 changes: 3 additions & 10 deletions Elements/src/Geometry/Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,23 +1224,16 @@ public Line Projected(Plane plane)
}

/// <summary>
/// Projects current line orthogonally onto another line
/// Projects current line onto another line
/// </summary>
/// <param name="line">Line to project on</param>
/// <returns>New line on a line</returns>
public Line Projected(Line line)
{
var lineDirection = line.Direction();

Vector3 ProjectPoint(Vector3 point, Vector3 lineStart)
{
var toPoint = point - lineStart;
var projectionLength = toPoint.Dot(lineDirection);
return lineStart + lineDirection.Scale(projectionLength);
}

var newLineStart = ProjectPoint(Start, line.Start);
var newLineEnd = ProjectPoint(End, line.Start);
var newLineStart = Start.Project(line.Start, lineDirection);
var newLineEnd = End.Project(line.End, lineDirection);

return new Line(newLineStart, newLineEnd);
}
Expand Down
25 changes: 24 additions & 1 deletion Elements/src/Geometry/Vector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,33 @@ public double DistanceTo(Ray ray)
{
return double.PositiveInfinity;
}
var closestPointOnRay = ray.Origin + t * ray.Direction;
var closestPointOnRay = Project(ray);
return closestPointOnRay.DistanceTo(this);
}

/// <summary>
/// Project a point onto a ray.
/// The ray is treated as being infinitely long.
/// </summary>
/// <param name="ray">The target ray.</param>
public Vector3 Project(Ray ray)
{
var toPoint = this - ray.Origin;
var projectionLength = toPoint.Dot(ray.Direction);
return ray.Origin + ray.Direction.Scale(projectionLength);
}

/// <summary>
/// Project a point onto a constructed ray.
/// The ray is treated as being infinitely long.
/// </summary>
/// <param name="origin">The origin of the line.</param>
/// <param name="direction">The direction of the line.</param>
public Vector3 Project(Vector3 origin, Vector3 direction)
{
return Project(new Ray(origin, direction));
}

internal double ProjectedParameterOn(Ray ray)
{
return ray.Direction.Dot(this - ray.Origin) / ray.Direction.Length(); // t will be [0,1]
Expand Down

0 comments on commit 15a9948

Please sign in to comment.