给定一个三维平面,因此有三个坐标,任务是找到给定点之间的距离并显示结果。
在三维平面上,有三个坐标轴,x轴的坐标为(x1,y1,z1),y轴的坐标为(x2,y2,z2),z轴的坐标为(x3,y3,z)。计算它们之间的距离有一个直接的公式如下所示
$$sqrt{lgroup x2-x1rgroup^{2}+lgroup y2-y1rgroup^{2}+lgroup z2-z1rgroup^{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中文网其他相关文章!