All problems
Distance Between Skew Lines
Problem
Compute the distance between two lines in 3D: L1 = p1 + t*d1 and L2 = p2 + s*d2.
Use the common perpendicular direction n = d1 x d2:
dist = |(p2 - p1) . n| / ||n||
Return None if n is (nearly) zero, meaning the directions are parallel.
Hint
The shortest segment joining two skew lines is parallel to their cross product.
Test cases
case 1
in: [[0,0,0],[1,0,0],[0,1,0],[0,0,1]]
expected: 1
case 2
in: [[0,0,0],[1,0,0],[0,1,0],[2,0,0]]
expected: null
case 3
in: [[1,0,0],[0,1,0],[0,1,1],[1,0,0]]
expected: 1
case 4
in: [[0,0,0],[1,0,0],[0,0,0],[0,1,0]]
expected: 0
case 5
in: [[1,1,1],[1,1,0],[0,0,2],[0,1,1]]
expected: 0.5773502691896258
Starter codePython
def distance_skew_lines(p1, d1, p2, d2):
# Your code here
pass5 test cases · run your code against them in the editor