C 棱镜三维空间中两个坐标之间的距离
我有一个家庭作业问题,要求用户提供三维空间中矩形棱镜的顶点。我需要计算棱镜的表面积和体积。我必须有一个计算棱镜上两点之间距离的函数,我可以要求用户从用户那里获得坐标的信息C 棱镜三维空间中两个坐标之间的距离,c,3d,coordinates,distance,C,3d,Coordinates,Distance,我有一个家庭作业问题,要求用户提供三维空间中矩形棱镜的顶点。我需要计算棱镜的表面积和体积。我必须有一个计算棱镜上两点之间距离的函数,我可以要求用户从用户那里获得坐标的信息 double cx1, cy1, cz1, cx2, cy2, cz2, cx3, cy3, cz3, cx4, cy4, cz4, cx5, cy5, cz5, cx6, cy6, cz6, cx7, cy7, cz7, cx8, cy8, cz8; int main() { printf("Enter the f
double cx1, cy1, cz1, cx2, cy2, cz2, cx3, cy3, cz3, cx4, cy4, cz4, cx5, cy5, cz5, cx6, cy6, cz6, cx7, cy7, cz7, cx8, cy8, cz8;
int main() {
printf("Enter the first coordinate in the form x y z: \n");
scanf("%lf %lf %lf", &cx1, &cy1, &cz1);
printf("Enter the second coordinate in the form x y z: \n");
scanf("%lf %lf %lf", &cx2, &cy2, &cz2);
printf("Enter the third coordinate in the form x y z: \n");
scanf("%lf %lf %lf", &cx3, &cy3, &cz3);
printf("Enter the fourth coordinate in the form x y z: \n");
scanf("%lf %lf %lf", &cx4, &cy4, &cz4 );
printf("Enter the fifth coordinate in the form x y z: \n");
scanf("%lf %lf %lf", &cx5, & cy5, &cz5);
printf("Enter the sixth coordinate in the form x y z: \n");
scanf("%lf %lf %lf", &cx6, &cy6, &cz6);
printf("Enter the seventh coordinate in the form x y z: \n");
scanf("%lf %lf %lf", &cx7, &cy7, &cz7);
printf("Enter the eighth coordinate in the form x y z: \n");
scanf("%lf %lf %lf",&cx8, &cy8, &cz8);
return get_dist(cx1, cx2, cy1, cy2);
}
然后,我将每个x、y和z坐标指定给一个变量,这都在主函数中…然后我对一个点执行此操作:
double get_dist(cx1,cx2,cy1, cy2){
double distance1_2;
distance1_2 = sqrt(((cx2 - cx1)*(cx2 - cx1)) - ((cy2 - cy1)*(cy2 - cy1)) );
printf("%lf",distance1_2);
return 0;
}
它给了我正确的值2,但是有没有比单独做每个坐标更简单/更快的方法呢 这就是你可以通过这么多变量来减少头痛的原因:
- 创建一个结构,将点的所有3个坐标分组
- 为点创建和设置阵列
struct Point3 {
double x, y, z;
}; // <-- the semocolon (;) is mandatory here
// hack so that you can use `Point3` instead of `struct Point3` when
// referring to the structure type. This is completely unnecessary in C++
typedef struct Point3 Point3;
int const g_num_points = 8; // this is actually an instance where
// a global variable is arguably not that bad
int main() {
Point3 points[g_num_points]; // avoid global variables like plague
int i;
int ret;
for (i = 0; i < g_num_points; ++i) {
printf("Enter the %d'th coordinate in the form x y z: \n", i);
ret = scanf("%lf %lf %lf", &(points[i].x), &(points[i].y), &(points[i].z));
if (ret != 3) {
// deal with invalid input. Since this is homework
// I will leave it as an exercise for you
}
}
}
struct Point3{
双x,y,z;
}; // 顺便说一下,您的距离计算是错误的。Point3
(或smthg)结构中的第3组坐标。做一个8个点的数组。这是怎么回事?我用C不是C++,上面的代码我缩小它来测试代码,当我继续工作的时候,我意识到我需要包含Z,所以我把它添加到像X和Y一样的上面,所以我用类似Distangs1S2 2= SqRT((Cx2-Cx1)*(Cx2-Cx1))代替它们。(cy2-cy1)*(cy2-cy1)-(cz2-cz1)*(cz2-cz1));虽然我想我最初的问题需要正确的措辞,但我想知道是否有一种更快的方法来获得距离,而不需要单独做每一件事,尽管单独做也能给我正确的答案