lineLineIntersect

common.lineLineIntersect()
lineLineIntersect(p1: Point3, p2: Point3, p3: Point3, p4: Point3): (None | Point3)

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

p1: Point3

First point defining the first line

p2: Point3

Second point defining the first line

p3: Point3

First point defining the second line

p4: Point3

Second point defining the second line

Returns: (None | Point3)

The closest point on line p1-p2 to line p3-p4, or null if either line is degenerate or lines are parallel

Examples

// 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