|
| 1 | +#include<bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | +#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) |
| 4 | +#define md 1000000007 |
| 5 | +#define ll long long int |
| 6 | +#define vi vector<int> |
| 7 | +#define vll vector<i64> |
| 8 | +#define pb push_back |
| 9 | +#define all(c) (c).begin(),(c).end() |
| 10 | +template< class T > T max2(const T &a,const T &b) {return (a < b ? b : a);} |
| 11 | +template< class T > T min2(const T &a,const T &b) {return (a > b ? b : a);} |
| 12 | +template< class T > T max3(const T &a, const T &b, const T &c) { return max2(a, max2(b, c)); } |
| 13 | +template< class T > T min3(const T &a, const T &b, const T &c) { return min2(a, min2(b, c)); } |
| 14 | +template< class T > T gcd(const T a, const T b) { return (b ? gcd<T>(b, a%b) : a); } |
| 15 | +template< class T > T lcm(const T a, const T b) { return (a / gcd<T>(a, b) * b); } |
| 16 | +template< class T > T mod(const T &a, const T &b) { return (a < b ? a : a % b); } |
| 17 | +typedef pair<ll,ll> pi; |
| 18 | +typedef struct |
| 19 | +{ |
| 20 | + int x; |
| 21 | + int y; |
| 22 | +}point; |
| 23 | +int crossprod(point points[],int a,int b,int c)//This function calculates the crossproduct of the given two vectors . one vector is ab and other is ac. |
| 24 | +{ |
| 25 | + point current = points[a]; |
| 26 | + point target = points[b]; |
| 27 | + point temp = points[c]; |
| 28 | + int y1 = target.y-current.y; |
| 29 | + int x1 = target.x-current.x; |
| 30 | + int x2 = temp.x-current.x; |
| 31 | + int y2 = temp.y-current.y; |
| 32 | + return y2*x1-x2*y1; |
| 33 | +} |
| 34 | +int closer(point points[],int a,int b,int c)//This function calculates the closer of the given two points from a and returns the index of the closer one. |
| 35 | +{ |
| 36 | + point current = points[a]; |
| 37 | + point target = points[b]; |
| 38 | + point temp = points[c]; |
| 39 | + double dist1 = sqrt((current.x-target.x)*(current.x-target.x)-(current.y-target.y)*(current.y-target.y)); |
| 40 | + double dist2 = sqrt((current.x-temp.x)*(current.x-temp.x)-(current.y-temp.y)*(current.y-temp.y)); |
| 41 | + if(dist1<dist2) |
| 42 | + return b; |
| 43 | + else |
| 44 | + return c; |
| 45 | +} |
| 46 | +int main() |
| 47 | +{ |
| 48 | + int n;//no of points |
| 49 | + cin>>n; |
| 50 | + point points[n]; |
| 51 | + for(int i=0;i<n;i++) |
| 52 | + { |
| 53 | + cin>>points[i].x>>points[i].y; |
| 54 | + } |
| 55 | + //now we look for the leftmost point |
| 56 | + int index=0,mini=100000; |
| 57 | + for(int i=0;i<n;i++) |
| 58 | + { |
| 59 | + if(points[i].x<mini) |
| 60 | + { |
| 61 | + mini=points[i].x; |
| 62 | + index=i; |
| 63 | + } |
| 64 | + } |
| 65 | + set<int> result; |
| 66 | + |
| 67 | + result.insert(index);//we add the left most point in the result array |
| 68 | + int current = index;//now our index becomes the index to the current point |
| 69 | + vi colinear; |
| 70 | + while(true) |
| 71 | + { |
| 72 | + int target = 0 ;//consider the starting target to be point with index zero |
| 73 | + for(int i=1;i<n;i++) |
| 74 | + { |
| 75 | + |
| 76 | + if(current==i)//if the current point and the point we are going to compare are the same then nothing below should be executed |
| 77 | + { |
| 78 | + continue; |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + int val = crossprod(points,current,target,i); |
| 83 | + if(val > 0)//If the cross prod value is greater than zero than this means that we the point i is in the left side of the line segement that passes through the target and current so now the target needs to be changed. |
| 84 | + { |
| 85 | + |
| 86 | + target = i; |
| 87 | + colinear.clear();//we also need to clear the colinear array as the target itself is changed. |
| 88 | + } |
| 89 | + if(val == 0) |
| 90 | + { |
| 91 | + int close = closer(points,current,target,i);//This gives the index of the closer point. |
| 92 | + if(close == target) |
| 93 | + target = i; |
| 94 | + colinear.pb(close); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + int len = colinear.size(); |
| 99 | + for(int k = 0;k<len;k++) |
| 100 | + |
| 101 | + colinear.clear(); |
| 102 | + if(target == index)//we should end our search if our target becomes equal to the first index we added in the result |
| 103 | + { |
| 104 | + break; |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + |
| 109 | + result.insert(target); |
| 110 | + current = target; |
| 111 | + } |
| 112 | + } |
| 113 | +int len = result.size(); |
| 114 | + set<int> :: iterator i= result.begin(); |
| 115 | +for(;i!=result.end();++i) |
| 116 | +{ |
| 117 | + cout<<points[(*i)].x<<" "<<points[(*i)].y<<endl; |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +} |
0 commit comments