码迷,mamicode.com
首页 > 其他好文 > 详细

uva 11817(球面距离)

时间:2015-08-01 19:10:25      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

题意:假设地球是一个正球体,半径是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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

uva 11817(球面距离)

标签:

原文地址:http://blog.csdn.net/hyczms/article/details/47187537

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!