forked from Ashishgup1/Competitive-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Convex Hull (Graham's Scan).cpp
109 lines (86 loc) · 1.86 KB
/
Convex Hull (Graham's Scan).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
struct point //Replace double with int if not required
{
double x, y;
point () {}
point(int x, int y) : x(x), y(y) {}
void operator =(const point &p)
{
x=p.x, y=p.y;
}
bool operator <(const point&p)
{
if(x==p.x)
return y<p.y;
return x<p.x;
}
point operator +(const point&p) const
{
point pt(x + p.x, y + p.y);
return pt;
}
point operator -(const point&p) const
{
point pt(x - p.x, y - p.y);
return pt;
}
double crossProduct(const point &p) const
{
return x * p.y - y * p.x;
}
int dotProduct(const point &p) const
{
return x * p.x + y * p.y;
}
double dist()
{
return x*x + y*y;
}
};
bool comp(point &p1, point &p2)
{
if(p1.x!=p2.x)
return p1.x<p2.x;
return p1.y<p2.y;
}
bool cw(point &a, point &b, point &c)
{
int area=a.x*(b.y-c.y) + b.x*(c.y-a.y) + c.x*(a.y-b.y);
return area<0;
}
bool ccw(point &a, point &b, point &c)
{
int area=a.x*(b.y-c.y) + b.x*(c.y-a.y) + c.x*(a.y-b.y);
return area>0;
}
vector<point> convex_hull(vector<point> &v)
{
if(v.size()==1)
return v;
sort(v.begin(), v.end(), comp);
point p1=v[0], p2=v.back();
vector<point> up, down;
up.push_back(p1);
down.push_back(p1);
for(int i=1;i<v.size();i++)
{
if(i==v.size()-1 || cw(p1, v[i], p2))
{
while(up.size()>=2 && !cw(up[up.size()-2], up[up.size()-1], v[i]))
up.pop_back();
up.push_back(v[i]);
}
if(i==v.size()-1 || ccw(p1, v[i], p2))
{
while(down.size()>=2 && !ccw(down[down.size()-2], down[down.size()-1], v[i]))
down.pop_back();
down.push_back(v[i]);
}
}
for(int i=down.size()-2;i>0;i--)
up.push_back(down[i]);
return up;
}
//Problem 1 (Polygon Congruence): http://codeforces.com/contest/1017/problem/E
//Solution 1: http://codeforces.com/contest/1017/submission/41401690
//Problem 2 and Solution: http://codeforces.com/gym/101606/submission/41541222
//Problem 3: https://codeforces.com/contest/1300/problem/E