Simple way of fusing a few close points?

前端 未结 4 2134
南旧
南旧 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条回答
  •  -上瘾入骨i
    2021-02-06 17:21

    You could just give a radius limit and iteratively join points that are closer than that radius away. If your dataset is small enough, brute force may suffice:

    def join_pair(points, r):
        for p, q in itertools.combinations(points, 2):
            if dist(p, q) < r:
                points.remove(p)
                points.remove(q)
                points.append(((p[0]+q[0]) / 2, (p[1]+q[1]) / 2))
                return True
        return False
    
    while join_pair(points, R):
        pass
    

提交回复
热议问题