python - 如何使用python中的return方法计算两点之间的距离?

标签 python return distance

我对 python 还是个新手,一直在努力掌握它的窍门。我一直在尝试学习简单的返回方法,但我似乎无法掌握它。 我一直在试图找到两点之间的距离,这就是我到目前为止所拥有的。 如果有人能帮我解决这个问题,那将非常有帮助!谢谢!

import math

def calculateDistance(x1,y1,x2,y2):
     dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
     return dist

calculateDistance(2,4,6,8)

print calculateDistance

最佳答案

为什么不用 math.hypot() 来计算距离呢?

>>> import math
>>> p1 = (3, 5)  # point 1 coordinate
>>> p2 = (5, 7)  # point 2 coordinate
>>> math.hypot(p2[0] - p1[0], p2[1] - p1[1]) # Linear distance 
2.8284271247461903

关于python - 如何使用python中的return方法计算两点之间的距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21369449/

相关文章:

java - ArrayList.isEmpty() 时如何返回值?

r - R中的距离计算优化

SQL 基于距离的平均数据

python - cv.IntRanges 中使用的 cv.Scalar (min) 和 (max) 是什么

python - 使用 python pandas 查找 'Time Delayed'

python - 硬件问题 : Is it possible to convert all the test scores without additional blocks in grading function?

java - 如何获取简单的泛型类型名称?

混合和大型数据集的 R 距离矩阵和聚类?

python - 比如说,send_message 和 send,在 @bot 事件中不起作用怎么办?不和谐.py

python - 我如何找出使用 PyImportModule 导入失败的原因?