Skip to content
DeepForgeby svx
All problems
MediumLinear Algebrala-318

Project Point onto Line 3D

Problem

Project a point p onto the line through q with direction d in 3D.

The closest point is q + s*d with s = (p - q) . d / (d . d). Return the projected point as a list, or None if d is the zero vector.

Hint

The offset from q must be parallel to d, so rescale d by the signed length.

Test cases

case 1
in: [[1,1,1],[0,0,0],[1,0,0]]
expected: [1,0,0]
case 2
in: [[2,3,4],[1,1,1],[1,1,1]]
expected: [3,3,3]
case 3
in: [[1,2,3],[1,2,3],[5,0,0]]
expected: [1,2,3]
case 4
in: [[1,2,3],[0,0,0],[0,0,0]]
expected: null
case 5
in: [[0,0,5],[0,0,1],[0,0,2]]
expected: [0,0,5]
Starter codePython
def project_point_line_3d(p, q, d):
    # Your code here
    pass

5 test cases · run your code against them in the editor