题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4682
As everybody knows, the sum of the interior angles of a triangle on a plane is always 180 degree. But this is not true when the triangle is on spherical surface. Given a triangle on a spherical surface, you are asked to calculate the sum of the interior angles of the triangle.
Formally, you are given the 3 vertex of the triangle. They are connected by the arcs of the great circles, i.e. circles whose centers coincide with the center of the sphere. It is guaranteed that the triangle is not degenerate, i.e. the 3 vertices will not lie on one great circle and no two vertices collide. The interior of the triangle is defined as the smaller part that the triangle is divide into.
There are multiple test cases. The first line of input contains an integer T (0 < T ≤ 2012) indicating the number of test cases. Then T test cases follow.
Each test case contains 3 lines, indicating the position of the 3 vertices. Each line contains 2 real number, each of which contains at most 2 digits after the decimal point, indicating the longitude and the latitude of the vertex. The longitude and the latitude are measured in degree. The longitude will be in (-180, 180] while the latitude will be in [-90, 90].
For each test case, output the sum of the interior angles of the triangle measured in degree, accurate to 0.01.
1 0 0 90 0 0 90
270.00
百度百科:球面三角
代码如下:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream> #include <algorithm> using namespace std; #include <stack> #include <queue> #include <vector> #include <deque> #include <set> #include <map> #define pi acos(-1.0) 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))); } 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)); } inline double sphere_dist(double r,double lng1,double lat1,double lng2,double lat2) { return r*angle(lng1,lat1,lng2,lat2); } double lng[5],lat[5],len[5]; int main() { int t; cin>>t; double r=1; while(t--) { for(int i=0; i<3; i++) { cin>>lng[i]>>lat[i]; } for(int i=0; i<3; i++) { len[i]=sphere_dist(r,lng[i],lat[i],lng[(i+1)%3],lat[(i+1)%3]); } double p=0; for(int i=0; i<3; i++) p+=len[i]; p/=2.0; //cout<<'p'<<p<<endl; double m=sqrt((sin(p-len[0])*sin(p-len[1])*sin(p-len[2]))/sin(p)); //cout<<m<<endl; double ctg[5]; double sum=0; for(int i=0; i<3; i++) { ctg[i]=sin(p-len[i])/m; ctg[i]=1.0/ctg[i]; ctg[i]=atan(ctg[i])*2.0; sum+=ctg[i]; } printf("%.2lf\n",sum/pi*180); } return 0; }
ZOJ 3598 Spherical Triangle(计算几何 球面三角形内角和)
原文:http://blog.csdn.net/u012860063/article/details/44785161