Advertisement
Guest User

polar.cpp

a guest
May 6th, 2018
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define FR(i,a,b) for (int i=(a); i<(b); i++)
  3. #define F(i,n) FR(i,0,n)
  4. using namespace std;
  5. typedef long double ld;
  6. const ld PIl = acos((ld)-1);
  7.  
  8. struct p3 {ld x,y,z;};
  9. p3 operator+(p3 a, p3 b) {return {a.x+b.x, a.y+b.y, a.z+b.z};}
  10. p3 operator-(p3 a, p3 b) {return {a.x-b.x, a.y-b.y, a.z-b.z};}
  11. p3 operator*(p3 a, ld d) {return {a.x*d, a.y*d, a.z*d};}
  12.  
  13. ld eval(function<ld(ld)> f, ld a, ld b) {
  14.     return (b-a)/6 * (f(a) + 4*f((a+b)/2) + f(b));
  15. }
  16.  
  17. ld integrate(function<ld(ld)> f, ld a, ld b) {
  18.     ld mid = (a+b)/2, q1 = eval(f,a,b),
  19.         q2 = eval(f,a,mid) + eval(f,mid,b);
  20.     if (abs(q2-q1) <= 1e-11)
  21.         return q2 + (q2-q1)/15;
  22.     return integrate(f,a,mid) + integrate(f,mid,b);
  23. }
  24.  
  25. ld cover(p3 a, p3 b) {
  26.     ld cross = a.x*b.y - a.y*b.x;
  27.     auto f = [&](ld t) {
  28.         p3 p = a+(b-a)*t;
  29.         ld sqXY = p.x*p.x + p.y*p.y;
  30.         ld r = .5 - atan(p.z/sqrt(sqXY))/PIl;
  31.         ld dphi = cross/sqXY;
  32.         return r*r*dphi/2;
  33.     };
  34.     ld res = integrate(f, 0, 1);
  35.     return res;
  36. }
  37.  
  38. p3 sph(ld lat, ld lon) {
  39.     lat *= PIl/180, lon *= PIl/180;
  40.     return {cos(lat)*cos(lon), cos(lat)*sin(lon), sin(lat)};
  41. }
  42.  
  43. signed main() {
  44.     ios::sync_with_stdio(false);
  45.     cin.tie(NULL);
  46.     int n; cin >> n;
  47.     vector<p3> ps(n);
  48.     F(i,n) {
  49.         int lat, lon; cin >> lat >> lon;
  50.         ps[i] = sph(lat, lon);
  51.     }
  52.     ld sum = 0;
  53.     F(i,n) sum += cover(ps[i], ps[(i+1)%n]);
  54.     cout << fixed << setprecision(12) << sum << "\n";
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement