1 // Implementation of Monotone Chain Convex Hull algorithm.
13 point(int a
, int b
){ x
= a
;y
= b
;}
14 bool operator <(const point
&p
) const {
15 return x
< p
.x
|| (x
== p
.x
&& y
< p
.y
);
20 // Return a positive value, if OAB makes a counter-clockwise turn,
21 // negative for clockwise turn, and zero if the points are collinear.
22 int cross(const point
&O
, const point
&A
, const point
&B
)
24 return (A
.x
- O
.x
) * (B
.y
- O
.y
) - (A
.y
- O
.y
) * (B
.x
- O
.x
);
27 // Returns a list of points on the convex hull in counter-clockwise order.
28 // Note: the last point in the returned list is the same as the first one.
29 vector
<point
> convexHull(vector
<point
> P
)
31 int n
= P
.size(), k
= 0;
33 // Sort points lexicographically
34 sort(P
.begin(), P
.end());
36 for (int i
= 0; i
< n
; i
++) {
37 while (k
>= 2 && cross(H
[k
-2], H
[k
-1], P
[i
]) <= 0) k
--;
41 for (int i
= n
-2, t
= k
+1; i
>= 0; i
--) {
42 while (k
>= t
&& cross(H
[k
-2], H
[k
-1], P
[i
]) <= 0) k
--;
48 ///////////////////////// hiiiiper rapida :)
51 void convexHull(point P
[], int n
)
54 // Sort points lexicographically
57 for (int i
= 0; i
< n
; i
++) {
58 while (k
>= 2 && cross(H
[k
-2], H
[k
-1], P
[i
]) <= 0) k
--;
62 for (int i
= n
-2, t
= k
+1; i
>= 0; i
--) {
63 while (k
>= t
&& cross(H
[k
-2], H
[k
-1], P
[i
]) <= 0) k
--;