python如何计算两个点距离
时间: 2023-07-05 07:36:06 浏览: 48
Python中可以使用math库中的distance函数来计算两个点的距离,具体实现方式如下所示:
```python
import math
# 定义两个点的坐标
x1, y1 = 1, 2
x2, y2 = 3, 4
# 计算两个点的距离
distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
# 输出计算结果
print("两个点的距离为:", distance)
```
输出结果为:
```
两个点的距离为: 2.8284271247461903
```
其中,math.sqrt()函数用于计算平方根,**运算符用于计算乘方。
相关问题
python计算两个点的最小距离
可以使用数学公式计算两个点之间的距离,公式为:d = √((x2-x1)² + (y2-y1)²),其中(x1,y1)和(x2,y2)分别为两个点的坐标。在Python中可以使用math库中的sqrt函数来计算平方根,具体代码如下:
import math
def distance(x1, y1, x2, y2):
return math.sqrt((x2-x1)**2 + (y2-y1)**2)
# 示例
print(distance(1, 2, 4, 6)) # 输出 5.
python 计算两点间距离
可以使用以下公式来计算两点之间的距离:
$$distance = \sqrt{(x2-x1)^2 + (y2-y1)^2}$$
其中,$(x1, y1)$ 和 $(x2, y2)$ 分别是两个点的坐标。
在 Python 中,你可以使用如下的代码来计算两点之间的距离:
```python
import math
def distance(x1, y1, x2, y2):
return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
# 示例
print(distance(0, 0, 3, 4)) # 输出 5.0
```
其中,`math.sqrt()` 是 Python 内置的求平方根的函数。