ZOJ 3598 Spherical Triangle(计算几何 球面三角形内角和)

题目链接:?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.

Input

There are multiple test cases. The first line of input contains an integerT(0 <T≤ 2012) indicating the number of test cases. ThenTtest 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].

Output

For each test case, output the sum of the interior angles of the triangle measured in degree, accurate to 0.01.

Sample Input10 090 00 90Sample Output270.00References

百度百科:球面三角

代码如下:

#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(计算几何 球面三角形内角和)

相关文章:

你感兴趣的文章:

标签云: