Finding face vertices of a polygon

26 views (last 30 days)
consider the following 5 by 2 matrix that contains the indices of the nodes of a polygon:
N = [...
5 12
5 11
6 8
12 8
11 6];
each row in N contains the node indices of the polygon's edges, but they are not ordered. For example, the first edge of the polygon connects the nodes number 5 and 12 and the 4th edge connects nodes number 12 and 8.
I am looking for the closed loop in this matrix without any further considerations,i.e. the starting point is not important nor is the direction of the path (clockwise or counterclockwise).
For N, a good possible answer is:
p = [5 12 8 6 11]
Also any cyclic permutation of p and its fliped version are acceptable.
My actual problem involves lots of these polygons, but non of them has more than 10 edges.
I have tried for loops for each polygon, but it turns out to be time consuming. Is there any better way to do so, timewisely? what would be the best approach to this problem?

Accepted Answer

Bruno Luong
Bruno Luong on 25 Nov 2021
If you have R2021a or later, there is a new method allcycles that can achieve the goal
N = [...
5 12
5 11
6 8
12 8
11 6];
G = graph(N(:,1),N(:,2));
G.allcycles
ans = 1×1 cell array
{[5 11 6 8 12]}

More Answers (2)

Matt J
Matt J on 4 Dec 2020
Edited: Matt J on 4 Dec 2020
My spatialgraph2D class might be applicable, assuming the polygon edges are non-intersecting:
In particular the polyshape method will return the index list of all polygon tiles in the graph, in counter-clockwise order.
[~,p]=polyshape(obj)
  5 Comments
Oskar Cheong
Oskar Cheong on 24 Nov 2021
Also does it work for periodic boundary conditions? :)
Matt J
Matt J on 25 Nov 2021
@Oskar Cheong I'm not sure what periodic boundary conditions means, but I would guess that it does not.

Sign in to comment.


Matt J
Matt J on 4 Dec 2020
Edited: Matt J on 4 Dec 2020
You cannot do what you propose with just a list of vertex indices. You need the x,y coordinates of the vertices themselves. Given that, convhull will readily provide you the index list in sorted order.
p = convhull(x,y)
  3 Comments
saeed oveisi
saeed oveisi on 4 Dec 2020
thanks for your answer
actually I have that already. but thats not my concern. I only want to reorder the nodes and find the closed path that is enforced by the list. I am not looking for the convex hull.
Matt J
Matt J on 4 Dec 2020
I only want to reorder the nodes and find the closed path that is enforced by the list. I am not looking for the convex hull.
Then your polygons are not convex? If they are convex, then it is irrelevant that you are not looking for the convex hull. The point is that convhull() will reorder the vertices for you in a cyclic ordering.

Sign in to comment.

Categories

Find more on Bounding Regions 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!