棱镜的 3D space 中两个坐标之间的距离
Distance between two coordinates in 3D space of a prism
我有一个家庭作业问题,要求用户提供位于 3-D space 中的直角棱柱的顶点。我需要计算棱镜的表面积和体积。我必须有一个函数来计算棱镜上两点之间的距离,我可以要求从用户那里获取坐标是什么的信息
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,但是有没有一种 easier/faster 方法可以做到这一点,而不是必须单独处理每个坐标?
这就是您可以通过这么多变数来减轻头痛的方法:
- 创建一个结构来对一个点的所有 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
}
}
}
由于这是家庭作业,我将让您自己弄清楚如何使用 Point3
结构编写 get_distance
函数。
顺便说一句,3d 坐标中两点之间的距离涉及......嗯......两点,每个点有 3 个坐标,你有 6 个坐标。您的函数只需要 4 个坐标作为输入参数。使用像这样的结构通过它的 3 个坐标来表示一个点应该可以更清楚地解决这个问题。
我有一个家庭作业问题,要求用户提供位于 3-D space 中的直角棱柱的顶点。我需要计算棱镜的表面积和体积。我必须有一个函数来计算棱镜上两点之间的距离,我可以要求从用户那里获取坐标是什么的信息
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,但是有没有一种 easier/faster 方法可以做到这一点,而不是必须单独处理每个坐标?
这就是您可以通过这么多变数来减轻头痛的方法:
- 创建一个结构来对一个点的所有 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
}
}
}
由于这是家庭作业,我将让您自己弄清楚如何使用 Point3
结构编写 get_distance
函数。
顺便说一句,3d 坐标中两点之间的距离涉及......嗯......两点,每个点有 3 个坐标,你有 6 个坐标。您的函数只需要 4 个坐标作为输入参数。使用像这样的结构通过它的 3 个坐标来表示一个点应该可以更清楚地解决这个问题。