我有一组定义多边形的边和顶点(不一定是凸的)。顶点和边的顺序是随机的,我想按顺时针(或逆时针)方向对这个多边形的顶点进行排序。
你知道如何实现这一点吗?
发布于 2012-10-29 08:23:23
我认为这本质上是Königsberg Bridge问题的简化版本。
如果不存在两条以上的边在单个节点上连接的情况,您可以构建一个相邻列表并“遍历”所有节点。
如果有>2条边连接在一个节点上的情况,...嗯,我想可能会有不止一个订单。只需参考Königsberg Bridge问题的解决方案。
for v,u in edges:
adjacent[v].append(u)
adjacent[u].append(v)
order=[]
start = v0 #start from an arbitrary node
def draw(now,last):
if now==start and len(order)>0:
return
order.append(now)
for i in adjacent[now]:
if i != last:
draw(i,now)
draw(start,NULL)发布于 2012-10-29 08:30:40
我假设CW/CCW方向无关紧要(保证其中之一要复杂得多)。此伪码算法对CW或CCW顶点进行排序。
marked_edge <- any edge
first <- marked_edge.start
list <- [first]
current <- marked_edge.end
while current <> first
list <- list + [current]
new_edge <- find the edge that is not the marked_edge and has the vertex current as either start or end
if new_edge.start=current then
current <- new_edge.end
else
current <- new_edge.start
endif
marked_edge <- new_edge
endwhilehttps://stackoverflow.com/questions/13114378
复制相似问题