Consider the following polygon:
First, order your vertices by y-coordinate. In groups of equal y-coordinate sort the vertices by 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.
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…