Advertisement
Manioc

basic geo

Aug 24th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define EPS 1e-9
  3. #define PI acos(-1.0)
  4.  
  5. using namespace std;
  6.  
  7. double DEG_to_RED(double d){
  8.     return d*PI/180.0;
  9. }
  10.  
  11. double RED_to_DEG(double d){
  12.     return d*180.0/PI;
  13. }
  14.  
  15. struct point{
  16.     double x, y;
  17.     point(){
  18.         x = y = 0.0;
  19.     }
  20.  
  21.     point(double _x, double _y): x(_x), y(_y) {}
  22.  
  23.     bool operator==(point &other) const{
  24.         return fabs(x-other.x) < EPS && fabs(y-other.y) < EPS;
  25.     }
  26. };
  27.  
  28. struct vetor{
  29.     double x, y;
  30.     vetor(double _x, double _y): x(_x), y(_y){}
  31. };
  32.  
  33. vetor toVetor(point a, point b){
  34.     return vetor(b.x-a.x, b.y-a.y);
  35. }
  36.  
  37. double dist(point a, point b){
  38.     return hypot(a.x-b.x, a.y-b.y);
  39. }
  40.  
  41. double perimetro(vector<point> &poli){
  42.     double ans = 0.0;
  43.     for(int i = 0; i < poli.size()-1; i++){
  44.         ans += dist(poli[i], poli[i+1]);
  45.     }
  46.  
  47.     return ans;
  48. }
  49. double area(vector<point> &poli){
  50.     double ans = 0.0;
  51.  
  52.     for(int i = 0; i < poli.size()-1; i++){
  53.         ans += (poli[i].x*poli[i+1].y -
  54.                 poli[i].y*poli[i+1].x);
  55.     }
  56.  
  57.     return fabs(ans)/2.0;
  58. }
  59.  
  60. // produto escalar
  61. double pe(vetor a, vetor b){
  62.     return a.x*b.x + a.y*b.y;
  63. }
  64.  
  65. //produto vetorial
  66. double pv(vetor a, vetor b){
  67.     return a.x*b.y - a.y*b.x;
  68. }
  69.  
  70. // diz se o ponto o esta a esquerda da linha lr;
  71. // mesma orientação
  72. double inLeft(point o, point l, point r){
  73.     return pv(toVetor(o, l), toVetor(o, r)) > 0;
  74. }
  75. // |v|
  76. double norma(vetor v){
  77.     return v.x*v.x + v.y*v.y;
  78. }
  79.  
  80. // retorna o angulo AoB
  81. double angulo(point a, point o, point b){
  82.     vetor oa = toVetor(o, a), ob = toVetor(o, b);
  83.     return acos(pe(oa, ob)/sqrt(norma(oa)*norma(ob)));
  84. }
  85.  
  86. // retorna se o ponto o esta na linha lr
  87. bool colinear(point o, point l, point r){
  88.     return pv(toVetor(o, l), toVetor(o, r)) < EPS;
  89. }
  90.  
  91. bool isConvex(vector<point> &poli){
  92.     int sz = poli.size();
  93.     if(sz <= 3) return false;
  94.     bool isLeft = inLeft(poli[0], poli[1], poli[2]);
  95.     for(int i = 1; i < sz-1; i++){
  96.         if(inLeft(poli[i], poli[i+1], poli[(i+2) == sz? 1: (i+2)]) != isLeft) return false;
  97.     }
  98.  
  99.     return true;
  100. }
  101.  
  102. bool inPolygon(point pt, vector<point> &poli){
  103.     if(poli.size() == 0) return false;
  104.     double sum = 0;
  105.     for(int i = 0; i < poli.size()-1; i++){
  106.         if(inLeft(pt, poli[i], poli[i+1])){
  107.             sum += angulo(poli[i], pt, poli[i+1]);
  108.         }else sum -= angulo(poli[i], pt, poli[i+1]);
  109.     }
  110.  
  111.     return fabs(fabs(sum)-2*PI) < EPS;
  112. }
  113.  
  114. point pivot;
  115. bool compare(point a, point b){
  116.     if(colinear(pivot, a, b)){
  117.         return dist(pivot, a) < dist(pivot, b);
  118.     }
  119.  
  120.     double pax = a.x-pivot.x, pay = a.y-pivot.y;
  121.     double pbx = b.x-pivot.x, pby = b.y-pivot.y;
  122.  
  123.     return (atan2(pay,pax)-atan2(pby, pbx)) < 0;
  124. }
  125. vector<point> CH(vector<point> &poli){
  126.     int n = poli.size();
  127.     if(n <= 3){
  128.         if(!(poli[0] == poli[n-1])) poli.push_back(poli[0]);
  129.         return poli;
  130.     }
  131.  
  132.     // acha o melhor ponto
  133.     // o menor Y com o maior X
  134.     int ini = 0;
  135.     for(int i = 0; i < n; i++){
  136.         if(poli[i].y < poli[ini].y ||
  137.            ((poli[i].y == poli[ini].y) && poli[i].x > poli[ini].x)) ini = i;
  138.     }
  139.  
  140.     point aux = poli[0];
  141.     poli[0] = poli[ini];
  142.     poli[ini] = aux;
  143.  
  144.     pivot = poli[0];
  145.     sort(++poli.begin(), poli.end(), compare);
  146.  
  147.     vector<point> ans;
  148.     ans.push_back(poli[n-1]);
  149.     ans.push_back(poli[0]);
  150.     ans.push_back(poli[1]);
  151.  
  152.     int idx = 2;
  153.     while(idx < n){
  154.         int last = ans.size()-1;
  155.         if(inLeft(ans[last-1], ans[last], poli[idx])){
  156.             ans.push_back(poli[idx++]);
  157.         }else ans.pop_back();
  158.     }
  159.  
  160.     return ans;
  161. }
  162. int main(){
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement