如何计算三维空间中点与点之间的距离?

问题描述 投票:0回答:1

如何计算三维空间中点与点之间的距离? 我有三个一维数组,用于构建三维图。我需要找到点之间的距离。我想沿 y 轴取间隔,对点进行排序并找到线的长度,然后是下一个间隔等,然后将所有内容加起来。但我做不到。请告诉我如何做这个或其他计算方式。 使用 Python

我尝试使用 pdist,将 y 轴划分为间隔

python calculation area
1个回答
0
投票

以下代码展示了如何使用 math.dist() 函数计算 3 维空间中两点之间的距离:

import math

# Define the points
p1 = (1, 2, 3)
p2 = (4, 5, 6)

# Calculate the distance
distance = math.dist(p1, p2)

# Print the distance
print(distance)

在本例中,输出为 5.

如果您想手动执行此操作(没有库),则需要使用毕达哥拉斯定理。

# Define the points
p1 = (1, 2, 3)
p2 = (4, 5, 6)

# Calculate the distance
x1, y1, z1 = p1
x2, y2, z2 = p2

distance = sqrt((x2 - x1)² + (y2 - y1)² + (z2 - z1)²)

# Print the distance
print(distance)
© www.soinside.com 2019 - 2024. All rights reserved.