简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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