[英]Declare and define the function distance() to find the Euclidean distance between the two points (x[0], y[0]) and (x[1], y[1]) in c
[英]Calculate distance between 2 coordinates with function in c programing (x,y,z)
我正在尝试计算 3D 中 2 个坐标之间的距离,但是,当编译并输入 (1,1,1), (1,1,1) 时,输出返回 2 而不是 0。
代码在这里
#include <stdio.h>
#include <math.h>
int get_dist(int x_i,int y_i,int z_i,int x_o,int y_o,int z_o){
int coord_dist = 0;
coord_dist = sqrt(((x_o - x_i)^2) + ((y_o - y_i)^2) + ((z_o - z_i)^2));
return coord_dist;
}
int main(void){
int x0,y0,z0,x1,y1,z1;
int dist0_1 = 0;
printf("coord 0:");
scanf("%d %d %d", &x0, &y0, &z0);
printf("coord 1:");
scanf("%d %d %d", &x1, &y1, &z1);
dist0_1 = get_dist(x0,y0,z0,x1,y1,z1);
printf("%d\n", dist0_1);
return 0;
}
^ 是异或,不是指数。 你想要 pow() 函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.