フィルターのクリア

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

140 ビュー (過去 30 日間)
Is there a function in MATLAB that calculates the shortest distance from a point to a line?

採用された回答

MathWorks Support Team
MathWorks Support Team 2009 年 6 月 27 日
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)
  1 件のコメント
Rik
Rik 2019 年 2 月 6 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Rik
Rik 2017 年 9 月 12 日
編集済み: Rik 2018 年 3 月 3 日
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 件のコメント
Rik
Rik 2018 年 3 月 3 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePoint Cloud Processing についてさらに検索

製品


リリース

R13SP1

Community Treasure Hunt

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

Start Hunting!

Translated by