简体   繁体   English

用c编程中的函数计算2个坐标之间的距离(x,y,z)

[英]Calculate distance between 2 coordinates with function in c programing (x,y,z)

I'm trying to calculate the distance between 2 coordinates in 3D, however, when complied, and entered (1,1,1), (1,1,1), output returns 2 instead of 0.我正在尝试计算 3D 中 2 个坐标之间的距离,但是,当编译并输入 (1,1,1), (1,1,1) 时,输出返回 2 而不是 0。

Code here代码在这里

#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;
}

^ is XOR, not exponent. ^ 是异或,不是指数。 You want the pow() function.你想要 pow() 函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM