File Exchange

image thumbnail

point to line distance

version 1.3.0.0 (2.12 KB) by Rik

Rik (view profile)

calculate the distance between a 2D or 3D line and one or more points

25 Downloads

Updated 29 Jan 2019

View License

You can input either a 2D or a 3D line and 2D or 3D points. This function is an extended version of the line below, with some input checking:
distance=norm(cross(v1-v2,pt-v2))/norm(v1-v2)

Cite As

Rik (2019). point to line distance (https://www.mathworks.com/matlabcentral/fileexchange/64396-point-to-line-distance), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (6)

Rik

Rik (view profile)

@Nicola, thanks for pointing out this issue. I now uploaded a version where this should be fixed. The function now properly accepts 2D inputs.

The input point "pt" shouldn't be checked against the case in which it's of size 2?

Rik

Rik (view profile)

@Kaleesh, I'll try to help you your question thread https://www.mathworks.com/matlabcentral/answers/425347

lemme put this way

curvexy1 = [ 0 20 ];
curvexy2 =[20 50];

curvexy = (curvexy1 & curvexy2 );

plot (curvexy,'-')
hold on

[x,y] = ginput(1);
h1 = text(x,y,'o', ...
'HorizontalAlignment','center', ...
'Color', [1 0 0], ...
'FontSize',8);

%% let curvexy1 ,curvexy2 be v1,v2 and ginput(1) be pt - I tried using your code to identify the perpendicular distance but ??

Rik

Rik (view profile)

You can find the solution here: https://gamedev.stackexchange.com/questions/72528/how-can-i-project-a-3d-point-onto-a-3d-line

A non-vectorized solution in the terms of the input to this function:

ap = pt-v1;ab = v2-v1;
result = v1 + dot(ap,ab)/dot(ab,ab) * ab;

Hi! Thanks, works great. I was wondering: how do I find the POINT of intersection between the original line and the projection of the point to this line? Thank you!

Updates

1.3.0.0

fixed issue with 2D input

1.2.0.0

added compatibility for R13 (MATLAB 6.5)

1.1.0.0

uploaded incorrect version, so now a bug in the orientation is fixed

MATLAB Release Compatibility
Created with R2018b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor