how to find the shortest distance between two lines in R3 ?

5 views (last 30 days)
I need to find the shortest distance between two lines in R3, and I have the point (x,y,z)=(0,0,0)
I know how to plot the lines on a graph, after that I get stuck..
Thanks in advance
  1 Comment
Noella Makhlouta
Noella Makhlouta on 5 Dec 2018
L1 = {(x,y,z) ∈ R 3 | x = 10 + 3t, y = −1 + 2t, z = −1.5 − 0.01t, t ∈ R},
L2 = {(x,y,z) ∈ R 3 | x = −1 − 0.1t, y = 2t, z = −2.5 + 0.02t, t ∈ R},

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 5 Dec 2018
Edited: Bruno Luong on 5 Dec 2018
No need a big hammer, a simple formula is enough
d = abs(null([x1(:)-x0(:),y1(:)-y0(:)]')'*(x1(:)-y1(:)))
Or to avoid NULL
n = cross(x1(:)-x0(:),y1(:)-y0(:));
d = abs(n'*(x1(:)-y1(:)))/sqrt(n'*n)

More Answers (1)

Torsten
Torsten on 5 Dec 2018
% Line 1 is given as x=x0+lambda*(x1-x0)
x0 = ...;
x1 = ...;
% Line 2 is given as y=y0+eta*(y1-y0)
y0 = ...;
y1 = ...;
fun = @(x) sum(((x0+x(1)*(x1-x0))-(y0+x(2)*(y1-y0))).^2);
xstart = [0 0];
sol = fminsearch(fun,xstart);
distance = sqrt(fun(sol))

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!