用C语言计算两点之间的距离
时间: 2023-03-25 20:02:05 浏览: 112
可以使用以下公式计算两点之间的距离:
distance = sqrt(pow((x2-x1),2) + pow((y2-y1),2));
其中,(x1,y1)和(x2,y2)分别是两个点的坐标,pow()函数用于计算平方,sqrt()函数用于计算平方根。
相关问题
C语言计算两点之间的距离
以下是两种C语言计算两点之间距离的方法:
1.使用math库中的sqrt()函数和pow()函数
```c
#include <stdio.h>
#include <math.h>
int main()
{
float x1, y1, x2, y2, dist;
printf("请输入两点坐标(x1,y1,x2,y2):");
scanf("%f%f%f%f", &x1, &y1, &x2, &y2);
dist = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
printf("两点之间的距离为:%.2f\n", dist);
return 0;
}
```
2.不使用math库中的函数
```c
#include <stdio.h>
int main()
{
float x1, y1, x2, y2, dist;
printf("请输入两点坐标(x1,y1,x2,y2):");
scanf("%f%f%f%f", &x1, &y1, &x2, &y2);
dist = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
printf("两点之间的距离为:%.2f\n", dist);
return 0;
}
```
用c语言计算两点间的距离
C语言中可以使用勾股定理来计算两点之间的距离,即d=sqrt((x2-x1)^2+(y2-y1)^2)。其中sqrt()函数是C语言中的数学函数,用于计算平方根。下面是一个示例代码:
```c
#include <stdio.h>
#include <math.h>
double dist(double x1, double y1, double x2, double y2) {
double d = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
return d;
}
int main() {
double x1, y1, x2, y2;
scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
printf("dist = %.2f\n", dist(x1, y1, x2, y2));
return 0;
}
```