用c语言来解决这个问题 大侠们指点一下呀
地球上同一纬度的不同经度的两个城市A、B,问它们地球表面的最近距离是多少?地球半径6300km
[解决办法]
画个图就出来了
把地球当个球体,知道两个点纬度和经度
由纬度和半径长度可以算出该纬度处的半径
再由两点经度和该纬度处的半径就可算出弧长
[解决办法]
上面的说法和代码都是不对的。
在一个球面上的两个点的最短距离是要用大圆的弧段来计算的,而不是用某个纬度所在的小圆的弧段来计算的。看来是高中的立体几何没有学好。
[解决办法]
修正版
- C/C++ code
#include <stdio.h>#include <math.h>void main(){ float latitude,longitude1,longitude2,dtLongitude; float r,chord2,cosa; latitude=40.0; longitude1=100.0; longitude2=102.0; dtLongitude=fabs(longitude2-longitude1); if (dtLongitude>180.0) dtLongitude=360.0-dtLongitude; r=cos(latitude/180*3.1416)*6300.0; chord2=2*r*r-2*r*r*cos(dtLongitude/180*3.1416); cosa=1-chord2/(2*6300*6300); printf("两点间的地表距离是:%f",6300*acos(cosa));}
[解决办法]
修正后程序:
- C/C++ code
#include <stdio.h>#include <math.h>void main(){ const float PI = 3.1415926; const float R = 6300.0; // 西经用请用负数表示,东经用正数表示 float latitude, longitude1, longitude2, tmpLongitude; float distance = 0.0; printf("Please input the latitude:\n"); scanf("%f", &latitude); printf("Please input the longitude1 and longitude2:\n"); scanf("%f %f", &longitude1, &longitude2); printf("The latitude is:%f\nthe longitude1 is:%f\n The longitude2 is:%f\n" , latitude, longitude1, longitude2); // calculate the distance between the two points. // 保证算锐角的距离 tmpLongitude = abs(longitude1 - longitude2) > 180 ? 360 - abs(longitude1 - longitude2) : abs(longitude1 - longitude2); distance = 2 * asin(sqrt(pow(sin(0.0), 2) + cos(latitude * PI / 180) * cos(latitude * PI / 180) * pow(sin(tmpLongitude * PI / 180 / 2), 2))); distance = R * distance; printf("The distance is:%f\n", distance); system("pause"); }
[解决办法]
求球中的大圆 制定两点之间的距离
[解决办法]
修正版
C/C++ code#include <stdio.h>
#include <math.h>
void main()
{
float latitude,longitude1,longitude2,dtLongitude;
float r,chord2,cosa;
latitude=40.0;
longitude1=100.0;
longitude2=102.0;
dtLongitude=fabs(longitude2-longitude1);
if (dtLongitude>180.0)
dtLongitude=360.0-dtLongitude;
r=cos(latitude/180*3.1416)*6300.0;
chord2=2*r*r-2*r*r*cos(dtLongitude/180*3.1416);
cosa=1-chord2/(2*6300*6300);
printf("两点间的地表距离是:%f",6300*acos(cosa));
}