题意:假设地球是一个正球体,半径是6371009米,给出地球上起始点和终止点的经纬度,北纬和东经用正数表示,南纬和西经用负数表示,问沿着球面走的最短路和用隧道走直线路程相差多少。
题解:先把纬度加180,范围转化为0~360,然后把角度转化为弧度,再把经纬度位置转化为空间坐标。
x = r * cos(a) * cos(b)
y = r * cos(a) * sin(b)
z = r * sin(a)
(x,y,z)是空间坐标,a是纬度[0,2π],b是经度[-π/2,π/2]
那么直线距离就是弦长d
球面距离就是弧长l
l = 2r * arcsin(d/2r)
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double r = 6371009;
const double PI = acos(-1);
struct Point3 {
double x, y, z;
Point3(double a = 0, double b = 0, double c = 0):x(a), y(b), z(c) {}
}S, E;
double torad(double deg) {
return deg / 180 * PI;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
double Sew, Sns, Eew, Ens;
scanf("%lf%lf%lf%lf", &Sns, &Sew, &Ens, &Eew);
Sns += 180;
Ens += 180;
Sew = torad(Sew);
Sns = torad(Sns);
Ens = torad(Ens);
Eew = torad(Eew);
S.z = r * sin(Sns);
S.y = r * cos(Sns) * sin(Sew);
S.x = r * cos(Sns) * cos(Sew);
E.z = r * sin(Ens);
E.y = r * cos(Ens) * sin(Eew);
E.x = r * cos(Ens) * cos(Eew);
double len1 = sqrt((S.x - E.x) * (S.x - E.x) + (S.y - E.y) * (S.y - E.y) + (S.z - E.z) * (S.z - E.z));
double len2 = 2 * r * asin(len1 / (2 * r));
printf("%lld\n", (long long)(len2 - len1 + 0.5));
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-12 03:34:04