地球的平均半徑爲6371千米,已知地球上兩個城市A、B的經度和緯度,編程序求出這兩個城市之間的地面距離
題目:
地球的平均半徑爲6371千米,已知地球上兩個城市A、B的經度和緯度,編程序求出這兩個城市之間的地面距離
解答:
求出經緯度差,根據cosθ=cosθ1cosθ2求出兩城到球心的角的弧度θ,再根據L = θ * R可求球面距離.
編程如下,僅是給個思路,考慮不周之處樓主請自行完善.
#include
#include
#include
using namespace std;
const double R = 6371.0;
const double PI = 3.141592653;
struct Coordinate
{
\x09string X;//經度
\x09string Y;//緯度
};
double GetTheta(Coordinate& A,Coordinate& B)
{
\x09int la,lb;
\x09double a,b,dx,dy;
\x09char cha,chb;
\x09string ax,ay,bx,by;
\x09ax = A.X;
\x09ay = A.Y;
\x09bx = B.X;
\x09by = B.Y;
\x09la = ax.length();
\x09lb = bx.length();
\x09cha = ax[la-1]; ax[la-1] = 0;
\x09chb = bx[lb-1]; bx[lb-1] = 0;
\x09sscanf(ax.data(),"%lf",&a);
\x09sscanf(bx.data(),"%lf",&b);
\x09if(cha == chb)
\x09\x09dx = abs(a-b);
\x09else
\x09{
\x09\x09dx = abs(a+b);
\x09\x09if(dx > 180) dx = 360 - dx;
\x09}
\x09la = ay.length();
\x09lb = by.length();
\x09cha = ay[la-1]; ay[la-1] = 0;
\x09chb = by[lb-1]; by[lb-1] = 0;
\x09sscanf(ay.data(),"%lf",&a);
\x09sscanf(by.data(),"%lf",&b);
\x09if(cha == chb)
\x09\x09dy = abs(a-b);
\x09else
\x09\x09dy = abs(a+b);
\x09dx = dx * PI / 180;//角度轉弧度
\x09dy = dy * PI / 180;
\x09return acos(cos(dx)*cos(dy)) ;
}
int main()
{
\x09double theta,dis;
\x09Coordinate A,B;
\x09A.X = "12.35E";
\x09A.Y = "23.05N";
\x09B.X = "30.25E";
\x09B.Y = "20.28S";
\x09theta = GetTheta(A,B);
\x09dis = theta * R;
\x09printf("%.2lfkm\n",dis);
\x09
\x09return 0;
}
- 上一篇 已知,在如下圖形中,AB平行於CD
- 下一篇 三次根號168與根號101怎麼比較大小
添加新評論