以经纬度求球上两点距离:经纬度板子

例题:POJ 2354 Titanic

#include<bits/stdc++.h>
#define ll long long
#define IOS std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DEBUG cout<<",here\n";
#define Rep(i,l,r) for(int i=(l);i<=(r);i++)
using namespace std;

const double eps=1e-4;
const double PI=acos(-1.0);
int sgn(double x){return (x>eps)-(x<-eps);};
struct Point3 {double x, y, z;};
Point3 operator-(const Point3& a, const Point3& b) {return (Point3){a.x-b.x, a.y-b.y, a.z-b.z};}
Point3 operator+(const Point3& a, const Point3& b) {return (Point3){a.x+b.x, a.y+b.y, a.z+b.z};}
double Dot3(Point3 A, Point3 B){return A.x*B.x + A.y*B.y + A.z*B.z;}
double Length3(Point3 A){return sqrt(Dot3(A, A));}
double radians(double angle){return angle*PI/180;}

struct Sphere{
    Point3 o={0,0,0};double r;

    Point3 GetCoord(double latitude, double longitude){//纬度以北为正,经度以东为正
        latitude=radians(latitude);
        longitude=radians(longitude);
        Point3 p;
        p.x=r*cos(latitude)*cos(longitude);
        p.y=r*cos(latitude)*sin(longitude);
        p.z=r*sin(latitude);
        p=p+o;
        return p;
    }
    double GetDisAB(Point3 a, Point3 b){
        double d=Length3(a-b);
        double rad=2*asin(d/2/r);
        return rad*r;
    }
};
Sphere sphere={{0,0,0},6875.0/2.0};

int main()
{
    string s;
    Rep(i,1,3){getline(cin,s);}
    double latitude,longitude,a,b,c;
    char d;
    scanf("%lf^%lf'%lf\" %cL ",&a,&b,&c,&d);
    latitude=a+b/60+c/3600;
    if(d=='S')latitude*=-1;
    scanf("and %lf^%lf'%lf\" %cL.",&a,&b,&c,&d);
    longitude=a+b/60+c/3600;
    if(d=='W')longitude*=-1;
    Point3 p1=sphere.GetCoord(latitude,longitude);

    getchar();
    getline(cin,s);
    scanf("%lf^%lf'%lf\" %cL ",&a,&b,&c,&d);
    latitude=a+b/60+c/3600;
    if(d=='S')latitude*=-1;
    scanf("and %lf^%lf'%lf\" %cL.",&a,&b,&c,&d);
    longitude=a+b/60+c/3600;
    if(d=='W')longitude*=-1;
    Point3 p2=sphere.GetCoord(latitude,longitude);
    getchar();
    getline(cin,s);

    double dis=sphere.GetDisAB(p1,p2);
    printf("The distance to the iceberg: %.2lf miles.\n",dis);
    if(floor(dis+0.005)<100){
        printf("DANGER!\n");
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值