Distance formula between two points in a list

前端 未结 7 1751
刺人心
刺人心 2021-01-31 19:39

I need to take a list I have created and find the closest two points and print them out. How can I go about comparing each point in the list?

There isn\'t any need to pl

7条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-31 20:00

    Many of the above questions suggest finding square root using math.sqrt which is slow as well as not a good approach to find square root. In spite of using such approach just recall the basic concepts from school: think of taking the square root of any positive number, x. The square root is then written as a power of one-half: x½. Thus, a fractional exponent indicates that some root is to be taken.

    so rather than using math.sqrt((p0[0] - p1[0])**2 + (p0[1] - p1[1])**2)

    Use

    def distance(a,b):
      euclidean_distance = ((b[0]-a[0])**2 + (a[1]-a[1])**2)**0.5
      return(euclidean_distance)
    

    Hope it helps

提交回复
热议问题