1 monotone chain convex hull (monotonic chain convex hull) algorithm pseudocode: 2 // input: A point set on the plane P 3 // Point Set P is sorted progressively after X 4 // M indicates a total of a [I = 0... m] points, ANS is the required point; 5 struct p 6 {7 int X, Y; 8 friend int operator <(p a, p B) 9 {10 if (. x <B. x) | (. X = B. X &. Y <B. y) 11 return 1; 12 Return 0; 13} 14} A [M + 10], ANS [M + 10]; 15 // judge whether the third point is on the left or right side of the Line 16 // when Judge (), the return value is less than or equal to 0, which indicates on the right side, we are always looking for 17 double judge (p a, p B, p c) 18 {19 Return (B. x-a.x) * (C. y-a.y)-(C. x-a.x) * (B. y-a.y); 20} 21 // construct the lower convex hull, from left to right, from the following through 22 int k1 = 0; 23 for (INT I = 0; I <m; I ++) // lower convex hull 24 {25 while (K1> 1 & judge (ANS [k1-2], ANS [k1-1], a [I]) <= 0) 26 {27 K1 --; 28} 29 ans [K1 ++] = A [I]; 30} 31 // build the upper convex hull, from right to left, from the above through 32 int k2 = k1; 33 for (INT I = s-1; I> = 0; I --) // upper convex hull 34 {35 while (K1> k2 & judge (ANS [k1-2], ANS [k1-1], a [I]) <= 0) 36 {37 K1 --; 38} 39 ans [K1 ++] = A [I]; 40} 41 K1 --; // minus the start point, because the start point has been entered twice;
Convex Hull: nyoj 78
Monotone chain convex hull (monotonous chain convex hull)