import java.util.*;
public class ConvexHull {
private static Point[] p;
private static Point p1;
private static Point[] CH;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
p = new Point[N];
int firstIndex = 0;
double firstX = sc.nextDouble();
double firstY = sc.nextDouble();
p[0] = new Point(firstX, firstY);
for (int i = 1; i < N; i++) {
double x = sc.nextDouble();
double y = sc.nextDouble();
p[i] = new Point(x, y);
if (y < firstY || (y == firstY && x < firstX)) {
firstY = y;
firstX = x;
firstIndex = i;
}
}
Point temp = p[0];
p[0] = p[firstIndex];
p[firstIndex] = temp;
p1 = p[0];
Arrays.sort(p, 1, p.length, new MyComparator());
grahamScan();
}
private static void grahamScan() {
CH = new Point[p.length];
CH[0] = p[0];
CH[1] = p[1];
CH[2] = p[2];
int top = 2;
for (int k = 3; k < p.length; ++k) {
while (crossProduct(CH[top - 1], CH[top], p[k]) <= 0) {
if (top-- == 0)// 防止所有点共线
break;
}
CH[++top] = p[k];
}
for (int i = 0; i <= top; i++) {
System.out.println(CH[i].x + "\t" + CH[i].y);
}
}
private static class MyComparator implements Comparator<Point> {
public int compare(Point o1, Point o2) {
double cross = crossProduct(p1, o1, o2);
if (cross < 0.0000001 && cross > -0.0000001) {
return (o1.y - o2.y) > 0 ? 1 : -1;
}
return cross > 0 ? -1 : 1;
}
}
public static double crossProduct(Point pp, Point o1, Point o2) {
return (o1.x - pp.x) * (o2.y - pp.y) - (o2.x - pp.x) * (o1.y - pp.y);
}
private static class Point {
public Point(double x2, double y2) {
this.x = x2;
this.y = y2;
}
double x, y;
}
}
测试数据:
10
1 -2
1 2
5 3
4 5
2 -4
6 -6
7 0
10 -3
9 4
8 -1
6.0 -6.0
10.0 -3.0
9.0 4.0
4.0 5.0
1.0 2.0
1.0 -2.0
2.0 -4.0