UVA11817 Tunnelling the Earth(计算几何、经纬度转空间坐标、球面距离)

整理的算法模板合集: ACM模板


题目大意:
以经纬度形式给出地球(当作一个球)上两个点,求这类的最短弧线距离和两点直线距离之差(四舍五入),假设地球半径为:6371009米

照着训练指南上的模板打就好。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#include<iostream>

using namespace std;
typedef long long ll;
const int N = 50007, M = 500007, INF = 0x3f3f3f3f;
const double inf = 1e100;
const double eps = 1e-8;
int sgn(double d)
{
    if(fabs(d) < eps)
        return 0;
    if(d < 0)
        return 1;
    return -1;
}

int dcmp(double x, double y)
{
    if(fabs(x - y) < eps)
        return 0;
    if(x > y)
        return 1;
    return -1;
}

struct Point
{
    double x, y;
    Point(double x = 0, double y = 0):x(x), y(y) { }
};

struct Point_3D
{
    double x, y, z;
    Point_3D(double x = 0, double y = 0, double z = 0):x(x), y(y), z(z){ }
};

const int R = 6371009;

double torad(double deg)//角度转弧度
{
    return deg / 180 * acos(-1);
}
//经纬度(角度)转化为空间坐标
void get_coord(double lat, double lng, double &x, double &y, double &z)
{
    lat = torad(lat);
    lng = torad(lng);
    x = R * cos(lat) * cos(lng);
    y = R * cos(lat) * sin(lng);
    z = R * sin(lat);
}
//就离谱,这里没有加double竟然不报错,导致答案小了1 ???
double get_dist_3D(Point_3D A, Point_3D B)
{
    double fx = A.x - B.x;
    double fy = A.y - B.y;
    double fz = A.z - B.z;
    return sqrt(fx * fx + fy * fy + fz * fz);
}
int t;
int main()
{
    scanf("%d", &t);
    while(t -- ){
        Point A, B;
        Point_3D a, b;
        scanf("%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y);
        get_coord(A.x, A.y, a.x, a.y, a.z);
        get_coord(B.x, B.y, b.x, b.y, b.z);
        double distance = get_dist_3D(a, b);
        double hu = 2.0 * asin(distance / 2.0 / R) *R;
        printf("%.f\n", hu - distance);//可以直接四舍五入
        //printf("%d\n", (int)(hu - distance + 0.5));
    }
    return 0;
}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:设计师小姐姐 返回首页