Check if a set of coordinates are on a line

12 ビュー (過去 30 日間)
Sebastian Ursarescu
Sebastian Ursarescu 2019 年 9 月 18 日
I have a set of coordinates, for example x = [0 0 0 0] y = [1 0.9 1.1 1] or x = [1 2 3 4] y = [1 4 6 7], and I want to know if they are almost on a line. I can say the first exemple does and the second doesn't.
I tried with polyarea checking if the area is small enough but it's hard to define the crossing area value regarding the size of the polygon and, moreover, it gives the wrong answer if the polygon crosses itself, like in the first example.
Thank you in advice.
  1 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 9 月 18 日
@Guillaume sir, sorry, I missed it, I misundestood the question and considering on image (Horizantal or Vertical line)
Always Thanks

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

採用された回答

Bruno Luong
Bruno Luong 2019 年 9 月 18 日
編集済み: Bruno Luong 2019 年 9 月 18 日
xy=[x(:)';y(:)'];
criteriatol = 0.01; % adjust to your need, smaller means stricter line test
s = svd(xy-mean(xy,2));
isline = max(s)*criteriatol>min(s)
Work isotropically (orientation independent, but also scaling, shifting independent), even with vertical line.
  1 件のコメント
Sebastian Ursarescu
Sebastian Ursarescu 2019 年 9 月 18 日
Seems to work pretty good

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

その他の回答 (1 件)

Guillaume
Guillaume 2019 年 9 月 18 日
Probably, the best way is to use polyfit (which does a least square fit) and look at the norm of the residuals. If it's small enough your points (tolerance is up to you) are on a line.
[~, pfit] = polyfit(x, y, 1); %least square fit of a line
if pfit.normr <= arbitrary_value
disp('is a line');
else
disp('not a line');
end
You may need to special case when all the x are (almost) all equal (vertical line) if that's an actual possibility.
  2 件のコメント
Sebastian Ursarescu
Sebastian Ursarescu 2019 年 9 月 18 日
The problem is actually when I have a vertical line. I already tried using functions but they don't cover the case of vertical line
Guillaume
Guillaume 2019 年 9 月 18 日
Then use Bruno's answer which is a lot better than mine anyway.

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

カテゴリ

Help Center および File ExchangeComputational Geometry についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by