Tunnelling the Earth
There are different methods of transporting people from place to place: cars, bikes, boats, trains, planes, etc. For very long distances, people generally fly in a plane. But this has the disadvantage that the plane must fly around the curved surface of the earth. A distance travelled would be shorter if the traveller followed a straight line from one point to the other through a tunnel through the earth.For example, travelling from Waterloo to Cairo requires a distance of 9293521 metres following the great circle route around the earth, but only 8491188 metres following the straight line through the earth.
For this problem, assume that the earth is a perfect sphere with radius of 6371009 metres.
Input Specification
The first line of input contains a single integer, the number of test cases to follow. Each test case is one line containing four floating point numbers: the latitude and longitude of the origin of the trip, followed by the latitude and longitude of the destination of the trip. All of these measurements are in degrees. Positive numbers indicate North latitude and East longitude, while negative numbers indicate South latitude and West longitude.Sample Input
1 43.466667 -80.516667 30.058056 31.228889
Output Specification
For each test case, output a line containing a single integer, the difference in the distance between the two points following the great circle route around the surface of the earth and following the straight line through the earth, in metres. Round the difference of the distances to the nearest integer number of metres.Output for Sample Input
802333
题意:给你地球上的两个点的纬度和经度,问他们在圆上的距离减去他们的直线距离是多少。
思路:首先根据纬度z的坐标是可以直接算出来的,然后通过经度可以算出y=kx,然后x^2+y^2+z^2=R^2,可以算出xyz,通过两个坐标来算直线距离,以及圆心角,从而算出圆上距离。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double R=6371009,PI=3.14159265358979;
int main()
{
int T,t,i,j;
double la1,la2,lo1,lo2,x1,y1,z1,x2,y2,z2,d,D,ret,ans,k;
scanf("%d",&T);
for(t=1;t<=T;t++)
{
scanf("%lf%lf%lf%lf",&la1,&lo1,&la2,&lo2);
z1=R*sin(la1/180*PI);
k=tan(lo1/180*PI);
x1=sqrt((R*R-z1*z1)/(1+k*k));
if(abs(lo1)>90)
x1=-x1;
y1=k*x1;
z2=R*sin(la2/180*PI);
k=tan(lo2/180*PI);
x2=sqrt((R*R-z2*z2)/(1+k*k));
if(abs(lo2)>90)
x2=-x2;
y2=k*x2;
d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
ret=acos(1-d*d/(2*R*R));
D=ret*R;
ans=D-d;
printf("%.0f\n",ans);
}
}