python求8个坐标相互之间的距离
时间: 2023-08-11 16:42:55 浏览: 60
好的,这里给出 Python 代码实现求8个坐标相互之间的距离:
```python
import math
# 假设有8个坐标分别为 (x1, y1), (x2, y2), (x3, y3), (x4, y4), (x5, y5), (x6, y6), (x7, y7), (x8, y8)
coordinates = [(x1, y1), (x2, y2), (x3, y3), (x4, y4), (x5, y5), (x6, y6), (x7, y7), (x8, y8)]
# 定义一个函数计算两个坐标之间的距离
def distance(x1, y1, x2, y2):
return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
# 遍历每个坐标计算它们与其他坐标之间的距离
for i in range(len(coordinates)):
print(f"第{i+1}个坐标与其他坐标之间的距离为:")
for j in range(len(coordinates)):
if i != j:
dist = distance(coordinates[i][0], coordinates[i][1], coordinates[j][0], coordinates[j][1])
print(f"第{j+1}个坐标与第{i+1}个坐标之间的距离为:{dist}")
```
其中,math 模块中的 sqrt 函数用于求平方根。这段代码会输出每个坐标与其他坐标之间的距离。你只需要将代码中的 (x1, y1) 到 (x8, y8) 替换成对应的坐标即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)