Test instructions: Give the latitude and longitude of two points, find the difference between the spherical distance and the straight distance of two points.
Solution: We calculate the spherical distance first, then we can calculate the straight distance according to the spherical distance.
Spherical distance formula: r*acos (sin (W1) *sin (W2) +cos (W1) *cos (W2) *cos (j1-j2)); (W1,W2 is the latitude value of two points, j1,j2 is the longitude value of two points)
The derivation process is not written, can be found on the Internet very clear derivation process.
Then calculate the spherical distance, is actually an arc, according to the length of the arc to find the chord length:
Code:
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#definePi ACOs (-1.0)using namespacestd;intMain () {intT,n,i; DoubleX1,x2,y1,y2; DoubleR =6371009; scanf ("%d",&t); while(t--) {scanf ("%LF%LF",&x1,&y1); scanf ("%LF%LF",&x2,&y2); X1= x1*pi/180.0; Y1= y1*pi/180.0; X2= x2*pi/180.0; Y2= y2*pi/180.0; Doubleans = r*acos (sin (x1) *sin (x2) +cos (x1) *cos (x2) *cos (y1-y2)); printf ("%.0f\n", ans-2*r*sin (ans/(2*R)); } return 0;}
View Code
UVA 11817 tunnelling The Earth--Spherical distance formula