Is there a function in MATLAB that calculates the shortest distance from a point to a line?
Mostrar comentarios más antiguos
Is there a function in MATLAB that calculates the shortest distance from a point to a line?
Respuesta aceptada
Más respuestas (1)
You can modify the staff answer with the code below to make it support a point cloud. I added input checks to this and uploaded it to the FEX.
function d = point_to_line(pt, v1, v2)
% pt should be nx3
% v1 and v2 are vertices on the line (each 1x3)
% d is a nx1 vector with the orthogonal distances
v1 = repmat(v1,size(pt,1),1);
v2 = repmat(v2,size(pt,1),1);
a = v1 - v2;
b = pt - v2;
d = sqrt(sum(cross(a,b,2).^2,2)) ./ sqrt(sum(a.^2,2));
1 comentario
Rik
el 3 de Mzo. de 2018
Sure. What is the exact code you used? I also uploaded this (with some input checking) to the File Exchange, so that should give you an understandable error.
Categorías
Más información sobre Networks en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!