poj2354
计算球面距离,度分秒转换。
#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; }
补充:软件开发 , C++ ,