Detect if a set of points in an array that are the vertices of a complex polygon were defined in a clockwise or counterclockwise order?

前端 未结 5 1006
说谎
说谎 2021-02-07 05:57

EDIT: I updated the program with the answer and it works great!

I am making a program (feel free to try it out) that lets users draw polygons which it then triangulates.

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-07 06:33

    A general idea would be to take a look at the convex hull of your polygone and guess the orientation from there. However, I think that you do not need to build the whole hull to find the orientation, but just one segment belonging to it.

    So:

    • Find two points of your polygones so that all the other points are on one side of this line.
    • If all the points are on the left (just check one of the points), it's counterclockwise. If they are on the right, it's clockwise.

    Example:

    On the top figure: 4-5 let the figure on the right, 5-11 let the figure on the right, ... On the bottom figure: 6-7 let the figure on the left, 7-14 let the figure on the left, ...

    Warning: While "walking" on your polygon, do not restart the numeration, otherwise it will be wrong. On the top figure, 4-(n-1) let the figure on the left!

提交回复
热议问题