Find a 4-point polygon enclosing scattered points

12 ビュー (過去 30 日間)
Albert
Albert 2021 年 6 月 8 日
コメント済み: Scott MacKenzie 2021 年 7 月 9 日
I have a set of scattered points (x,y) as in the attached figure and I want to find the polygon defined only by 4 points that encloses those scattered points. Those 4 points will belong to the original set of (x,y) points because they are quite regularly spaced as in the figure. How could I find the vertices of the shape? thanks

採用された回答

Scott MacKenzie
Scott MacKenzie 2021 年 6 月 8 日
編集済み: Scott MacKenzie 2021 年 6 月 8 日
No doubt this can be simplified, but I think it meets your objective of finding the vertices (4) enclosing the points:
% create sample of regularly-spaced points, as per question
g = 0:0.1:1;
x = [];
y = [];
for i=1:10
x = [x i+g];
y = [y i-g];
end
ax = gca;
ax.XTick = 0:12;
ax.YTick = -2:12;
hold on;
plot(x,y,'.');
k = boundary(x',y');
[~, xMinIdx] = min(x(k));
[~, xMaxIdx] = max(x(k));
[~, yMinIdx] = min(y(k));
[~, yMaxIdx] = max(y(k));
xx = x(k);
yy = y(k);
p = polyshape([xx(xMinIdx) xx(yMinIdx) xx(xMaxIdx) xx(yMaxIdx)], ...
[yy(xMinIdx) yy(yMinIdx) yy(xMaxIdx) yy(yMaxIdx)]);
plot(p);
v = p.Vertices % output vertices (4)
Command window output:
v =
1 1
10 10
11 9
2 0
Figure window:
  1 件のコメント
Albert
Albert 2021 年 6 月 8 日
It works perfectly!! Thanks ;)

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

その他の回答 (1 件)

Matt J
Matt J 2021 年 7 月 8 日
k=convhull(x,y);
xc=x(k); yc=y(k); %corners
  5 件のコメント
Matt J
Matt J 2021 年 7 月 8 日
編集済み: Matt J 2021 年 7 月 8 日
@Scott MacKenzie similar to your solution, polyshape is a pretty good tool for weeding out the non-vertex boundary points that convhull() doesn't manage to find:
g = 0:0.1:1;
x = [];
y = [];
for i=1:10
x = [x i+g];
y = [y i-g];
end
k=convhull(x,y);
p=polyshape(x(k),y(k));
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
v=p.Vertices
v = 4×2
1 1 10 10 11 9 2 0
Scott MacKenzie
Scott MacKenzie 2021 年 7 月 9 日
@Matt J Hey, that's great. Thanks for the follow-up and clarification.

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

カテゴリ

Help Center および File ExchangeElementary Polygons についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by