638 views (last 30 days)

Is there a function in MATLAB that calculates the shortest distance from a point to a line?

MathWorks Support Team
on 27 Jun 2009

The ability to automatically calculate the shortest distance from a point to a line is not available in MATLAB. To work around this, see the following function:

function d = point_to_line(pt, v1, v2)

a = v1 - v2;

b = pt - v2;

d = norm(cross(a,b)) / norm(a);

In this function, pt, v1, and v2 are the three-dimensional coordinates of the point, one vertex on the line, and a second vertex on the line, respectively. The following example illustrates how this function would be called:

v1 = [0,0,0];

v2 = [3,0,0];

pt = [0,5,0];

distance = point_to_line(pt,v1,v2)

Rik
on 12 Sep 2017

Edited: Rik
on 3 Mar 2018

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));

Rik
on 3 Mar 2018

Masoud
on 23 Jul 2014

Edited: Masoud
on 23 Jul 2014

The algorithm is not working and I get the following error:

function d = point_to_line(pt, v1, v2) | Error: Function definitions are not permitted in this context.

Could you please let me know why it's happening?

Star Strider
on 23 Jul 2014

Ashish Shinde
on 15 Jun 2016

I want to plot the points between which the distance is minimum. Can that be done?

Nimit Dhulekar
on 12 Jul 2016

Hello Ashish, You can use the "pdist2" function followed by the "min" function to find the points that have the minimum distance. You can then use the "plot" function to plot these points. Here is a sample code snippet:

X = rand(100,2); Y = rand(100,2); dists = zeros(100,1);

for ii = 1 : 100

[~,dists(ii)] = min(pdist2(X(ii,:),Y));

end

Yegor Sinelnikov
on 24 Dec 2018

Edited: Image Analyst
on 24 Dec 2018

In case you need all coordinates of a perpendicular to a line, the MathWorks team answer can be improved:

function perp = find_perpendicular_to_line(pt, v1, v2)

% Compute distance

a = v1 - v2;

b = pt - v2;

d = norm(cross(a,b)) / norm(a);

% Normalize couple of vectors

ae = a / norm(a);

be = b / norm(b);

% Two cross products give direction of perpendicular

h = cross(ae,cross(ae,be));

he = h / norm(h);

% Perpendicular is its base vector times length

perp = he*d;

Image Analyst
on 24 Dec 2018

This function, should the Mathworks ever decide to implement it (and I don't know why you wouldn't want to put such a useful function into MATLAB, but it has been decades so I guess there is a reason), would be better if it also returned the coordinates of the intersection of the line with the shortest line from the point, in addition to the distance.

Yegor, your comments and variable names are not clear. Exactly where is the x coordinate of the intersection point, and where is the y coordinate? I'd call them xIntersection, and yIntersection. Is xIntersection perp(1)? And yIntersection perp(2)???? You might want to clarify. Maybe something like

function [vIntersection, shortestDistance] = find_perpendicular_to_line(pt, v1, v2)

where vIntersection is a 2 element vector [xIntersection, yIntersection].

DG
on 11 Mar 2019

Just to continue the question,

How do you find the equation of a line that it perpendicular to the given line (formed by v1, v2) and has the point pt. Basically, I want to know a point that lies on the line formed by v1,v2 which is closest to pt.

This will give me equation of the perpendicular.

Thank you!

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

Start Hunting!
## 4 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/95608-is-there-a-function-in-matlab-that-calculates-the-shortest-distance-from-a-point-to-a-line#comment_318134

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/95608-is-there-a-function-in-matlab-that-calculates-the-shortest-distance-from-a-point-to-a-line#comment_318134

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/95608-is-there-a-function-in-matlab-that-calculates-the-shortest-distance-from-a-point-to-a-line#comment_668095

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/95608-is-there-a-function-in-matlab-that-calculates-the-shortest-distance-from-a-point-to-a-line#comment_668095

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/95608-is-there-a-function-in-matlab-that-calculates-the-shortest-distance-from-a-point-to-a-line#comment_668202

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/95608-is-there-a-function-in-matlab-that-calculates-the-shortest-distance-from-a-point-to-a-line#comment_668202

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/95608-is-there-a-function-in-matlab-that-calculates-the-shortest-distance-from-a-point-to-a-line#comment_721594

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/95608-is-there-a-function-in-matlab-that-calculates-the-shortest-distance-from-a-point-to-a-line#comment_721594

Sign in to comment.