Sort Anticlockwise the points of rectilinear polygon

后端 未结 1 1523
再見小時候
再見小時候 2020-12-18 17:19

I am given a rectilinear polygon whose coordinates are given.
Please suggest to me a solution to sort anticlockwise, such that a closed line can be formed.



        
相关标签:
1条回答
  • 2020-12-18 18:10

    Consider the following polygon:

    Polygon

    First, order your vertices by y-coordinate. In groups of equal y-coordinate sort the vertices by x-coordinate:

    Sorted by y and x-coordinate

    There will always be an even number of vertices in each group if there are no degenerate vertices. Edges will always alternate. So there is an edge between 0-1, no edge between 1-2, edge between 2-3, no edge, edge etc.

    Add edges

    Store the associated edges for each vertex. E.g. in a map or in an appropriate structure.

    Do the same for vertical edges (first sort by x-coordinate, in the groups sort by y-coordinate).

    Then you have all edges of the polygon. Each vertex should now have 2 associated edges. Pick one vertex and go from edge to edge. This will give you the polyline. If you find that the line is in clockwise direction, just revert the order and you'll get a ccw polyline.

    0 讨论(0)
提交回复
热议问题