Guest User

Graham scan

a guest
Jun 3rd, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<conio.h>
  4.  
  5. struct Point
  6. {
  7.     int x,y;  
  8. };
  9.  
  10. int direction(Point p0,Point p1,Point p2)
  11. {
  12.     return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
  13. }
  14.  
  15. int distSquared(Point a,Point b)
  16. {
  17.     return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y);
  18. }
  19.  
  20. bool comp(Point p0,Point a,Point b)
  21. {
  22.      int d = direction(p0,a,b);
  23.      if(d != 0)
  24.           return d>0;
  25.      else
  26.          return distSquared(p0,a) > distSquared(p0,b);
  27. }
  28.  
  29. void heapify(int l,int r, std::vector<Point> &A)
  30. {
  31.      int i,j;
  32.      Point x;
  33.      bool isCorrect;
  34.      x = A[l];
  35.      i = l;
  36.      j = 2 * i;
  37.      isCorrect = false;
  38.      while(j <= r && !isCorrect)
  39.      {
  40.              if(j < r && comp(A[0],A[j],A[j+1]))
  41.                   j++;
  42.              if(comp(A[0],x,A[j]))
  43.              {
  44.                   A[i] = A[j];
  45.                   i = j;
  46.                   j = 2 * i;                
  47.              }
  48.              else
  49.                  isCorrect = true;
  50.      }
  51.      A[i] = x;
  52. }
  53.  
  54. void buildHeap(std::vector<Point> &A)
  55. {
  56.      int n = A.size() - 1;
  57.      for(int i = n/2;i >= 1;i--)
  58.              heapify(i,n,A);
  59. }
  60.  
  61. void heapSort(std::vector<Point> &A)
  62. {
  63.      Point x;
  64.      int n = A.size()-1;
  65.      buildHeap(A);
  66.      for(int i = n;i >= 2;i--)
  67.      {
  68.          x = A[1];
  69.          A[1] = A[i];
  70.          A[i] = x;
  71.          heapify(1,i - 1,A);
  72.      }
  73. }
  74.  
  75. int removeDup(std::vector<Point> &A)
  76. {
  77.     int i,prev,count,size;
  78.     size = A.size();
  79.     prev = 1;
  80.     for(i = 1;i < size;i++)
  81.     {
  82.           if(direction(A[0],A[i],A[prev]) != 0)
  83.           {
  84.              prev++;
  85.              A[prev]=A[i];                            
  86.           }
  87.     }
  88.     i = prev+1;
  89.     count = prev + 1;
  90.     return count;
  91. }
  92.  
  93. void Solve(std::vector<Point> &P,std::vector<Point> &S)
  94. {
  95.      int j,m,n,min;
  96.      Point temp;
  97.      n = P.size();
  98.      min = 0;
  99.      for(int i = 1;i < n;i++)
  100.              if(P[i].y < P[min].y || (P[i].y == P[min].y && P[i].x < P[min].x))
  101.                        min = i;
  102.      temp = P[0];
  103.      P[0] = P[min];
  104.      P[min] = temp;
  105.      heapSort(P);
  106.      while(!S.empty())
  107.         S.pop_back();
  108.      m = removeDup(P);
  109.      while(!S.empty())
  110.         S.pop_back();
  111.      try
  112.      {
  113.          S.push_back(P[0]);
  114.          S.push_back(P[1]);
  115.          S.push_back(P[2]);
  116.          for(int i = 3;i < m;i++)
  117.          {
  118.              while(direction(S[S.size()-2],S[S.size()-1],P[i]) <= 0)
  119.                  S.pop_back();
  120.              S.push_back(P[i]);                                                                                                  
  121.          }
  122.          S.push_back(P[0]);            
  123.      }
  124.      catch(...)
  125.      {
  126.                
  127.      }
  128. }
  129.  
  130. int main()
  131. {
  132.     int n,countA,countS;
  133.     Point P;
  134.     char esc;    
  135.     do
  136.     {
  137.         std::vector<Point> A,S;
  138.         std::cout<<"Ile punktow wczytac \n";
  139.         std::cin>>n;
  140.         for(int k = 0;k < n;k++)
  141.         {
  142.             std::cout<<"P["<<k<<"]=";    
  143.             std::cin>>P.x>>P.y;
  144.             A.push_back(P);    
  145.          }
  146.          Solve(A,S);
  147.          countA = A.size();
  148.          countS = S.size();
  149.          std::cout<<"Wszystkie punkty \n";
  150.          for(int k = 0;k < countA;k++)
  151.              std::cout<<"("<<A[k].x<<" , "<<A[k].y<<") ";
  152.           std::cout<<"\n";
  153.           std::cout<<"Punkty na otoczce \n";
  154.          for(int k = 0;k < countS;k++)
  155.              std::cout<<"("<<S[k].x<<" , "<<S[k].y<<") ";
  156.          std::cout<<"\n";
  157.          while(!A.empty())
  158.               A.pop_back();
  159.          while(!S.empty())
  160.               S.pop_back();              
  161.         esc = getch();
  162.     }
  163.     while(esc != 27);
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment