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

标签 c function

我正在尝试计算 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() 函数。

关于使用c编程中的函数计算2个坐标之间的距离(x,y,z),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33060078/

相关文章:

c++ - 当第一个字符串在预处理器指令中定义而第二个字符串在 C++ 中为常量时,如何连接 2 个字符串?

c - 为什么函数不能与指针一起使用?

c++ - 囤积内存分配器

javascript - 为什么 foo2() 在这里返回 undefined ?

c - 如何将整数数组转换为排列并计算其中的循环?

oracle - Oracle函数中如何处理被零除的情况

c - 父进程关闭后如何保留子进程对终端的访问权限

python - 如何在 Lua 中直接访问具有多个输出的函数的第 n 个输出

javascript - 为什么这个功能有效?每次调用该函数时, "executed"不应该返回 false 吗?

stdint 的 int8_t 可以存在于没有 8 位字节的架构上吗?