Sorting according to clockwise point coordinates

前端 未结 7 2142
别跟我提以往
别跟我提以往 2020-12-16 18:18

Given a list in Python containing 8 x, y coordinate values (all positive) of 4 points as [x1, x2, x3, x4, y1, y2, y3, y4] ((xi, yi) are x and y coo

相关标签:
7条回答
  • 2020-12-16 18:49
    import math
    
    def centeroidpython(data):
        x, y = zip(*data)
        l = len(x)
        return sum(x) / l, sum(y) / l
    
    xy = [405952.0, 408139.0, 407978.0, 405978.0, 6754659.0, 6752257.0, 6754740.0, 6752378.0]
    xy_pairs = list(zip(xy[:int(len(xy)/2)], xy[int(len(xy)/2):]))
    
    centroid_x, centroid_y = centeroidpython(xy_pairs)
    xy_sorted = sorted(xy_pairs, key = lambda x: math.atan2((x[1]-centroid_y),(x[0]-centroid_x)))
    xy_sorted_x_first_then_y = [coord for pair in list(zip(*xy_sorted)) for coord in pair]
    
    0 讨论(0)
提交回复
热议问题