一、计算几何概述
计算几何研究几何对象的算法表示与操作,面试中常考:
- 点与向量的基本运算
- 线段相交判断
- 凸包算法
- 面积与包含关系
核心工具:叉积(Cross Product)——一个公式解决 80% 的几何题。
二、基本工具
2.1 点与向量
class Point {
double x, y;
Point(double x, double y) { this.x = x; this.y = y; }
}2.2 向量运算
// 向量减法:A→B = B - A
static Point subtract(Point b, Point a) {
return new Point(b.x - a.x, b.y - a.y);
}
// 点积:a·b = |a||b|cos θ
static double dot(Point a, Point b) {
return a.x * b.x + a.y * b.y;
}
// 叉积:a×b = |a||b|sin θ(有符号)
static double cross(Point a, Point b) {
return a.x * b.y - a.y * b.x;
}