Simple way of fusing a few close points?

前端 未结 4 2129
南旧
南旧 2021-02-06 16:57

I have a list of points as such

points = [(-57.213878612138828, 17.916958304169601),
          (76.392039480378514, 0.060882542482108504),
          (0.124176706         


        
4条回答
  •  盖世英雄少女心
    2021-02-06 17:26

    You can have a function, which given a distance d would fuse the points which are within distance d of a given point (by taking their average):

    def dist2(p1, p2):
        return (p1[0]-p2[0])**2 + (p1[1]-p2[1])**2
    
    def fuse(points, d):
        ret = []
        d2 = d * d
        n = len(points)
        taken = [False] * n
        for i in range(n):
            if not taken[i]:
                count = 1
                point = [points[i][0], points[i][1]]
                taken[i] = True
                for j in range(i+1, n):
                    if Dist2(points[i], points[j]) < d2:
                        point[0] += points[j][0]
                        point[1] += points[j][1]
                        count+=1
                        taken[j] = True
                point[0] /= count
                point[1] /= count
                ret.append((point[0], point[1]))
        return ret
    

提交回复
热议问题