Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<conio.h>
- struct Point
- {
- int x,y;
- };
- int direction(Point p0,Point p1,Point p2)
- {
- return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
- }
- int distSquared(Point a,Point b)
- {
- return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y);
- }
- bool comp(Point p0,Point a,Point b)
- {
- int d = direction(p0,a,b);
- if(d != 0)
- return d>0;
- else
- return distSquared(p0,a) > distSquared(p0,b);
- }
- void heapify(int l,int r, std::vector<Point> &A)
- {
- int i,j;
- Point x;
- bool isCorrect;
- x = A[l];
- i = l;
- j = 2 * i;
- isCorrect = false;
- while(j <= r && !isCorrect)
- {
- if(j < r && comp(A[0],A[j],A[j+1]))
- j++;
- if(comp(A[0],x,A[j]))
- {
- A[i] = A[j];
- i = j;
- j = 2 * i;
- }
- else
- isCorrect = true;
- }
- A[i] = x;
- }
- void buildHeap(std::vector<Point> &A)
- {
- int n = A.size() - 1;
- for(int i = n/2;i >= 1;i--)
- heapify(i,n,A);
- }
- void heapSort(std::vector<Point> &A)
- {
- Point x;
- int n = A.size()-1;
- buildHeap(A);
- for(int i = n;i >= 2;i--)
- {
- x = A[1];
- A[1] = A[i];
- A[i] = x;
- heapify(1,i - 1,A);
- }
- }
- int removeDup(std::vector<Point> &A)
- {
- int i,prev,count,size;
- size = A.size();
- prev = 1;
- for(i = 1;i < size;i++)
- {
- if(direction(A[0],A[i],A[prev]) != 0)
- {
- prev++;
- A[prev]=A[i];
- }
- }
- i = prev+1;
- count = prev + 1;
- return count;
- }
- void Solve(std::vector<Point> &P,std::vector<Point> &S)
- {
- int j,m,n,min;
- Point temp;
- n = P.size();
- min = 0;
- for(int i = 1;i < n;i++)
- if(P[i].y < P[min].y || (P[i].y == P[min].y && P[i].x < P[min].x))
- min = i;
- temp = P[0];
- P[0] = P[min];
- P[min] = temp;
- heapSort(P);
- while(!S.empty())
- S.pop_back();
- m = removeDup(P);
- while(!S.empty())
- S.pop_back();
- try
- {
- S.push_back(P[0]);
- S.push_back(P[1]);
- S.push_back(P[2]);
- for(int i = 3;i < m;i++)
- {
- while(direction(S[S.size()-2],S[S.size()-1],P[i]) <= 0)
- S.pop_back();
- S.push_back(P[i]);
- }
- S.push_back(P[0]);
- }
- catch(...)
- {
- }
- }
- int main()
- {
- int n,countA,countS;
- Point P;
- char esc;
- do
- {
- std::vector<Point> A,S;
- std::cout<<"Ile punktow wczytac \n";
- std::cin>>n;
- for(int k = 0;k < n;k++)
- {
- std::cout<<"P["<<k<<"]=";
- std::cin>>P.x>>P.y;
- A.push_back(P);
- }
- Solve(A,S);
- countA = A.size();
- countS = S.size();
- std::cout<<"Wszystkie punkty \n";
- for(int k = 0;k < countA;k++)
- std::cout<<"("<<A[k].x<<" , "<<A[k].y<<") ";
- std::cout<<"\n";
- std::cout<<"Punkty na otoczce \n";
- for(int k = 0;k < countS;k++)
- std::cout<<"("<<S[k].x<<" , "<<S[k].y<<") ";
- std::cout<<"\n";
- while(!A.empty())
- A.pop_back();
- while(!S.empty())
- S.pop_back();
- esc = getch();
- }
- while(esc != 27);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment