程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj2354

poj2354

編輯:C++入門知識

計算球面距離,度分秒轉換。

 

 

 #include <iostream>  
#include <math.h>  
#include <stdio.h>  
 
#define pi acos(-1.0)  
 
//計算圓心角lat表示緯度,-90<=w<=90,lng表示經度  
//返回兩點所在大圓劣弧對應圓心角,0<=angle<=pi  
double angle(double lng1, double lat1, double lng2, double lat2){ 
    double dlng = fabs(lng1 - lng2)*pi / 180; 
    while (dlng >= pi + pi) 
        dlng -= pi + pi; 
    if (dlng > pi) 
        dlng = pi + pi - dlng; 
    lat1 *= pi / 180, lat2 *= pi / 180; 
    return acos(cos(lat1)*cos(lat2)*cos(dlng) + sin(lat1)*sin(lat2)); 
} 
 
//計算距離,r為球半徑  
double line_dist(double r, double lng1, double lat1, double lng2, double lat2){ 
    double dlng = fabs(lng1 - lng2)*pi / 180; 
    while (dlng >= pi + pi) 
        dlng -= pi + pi; 
    if (dlng > pi) 
        dlng = pi + pi - dlng; 
    lat1 *= pi / 180, lat2 *= pi / 180; 
    return r*sqrt(2 - 2 * (cos(lat1)*cos(lat2)*cos(dlng) + sin(lat1)*sin(lat2))); 
} 
 
//計算球面距離,r為球半徑  
inline double sphere_dist(double r, double lng1, double lat1, double lng2, double lat2){ 
    return r*angle(lng1, lat1, lng2, lat2); 
} 
 
double Dis3D(double a1,double b1,double a2,double b2) 
{ 
    return 3437.5*acos(sin(a1)*sin(a2)+cos(a1)*cos(a2)*cos(b1-b2)); 
} 
 
int main() 
{ 
    char ccc[100]; 
    gets(ccc); 
    gets(ccc); 
    gets(ccc); 
 
    double a, b, c; 
    char cc; 
    scanf("%lf%*c%lf%*c%lf%*c %c%*c%*c", &a, &b, &c, &cc); 
    //std::cout << a << ' ' << b << ' ' << c << ' ' << cc << std::endl;  
    double angle = a + b*1.0/60 + c *1.0/3600; 
    if(cc == 'S') angle = - angle; 
    //std::cout << angle << std::endl;  
 
    double d, e, f; 
    char cccc; 
    scanf("%*s%lf%*c%lf%*c%lf%*c %c%*c%*c%*c",&d,&e,&f,&cccc); 
    //std::cout << d << ' ' << e << ' ' << f << ' ' << cccc << std::endl;  
    double angle2 = d + e*1.0/60 + f *1.0/3600; 
    if(cccc == 'W') angle2 = - angle2; 
    //std::cout << angle2 << std::endl;  
 
    gets(ccc); 
    //std::cout << ccc << std::endl;  
    scanf("%lf%*c%lf%*c%lf%*c %c%*c%*c", &a, &b, &c, &cc); 
    //std::cout << a << ' ' << b << ' ' << c << ' ' << cc << std::endl;  
    double angle3 = a + b*1.0/60 + c *1.0/3600; 
    if(cc == 'S') angle3 = - angle3; 
    //std::cout << angle3 << std::endl;  
 
    scanf("%*s%lf%*c%lf%*c%lf%*c %c%*c%*c%*c",&d,&e,&f,&cccc); 
    //std::cout << d << ' ' << e << ' ' << f << ' ' << cccc << std::endl;  
    double angle4 = d + e*1.0/60 + f *1.0/3600; 
    if(cccc == 'W') angle4 = - angle4; 
    //std::cout << angle4 << std::endl;  
 
    gets(ccc); 
 
    ///double dis = sphere_dist(3437.5,angle2,angle,angle4,angle3);  
    double dd = Dis3D(angle*acos(-1.0)/180,angle2*acos(-1.0)/180,angle3*acos(-1.0)/180,angle4*acos(-1.0)/180); 
    printf("The distance to the iceberg: %.2f miles.\n",dd); 
    if(floor(dd + 0.005) < 100) std::cout << "DANGER!" << std::endl; 
 
} 

