I have a convex polygon in 3D. For simplicity, let it be a square with vertices, (0,0,0),(1,1,0),(1,1,1),(0,0,1).
. I need to arrange these vertices in counter clockwise order. I found a solution here. It is suggested to determine the angle at the center of the polygon and sort them. I am not clear how is that going to work. Does anyone have a solution? I need a solution which is robust and even works when the vertices get very close.
A sample MATLAB code would be much appreciated!
Below are the steps I followed.
The 3D planar polygon can be rotated to 2D plane using the known formulas. Use the one under the section Rotation matrix from axis and angle.
Then as indicated by @Glenn, an internal points needs to be calculated to find the angles. I take that internal point as the mean
of the vertex locations.
Using the x-axis as the reference axis, the angle, on a 0
to 2pi
scale, for each vertex can be calculated using atan2
function as explained here.
The non-negative angle measured counterclockwise from vector a to vector b, in the range [0,2pi]
, if a = [x1,y1]
and b = [x2,y2]
, is given by:
angle = mod(atan2(y2-y1,x2-x1),2*pi);
Finally, sort the angles, [~,XI] = sort(angle);
.