classPoint{ int x, y; Point() {} Point(int a, int b) { x = a; y = b; } }
classSeg{ Point s, t; Seg() {} Seg(Point a, Point b) { s = a; t = b; } Seg(int x1, int y1, int x2, int y2) { s = new Point(x1, y1); t = new Point(x2, y2); } }
点与直线的位置关系:点在/不在直线上 设点为P,直线上有两点A B,判断点P在直线AB上的依据是:(P - A) × (P - B) = 0
// 向量AB 和向量AC 的叉积 int crossProduct(Point a, Point b, Point c) { int x1 = b.x - a.x; int y1 = b.y - a.y; int x2 = c.x - a.x; int y2 = c.y - a.y; return x1 * y2 - x2 * y1; }
给定一些点,求凸包:
(极角)排序(数组模拟/栈)
Graham核心算法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Point data[n +1] // 极角排序(前提:data[0] 为基础点,位置在左下) // Arrays.sort(data, 1, n, new Cmp());
classCmpimplementsComparator<Point> {
@Override publicintcompare(Point a, Point b){ if (crossProduct(data[0], a, b) > 0) return -1; elseif (crossProduct(data[0], a, b) < 0) return1; elsereturn0; } }