写一个c语言程序求两点间的距离
时间: 2023-04-01 17:04:51 浏览: 173
您好,我可以回答您的问题。以下是求两点间距离的 C 语言程序:
#include <stdio.h>
#include <math.h>
int main()
{
float x1, y1, x2, y2, distance;
printf("请输入第一个点的坐标(x1, y1):\n");
scanf("%f %f", &x1, &y1);
printf("请输入第二个点的坐标(x2, y2):\n");
scanf("%f %f", &x2, &y2);
distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
printf("两点之间的距离为: %f\n", distance);
return ;
}
希望能对您有所帮助。
相关问题
c语言极坐标求两点距离
极坐标是一种二维坐标系,其中每个点由极径和极角确定。在极坐标系中,两点之间的距离可以通过它们的极坐标来计算。具体而言,设两点的极坐标分别为(r1,θ1)和(r2,θ2),则它们之间的距离d可以通过以下公式计算:
d = sqrt(r1^2 + r2^2 - 2*r1*r2*cos(θ1-θ2))
下面是一个C语言程序,用于计算两个点的极坐标并计算它们之间的距离:
```c
#include <stdio.h>
#include <math.h>
int main() {
float r1, r2, theta1, theta2, x1, x2, y1, y2, distance;
printf("Enter the polar coordinates of point A (r, theta): ");
scanf("%f, %f", &r1, &theta1);
printf("Enter the polar coordinates of point B (r, theta): ");
scanf("%f, %f", &r2, &theta2);
x1 = r1 * cos(theta1);
y1 = r1 * sin(theta1);
x2 = r2 * cos(theta2);
y2 = r2 * sin(theta2);
distance = sqrt(pow((x1-x2),2) + pow((y1-y2),2));
printf("The distance between the two points is: %.2f", distance);
return 0;
}
```
该程序首先要求用户输入两个点的极坐标,然后将它们转换为直角坐标系中的坐标。最后,程序使用上述公式计算两点之间的距离,并将结果输出到屏幕上。
计算平面上两点间的距离c语言
计算平面上两点间的距离的公式为:
distance = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1))
其中,(x1, y1) 和 (x2, y2) 分别代表两个点的坐标。
以下是一个简单的 C 代码实现:
```c
#include <stdio.h>
#include <math.h>
int main() {
int x1, y1, x2, y2;
double distance;
printf("Enter the coordinates of point 1: ");
scanf("%d %d", &x1, &y1);
printf("Enter the coordinates of point 2: ");
scanf("%d %d", &x2, &y2);
distance = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
printf("Distance between the two points is: %lf\n", distance);
return 0;
}
```
在这个程序中,我们使用 `scanf()` 函数从用户那里获取两个点的坐标,然后使用 `sqrt()` 函数计算距离,并使用 `printf()` 函数显示结果。