Skip to content

Commit 6348e01

Browse files
authored
Merge pull request #232 from Jatin86400/convex-hull
added algorithm for convex hull by jarvis march
2 parents 15f786f + 195bb3f commit 6348e01

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Convex Hull (Jarvis March)
2+
3+
Given a set of points in the plane. the convex hull of the set is the smallest convex polygon that contains all the points of it.
4+
5+
![alt text](http://www.geeksforgeeks.org/wp-content/uploads/convexHull1.png)
6+
7+
The idea of Jarvis’s Algorithm is simple, we start from the leftmost point (or point with minimum x coordinate value) and we keep wrapping points in counterclockwise direction.
8+
The big question is, given a point p as current point, how to find the next point in output? The idea is to use orientation() here.
9+
Next point is selected as the point that beats all other points at counterclockwise orientation, i.e., next point is q if for any other point r, we have “orientation(p, r, q) = counterclockwise”. Following is the detailed algorithm.
10+
11+
1) Initialize p as leftmost point.
12+
2) Do following while we don’t come back to the first (or leftmost) point.
13+
…..a) The next point q is the point such that the triplet (p, q, r) is counterclockwise for any other point r.
14+
…..b) next[p] = q (Store q as next of p in the output convex hull).
15+
…..c) p = q (Set p as q for next iteration).
16+
17+
The below Gif may help:
18+
19+
![alt text](https://upload.wikimedia.org/wikipedia/commons/thumb/9/9c/Animation_depicting_the_gift_wrapping_algorithm.gif/330px-Animation_depicting_the_gift_wrapping_algorithm.gif)

0 commit comments

Comments
 (0)