#include <iostream>
#include <math.h>
#include <stdio.h>

#define pi acos(-1.0)

//計算圓心角lat表示緯度,-90<=w<=90,lng表示經度
//返回兩點所在大圓劣弧對應圓心角,0<=angle<=pi
double angle(double lng1, double lat1, double lng2, double lat2){
 double dlng = fabs(lng1 - lng2)*pi / 180;
 while (dlng >= pi + pi)
  dlng -= pi + pi;
 if (dlng > pi)
  dlng = pi + pi - dlng;
 lat1 *= pi / 180, lat2 *= pi / 180;
 return acos(cos(lat1)*cos(lat2)*cos(dlng) + sin(lat1)*sin(lat2));
}

//計算距離,r為球半徑
double line_dist(double r, double lng1, double lat1, double lng2, double lat2){
 double dlng = fabs(lng1 - lng2)*pi / 180;
 while (dlng >= pi + pi)
  dlng -= pi + pi;
 if (dlng > pi)
  dlng = pi + pi - dlng;
 lat1 *= pi / 180, lat2 *= pi / 180;
 return r*sqrt(2 - 2 * (cos(lat1)*cos(lat2)*cos(dlng) + sin(lat1)*sin(lat2)));
}

//計算球面距離,r為球半徑
inline double sphere_dist(double r, double lng1, double lat1, double lng2, double lat2){
 return r*angle(lng1, lat1, lng2, lat2);
}

double Dis3D(double a1,double b1,double a2,double b2)
{
    return 3437.5*acos(sin(a1)*sin(a2)+cos(a1)*cos(a2)*cos(b1-b2));
}

int main()
{
 char ccc[100];
 gets(ccc);
 gets(ccc);
 gets(ccc);

 double a, b, c;
 char cc;
 scanf("%lf%*c%lf%*c%lf%*c %c%*c%*c", &a, &b, &c, &cc);
 //std::cout << a << ' ' << b << ' ' << c << ' ' << cc << std::endl;
 double angle = a + b*1.0/60 + c *1.0/3600;
 if(cc == 'S') angle = - angle;
 //std::cout << angle << std::endl;

 double d, e, f;
 char cccc;
    scanf("%*s%lf%*c%lf%*c%lf%*c %c%*c%*c%*c",&d,&e,&f,&cccc);
    //std::cout << d << ' ' << e << ' ' << f << ' ' << cccc << std::endl;
    double angle2 = d + e*1.0/60 + f *1.0/3600;
    if(cccc == 'W') angle2 = - angle2;
    //std::cout << angle2 << std::endl;

    gets(ccc);
    //std::cout << ccc << std::endl;
    scanf("%lf%*c%lf%*c%lf%*c %c%*c%*c", &a, &b, &c, &cc);
 //std::cout << a << ' ' << b << ' ' << c << ' ' << cc << std::endl;
    double angle3 = a + b*1.0/60 + c *1.0/3600;
    if(cc == 'S') angle3 = - angle3;
 //std::cout << angle3 << std::endl;

    scanf("%*s%lf%*c%lf%*c%lf%*c %c%*c%*c%*c",&d,&e,&f,&cccc);
    //std::cout << d << ' ' << e << ' ' << f << ' ' << cccc << std::endl;
    double angle4 = d + e*1.0/60 + f *1.0/3600;
    if(cccc == 'W') angle4 = - angle4;
    //std::cout << angle4 << std::endl;

    gets(ccc);

    ///double dis = sphere_dist(3437.5,angle2,angle,angle4,angle3);
    double dd = Dis3D(angle*acos(-1.0)/180,angle2*acos(-1.0)/180,angle3*acos(-1.0)/180,angle4*acos(-1.0)/180);
    printf("The distance to the iceberg: %.2f miles.\n",dd);
    if(floor(dd + 0.005) < 100) std::cout << "DANGER!" << std::endl;

}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved