I'm writing a program for university on python. I have to find the shortest distance between two lines in 3d given by two points (A B and C D) and find the points on both of these lines with the shortest distance between them. I'm bad at math so I can't understand how to find the points, only managed to find the formula of the minimal distance between two lines.
tried to write program, but it finds only point of intersection between lines and doesn't work correctly
#line 1
A = [1, 3, 1]
B = [0, -1, 2]
#line 2
C = [0, -2, 3]
D = [1, 0, 2]
def line_intersection(a, b, c, d):
v1 = [a_i - b_i for a_i, b_i in zip(B, A)]
v2 = [a_i - b_i for a_i, b_i in zip(D, C)]
if (-v1[0] * v2[1] + v1[1] * v2[0]) == 0:
t = ((c[1]-a[1]) * (-v2[2]) + (v2[1]) * (c[2]-a[2]))/(-v1[1] * v2[2] + v1[2] * v2[1])
else:
t = ((c[0]-a[0]) * (-v2[1]) + (v2[0]) * (c[1]-a[1]))/(-v1[0] * v2[1] + v1[1] * v2[0])
x = a[0] + t * v1[0]
y = a[1] + t * v1[1]
z = a[2] + t * v1[2]
return x, y, z