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

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)

Show 1 older comment
MITgnome on 5 Feb 2019
I am not sure this works, if it is supposed to be the shortest distance from the point to the line segment.
When I enter
v1 = [100,200,0];
v2 = [200,600,0];
pt = [50,100,0];
distance = point_to_line(pt,v1,v2)
I get a distance of 24.2536, and it should be 100*sqrt(2)
Rik on 6 Feb 2019
There is a difference between a line and a line segment. What you need to do is calculate the distance to the end points as well, and only use the distance to the line if the max of the end point distances is smaller than the length of the line segment.
Christopher Rock on 5 Jul 2019
Or rather, make sure the sum of squares of the endpoint distances, minus twice the square of the line distance, is less than the square of the line segment length.

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

MAYUR PATIL on 3 Mar 2018
Dear sir, this code is not working, can u help me ?
Rik on 3 Mar 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.

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?

#### 1 Comment

Star Strider on 23 Jul 2014
You have to save it as a separate function file point_to_line.m, or use the three lines of its code in your script without referring to them as a function.

Ashish Shinde on 15 Jun 2016
I want to plot the points between which the distance is minimum. Can that be done?

#### 1 Comment

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!

R13SP1

### Community Treasure Hunt

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

Start Hunting!