给定一个三维平面,因此有三个坐标,任务是找到给定点之间的距离并显示结果。
在三维平面上,有三个坐标轴,x轴的坐标为(x1,y1,z1),y轴的坐标为(x2,y2,z2),z轴的坐标为(x3,y3,z)。计算它们之间的距离有一个直接的公式如下所示
$$\sqrt{\lgroup x2-x1\rgroup^{2}+\lgroup y2-y1\rgroup^{2}+\lgroup z2-z1\rgroup^{2}}$$
下面是表示三个不同坐标轴及其坐标的图示

下面使用的方法如下 −
Start
Step 1-> declare function to calculate distance between three point
void three_dis(float x1, float y1, float z1, float x2, float y2, float z2)
set float dis = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) + pow(z2 - z1, 2) * 1.0)
print dis
step 2-> In main()
Set float x1 = 4
Set float y1 = 9
Set float z1 = -3
Set float x2 = 5
Set float y2 = 10
Set float z2 = 9
Call three_dis(x1, y1, z1, x2, y2, z2)
Stop#include <stdio.h>
#include<math.h>
//function to find distance bewteen 3 point
void three_dis(float x1, float y1, float z1, float x2, float y2, float z2) {
float dis = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) + pow(z2 - z1, 2) * 1.0);
printf("Distance between 3 points are : %f", dis);
return;
}
int main() {
float x1 = 4;
float y1 = 9;
float z1 = -3;
float x2 = 5;
float y2 = 10;
float z2 = 9;
three_dis(x1, y1, z1, x2, y2, z2);
return 0;
}如果我们运行上面的代码,它将生成以下输出
Distance between 3 points are : 12.083046
以上就是C程序计算3D空间中三个点之间的距离的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号