给定两点坐标,任务是找到两点之间的距离并显示结果。
在二维平面中有两个点,假设 a 和 b 具有各自的坐标作为 (x1, y1) 和 (x2, y2) 并计算它们之间的距离,有一个直接公式,如下所示
$$\sqrt{\lgroup x2-x1\rgroup^{2 }+\lgroup y2-y1\rgroup^{2}}$$
下面是表示两点及其差异的图表
$$ \frac{(x_2-x_1)}{(x_1,y_1)\:\:\:\:\:\:(y_2-y_1)\:\:\:\:\:\:(x_2,y_2)} $$
下面使用的方法如下 -
输入坐标为x1、x2、y1和y2应用公式计算两点之间的差异打印距离算法startstep 1-> declare function to calculate distance between two point void three_dis(float x1, float y1, float x2, float y2) set float dis = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0) print disstep 2-> in main() set float x1 = 4 set float y1 = 9 set float x2 = 5 set float y2 = 10 call two_dis(x1, y1, x2, y2)stop
示例#include <stdio.h>#include<math.h>//function to find distance bewteen 2 pointsvoid two_dis(float x1, float y1, float x2, float y2) { float dis = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0); printf("distance between 2 points are : %f", dis); return;}int main() { float x1 = 4; float y1 = 9; float x2 = 5; float y2 = 10; two_dis(x1, y1, x2, y2); return 0;}
输出如果我们运行上述代码,它将生成以下输出
distance between 2 points are : 1.414214
以上就是计算两点之间距离的c程序的详细内容。