lineLineIntersect
-
common.lineLineIntersect() -
Finds the closest point on the first line to the second line in 3D space.
This function computes the point on line p1-p2 that is closest to line p3-p4. For intersecting lines, this returns the intersection point. For skew lines (non-intersecting, non-parallel lines in 3D), it returns the point on the first line that minimizes the distance to the second line.
Parameters
Returns: (None |
Point3)The closest point on line p1-p2 to line p3-p4, or null if either line is degenerate or lines are parallelExamples
// Intersecting lines const p1 = new Point3(0, 0, 0); const p2 = new Point3(1, 0, 0); const p3 = new Point3(0.5, -1, 0); const p4 = new Point3(0.5, 1, 0); const intersection = lineLineIntersect(p1, p2, p3, p4); // Returns: Point3(0.5, 0, 0) - the intersection point
// Skew lines (non-intersecting in 3D) const p1 = new Point3(0, 0, 0); const p2 = new Point3(1, 0, 0); const p3 = new Point3(0, 1, 1); const p4 = new Point3(1, 1, 1); const closest = lineLineIntersect(p1, p2, p3, p4); // Returns: Point3 representing the closest point on line p1-p2 to line p3-p4
// Parallel lines const p1 = new Point3(0, 0, 0); const p2 = new Point3(1, 0, 0); const p3 = new Point3(0, 1, 0); const p4 = new Point3(1, 1, 0); const result = lineLineIntersect(p1, p2, p3, p4); // Returns: null (parallel lines have no unique closest point)
Remarks
- Algorithm based on Paul Bourke’s line-line intersection method
- Uses a small epsilon (1e-12) for numerical stability
- Returns null for degenerate cases:
- Either line has zero length (p1 == p2 or p3 == p4)
- Lines are parallel (cross product of direction vectors is zero)
- For non-parallel lines, always returns a valid Point3
- The returned point always lies on the infinite extension of line p1-p2
- For truly intersecting lines, the distance between the returned point and line p3-p4 will be approximately zero
- For skew lines, the returned point minimizes the 3D distance to line p3-p4
See Also
- http://paulbourke.net/geometry/pointlineplane/lineline.c - Original algorithm reference
distanceLineLine- For computing distance between line segments with clamping