Find a 4-point polygon enclosing scattered points
Show older comments
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
Accepted Answer
More Answers (1)
Matt J
on 8 Jul 2021
k=convhull(x,y);
xc=x(k); yc=y(k); %corners
5 Comments
Albert
on 8 Jul 2021
Scott MacKenzie
on 8 Jul 2021
@Matt J: Wow, nice. Must confess, I've never used the convhull function. However, with the x and y test data I used in my answer, convhull yields 32 "corners". That's the sizes for xc and yc. But, there should only be 4 corners. Am I missing something?
@Scott MacKenzieFloating point errors can make (x,y) points along the boundary look slightly non-colinear with the four "true" corners. Therefore, as far as convhull can see, it is a 32-sided polygon. Basically, to make this approach robust, you need to post process the results and weed out approximately colinear points. But, at least convhull will give you the boundary points in sequential order and help you discard points that are substantially interior to the polygon...
@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));
v=p.Vertices
Scott MacKenzie
on 9 Jul 2021
@Matt J Hey, that's great. Thanks for the follow-up and clarification.
Categories
Find more on Polygonal Shapes in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!