How to order points anti clockwise

后端 未结 5 1642
梦毁少年i
梦毁少年i 2021-02-20 04:22

Lets take thess points.

pt={{-4.65371,0.1},{-4.68489,0.103169},{-4.78341,0.104834},{-4.83897,0.100757},
{-4.92102,0.0949725},{-4.93456,0.100181},{-4.89166,0.1226         


        
5条回答
  •  迷失自我
    2021-02-20 04:54

    Here's a python function which orders points counterclockwise. It Graham's Scan theorem. I've written it because I misunderstood a homework. It needs optimizing,though.

    def order(a):
    from math import atan2
    arctangents=[]
    arctangentsandpoints=[]
    arctangentsoriginalsandpoints=[]
    arctangentoriginals=[]
    centerx=0
    centery=0
    sortedlist=[]
    firstpoint=[]
    k=len(a)
    for i in a:
        x,y=i[0],i[1]
        centerx+=float(x)/float(k)
        centery+=float(y)/float(k)
    for i in a:
        x,y=i[0],i[1]
        arctangentsandpoints+=[[i,atan2(y-centery,x-centerx)]]
        arctangents+=[atan2(y-centery,x-centerx)]
        arctangentsoriginalsandpoints+=[[i,atan2(y,x)]]
        arctangentoriginals+=[atan2(y,x)]
    arctangents=sorted(arctangents)
    arctangentoriginals=sorted(arctangentoriginals)
    for i in arctangents:
        for c in arctangentsandpoints:
            if i==c[1]:
                sortedlist+=[c[0]]
    for i in arctangentsoriginalsandpoints:
        if arctangentoriginals[0]==i[1]:
            firstpoint=i[0]
    z=sortedlist.index(firstpoint)
    m=sortedlist[:z]
    sortedlist=sortedlist[z:]
    sortedlist.extend(m)
    return sortedlist
    

提交回复
热议问题