计算几何
libohan0905
·
·
个人记录
计算几何——从入门到跳楼。。。
- 凸包(二维)
- 旋转♂卡壳
- 最小圆の覆盖
- 半平面の交
- 平面最近の点对
向量的基本运算
向量的点积
向量的点积,也叫数量积、内积。
几何意义为一个向量在另一个向量上的投影再乘上第二个向量的模长,是个实数。
对于 \vec{a}=(x_1,y_1),\vec{b}=(x_2,y_2)。
计算式为 \vec{a}\cdot\vec{b}=|\vec{a}||\vec{b}|\cos\theta(\theta=\left<\vec{a},\vec{b}\right>)=x_1x_2+y_1y_2
向量的数量积具有交换律。
向量的叉积
向量的叉积,也是一种外积。
几何意义是两向量由平行四边形法则围成的面积。外积是一个向量。
对于 \vec{a}=(x_1,y_1),\vec{b}=(x_2,y_2)。
计算式为 \vec{a}\times\vec{b}=|\vec{a}||\vec{b}|\sin\theta=x_1y_2-x_2y_1
因为有谁转向谁的区别,所以外积没有交换律。
利用叉积判断向量转移的方向。
- [P1863 独眼兔](https://www.luogu.com.cn/problem/P1863)
- [UVA11626 Convex Hull](https://www.luogu.com.cn/problem/UVA11626)
```cpp
struct vec{
double x,y;
inline vec operator+(vec &p)const {return (vec) {x+p.x,y+p.y};}
inline vec operator-(vec &p)const {return (vec) {x-p.x,y-p.y};}
};
inline double operator*(vec a,vec b){return a.x*b.y-a.y*b.x;}//叉积
inline double operator^(vec a,vec b){return a.x*b.x+a.y*b.y;}//点积
struct point{
double x,y;
inline vec operator+(point &p)const {return (vec) {x+p.x,y+p.y};}
inline vec operator-(point &p)const {return (vec) {x-p.x,y-p.y};}
}p[N];
inline double dis(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
inline bool operator < (const point &a,const point &b){
return a.x==b.x?a.y<b.y:a.x<b.x;
}
```
### 三角剖分
把多边形剖分成多个三角形,可以用来求任意多边形的面积。
#### 把相邻每两个顶点与原点构成的向量的叉积的数值的一半依次累加起来,就能得到多边形的面积。
$$S_{ABCDEF}=\frac{\overrightarrow{OA}\times \overrightarrow{OB}+\overrightarrow{OB}\times \overrightarrow{OC}+\dots +\overrightarrow{OF}\times \overrightarrow{OA}}2$$
- [P1183 多边形的面积](https://www.luogu.com.cn/problem/P1183)
- [P2785 物理1(phsic1)- 磁通量](https://www.luogu.com.cn/problem/P2785)
### 线段相交
利用叉积判断与直线或线段的相交情况。
这种好背。
```cpp
struct point{
double x,y;
inline point operator-(point &p)const {return (point) {x-p.x,y-p.y};}
};
double cross(point a,point b){
return a.x*b.y-b.x*a.y;
}
bool isok(point a1,point a2,point b1,point b2){
double c1=cross(a2-a1,b1-a1),c2=cross(a2-a1,b2-a1);
double d1=cross(b2-b1,a2-b1),d2=cross(b2-b1,a1-b1);
return c1*c2<=0&&d1*d2<=0;
}
```
- [P1355 神秘大三角](https://www.luogu.com.cn/problem/P1355)
- [P1354 房间最短路问题](https://www.luogu.com.cn/problem/P1354)
每个点暴力连边,判断是否和墙相交(交一个点没事)。
- 双倍经验:[UVA393 The Doors](https://www.luogu.com.cn/problem/UVA393)
### 向量旋转
使用三角函数让点绕原点旋转。
可以自己推式子,也可以直接背公式。
$\vec a=(x\cos\alpha-y\sin\alpha,y\cos\alpha+x\sin\alpha)
如果绕某个点旋转再加上它的坐标即可。
inline point rotate(point a,point p,double t) {
double c=cos(t), s=sin(t);
return (point) {(a.x*c-a.y*s+p.x),(a.x*s+a.y*c+p.y)};
}
二维凸包
P2742 [USACO5.1]圈奶牛Fencing the Cows /【模板】二维凸包
即用最小多边形围住所有点的图形,显然凸包上所有点就是原有点中的。
### Graham
- 极角排序。
- 依次判断和上一个点相比是否右移,是则pop
- 入栈
排序时,如果遇到共线向量(且方向相同),则取靠近可行域的一个。
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
const double eps=1e-9;
struct vec{
double x,y;
inline vec operator+(vec &p)const {return (vec) {x+p.x,y+p.y};}
inline vec operator-(vec &p)const {return (vec) {x-p.x,y-p.y};}
};
inline double operator*(vec a,vec b){return a.x*b.y-a.y*b.x;}//叉积
struct point{
double x,y;
inline vec operator+(point &p)const {return (vec) {x+p.x,y+p.y};}
inline vec operator-(point &p)const {return (vec) {x-p.x,y-p.y};}
}p[N];
inline double dis(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp(point a,point b) {
double tmp=(a-p[1])*(b-p[1]);
if(tmp>0) return 1;
if(tmp==0&&dis(p[1],a)<dis(p[1],b)) return 1;
return 0;
}
inline double check(vec a,vec b){
double tmp=a*b;
if(fabs(tmp)<eps) return sqrt(a.x*a.x+a.y*a.y)<=sqrt(b.x*b.x+b.y*b.y);
return tmp<=0;
}
int n,st[N],top;
double Graham() {
for(int i=2; i<=n; i++)
if(p[i].y<p[1].y||(p[i].y==p[1].y&&p[i].x<p[1].x))
swap(p[1],p[i]);
sort(p+2,p+n+1,cmp);
st[1]=1;
top=1;
for(int i=2; i<=n; i++) {
while(check(p[st[top]]-p[st[top-1]],p[i]-p[st[top-1]])&&top>1)
--top;
st[++top]=i;
}
st[top+1]=1;
double ans=0;
for(int i=1; i<=top; i++)
ans+=dis(p[st[i]],p[st[i+1]]);
return ans;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lf %lf",&p[i].x,&p[i].y);
}
printf("%.2lf",Graham());
return 0;
}
```
### Andrew
分别求出上下凸壳,特定场合更方便,实际跑下来比 Graham 快。
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=200005;
const double eps=1e-9;
struct vec{
double x,y;
inline vec operator+(vec &p)const {return (vec) {x+p.x,y+p.y};}
inline vec operator-(vec &p)const {return (vec) {x-p.x,y-p.y};}
};
inline double operator*(vec a,vec b){return a.x*b.y-a.y*b.x;}//叉积
struct point{
double x,y;
inline vec operator+(point &p)const {return (vec) {x+p.x,y+p.y};}
inline vec operator-(point &p)const {return (vec) {x-p.x,y-p.y};}
}p[N];
inline double dis(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
inline bool operator < (const point &a,const point &b){
return a.x==b.x?a.y<b.y:a.x<b.x;
}
inline double check(vec a,vec b){
double tmp=a*b;
if(fabs(tmp)<eps) return sqrt(a.x*a.x+a.y*a.y)<=sqrt(b.x*b.x+b.y*b.y);
return tmp<=0;
}
inline point rotate(point a,point p,double t) {
double c=cos(t), s=sin(t);
return (point) {(a.x*c-a.y*s+p.x),(a.x*s+a.y*c+p.y)};
}
int n,st[N],top;
double Andrew(){
sort(p+1,p+n+1);
top=0;
for(int i=1;i<=n;i++){
while(check(p[st[top]]-p[st[top-1]],p[i]-p[st[top-1]])&&top>1) top--;
st[++top]=i;
}
int tmp=top;
for(int i=n-1;i>=1;i--){
while(check(p[st[top]]-p[st[top-1]],p[i]-p[st[top-1]])&&top>tmp) top--;
st[++top]=i;
}
double ans=0;
for(int i=1;i<top;i++)
ans+=dis(p[st[i]],p[st[i+1]]);
return ans;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lf %lf",&p[i].x,&p[i].y);
}
printf("%.2lf",Andrew());
return 0;
}
```
### 习题
- [P2116 城墙](https://www.luogu.com.cn/problem/P2116)
简单应用
- [UVA1303 Wall](https://www.luogu.com.cn/problem/UVA1303)
同上一题
- [P3829 [SHOI2012]信用卡凸包](https://www.luogu.com.cn/problem/P3829)
旋转
- [P3680 [CERC2016]凸轮廓线 Convex Contour](https://www.luogu.com.cn/problem/P3680)
请放飞思想乱搞!
---
#### 前置芝士:二维凸包
## 旋转卡壳
### [P1452 [USACO03FALL]Beauty Contest G /【模板】旋转卡壳](https://www.luogu.com.cn/problem/P1452)
旋转卡壳是 $\mathcal{O}(n)$ 的,但它要预处理凸包。。。
它可以寻找平面最远点对(最近点对在下面)。
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=200005;
const double eps=1e-9;
struct vec{
double x,y;
inline vec operator+(vec &p)const {return (vec) {x+p.x,y+p.y};}
inline vec operator-(vec &p)const {return (vec) {x-p.x,y-p.y};}
};
inline double operator*(vec a,vec b){return a.x*b.y-a.y*b.x;}
struct point{
double x,y;
inline vec operator+(point &p)const {return (vec) {x+p.x,y+p.y};}
inline vec operator-(point &p)const {return (vec) {x-p.x,y-p.y};}
}p[N];
inline double dis(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
inline bool operator < (const point &a,const point &b){
return a.x==b.x?a.y<b.y:a.x<b.x;
}
inline double check(vec a,vec b){
double tmp=a*b;
if(fabs(tmp)<eps) return sqrt(a.x*a.x+a.y*a.y)<=sqrt(b.x*b.x+b.y*b.y);
return tmp<=0;
}
int n,st[N],top;
double Andrew(){
sort(p+1,p+n+1);
top=0;
for(int i=1;i<=n;i++){
while(check(p[st[top]]-p[st[top-1]],p[i]-p[st[top-1]])&&top>1) --top;
st[++top]=i;
}
int tmp=top;
for(int i=n-1;i>=1;i--){
while(check(p[st[top]]-p[st[top-1]],p[i]-p[st[top-1]])&&top>tmp) --top;
st[++top]=i;
}
double ans=0;
for(int i=1;i<top;i++)
ans+=dis(p[st[i]],p[st[i+1]]);
return ans;
}
double rotating_calipers(){
Andrew();
double ans=0;
int j=2;
for(int i=1; i<top; i++) {
ans=max(ans,dis(p[st[i]],p[st[i+1]]));
while((p[st[i+1]]-p[st[i]])*(p[st[j]]-p[st[i]])<(p[st[i+1]]-p[st[i]])*(p[st[j+1]]-p[st[i]])) {
++j;
if(j>top-1) j%=(top-1);
}
ans=max(ans,max(dis(p[st[i]],p[st[j]]),dis(p[st[i+1]],p[st[j]])));
}
return ans;
}
int main() {
scanf("%d",&n);
for(int i=1; i<=n; i++) {
scanf("%lf%lf",&p[i].x,&p[i].y);
}
double ans=rotating_calipers();
printf("%.0lf\n",ans*ans);
return 0;
}
```
#### 习题
- [P4166 [SCOI2007]最大土地面积](https://www.luogu.com.cn/problem/P4166)
在凸包上。旋转卡壳最远点再左右算
- [P3187 [HNOI2007]最小矩形覆盖](https://www.luogu.com.cn/problem/P3187)
肯定有一边在凸包上,再旋转卡壳求最远点,最左右点
- [UVA10173 Smallest Bounding Rectangle](https://www.luogu.com.cn/problem/UVA10173)
同上一题
---
## 最小圆覆盖
玄学算法。。。
### [P1742 最小圆覆盖](https://www.luogu.com.cn/problem/P1742)
理论上可以用模拟退火,不过精度肯定不会太高,估计很难草过去。。。
#### [最小圆覆盖算法总结](https://blog.csdn.net/m0_55982600/article/details/122900196)
首先你应该知道三点定圆。
那么我们可以列出式子:
$\begin{cases}(x_1-x)^2+(y_1-y)^2=r^2\\(x_2-x)^2+(y_2-y)^2=r^2\\(x_3-x)^2+(y_3-y)^2=r^2\end{cases}
通过抄袭大佬的式子 严谨的分析,我们可以得出:
x=\frac{(x_2^2+y_2^2-x_1^2-y_1^2)(y_3-y_1)-(x_3^2+y_3^2-x_1^2-y_1^2)(y_2-y_1)}{2(x_2-x_1)(y_3-y_1)-2(x_3-x_1)(y_2-y_1)}
y=\frac{(x_2^2+y_2^2-x_1^2-y_1^2)(x_3-x_1)-(x_3^2+y_3^2-x_1^2-y_1^2)(x_2-x_1)}{2(y_2-y_1)(x_3-x_1)-2(y_3-y_1)(x_2-x_1)}]
然后因为式子有些恶臭 复杂,使用代换。
void calc(Point p1,Point p2,Point p3) {
double a,b,c,d,e,f;
a=p2.y-p1.y;
b=p3.y-p1.y;
c=p2.x-p1.x;
d=p3.x-p1.x;
f=p3.x*p3.x+p3.y*p3.y-p1.x*p1.x-p1.y*p1.y;
e=p2.x*p2.x+p2.y*p2.y-p1.x*p1.x-p1.y*p1.y;
o.x=(a*f-b*e)/(2*a*d-2*b*c);
o.y=(d*e-c*f)/(2*a*d-2*b*c);
r=dis(o,p1);
}
这里使用到了增量法。即每次加一个点。
如果当前的枚举的点在圆内部,就不用管。
不然的话就重新枚举。枚举前面的点并取中点,距离的一半为半径,以这个圆再枚举前面的点,然后用三点组成的三角形的外切圆来更新当前的圆。
o=p[1],r=0;
for(int i=2;i<=n;i++){
if(dis(p[i],o)>r+eps){
o=p[i],r=0;
for(int j=1;j<i;j++){
if(dis(p[j],o)>r+eps){
o.x=(p[i].x+p[j].x)/2;
o.y=(p[i].y+p[j].y)/2;
r=dis(p[i],p[j])/2;//取中点
for(int k=1;k<j;k++){
if(dis(p[k],o)>r+eps){//k在圆外就以i,j,k三点组成的三角形的外切圆来更新
calc(p[i],p[j],p[k]);
}
}
}
}
}
}
诶,这三重循环什么鬼?
但是它复杂度是均摊 \mathcal{O}(n) 的! 证明
然后啊,因为这个算法的数据复杂度是均摊的,会被友好的出题人卡,所以我们还要再打乱一下出题人精心准备的数据。
random_shuffle(p+1,p+n+1);
双倍经验:P2533 [AHOI2012]信号塔
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
const double eps=1e-9;
int n;
struct Point {
double x,y;
} p[N],o;
double r;
double dis(Point a,Point b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void calc(Point p1,Point p2,Point p3) {
double a,b,c,d,e,f;
a=p2.y-p1.y;
b=p3.y-p1.y;
c=p2.x-p1.x;
d=p3.x-p1.x;
f=p3.x*p3.x+p3.y*p3.y-p1.x*p1.x-p1.y*p1.y;
e=p2.x*p2.x+p2.y*p2.y-p1.x*p1.x-p1.y*p1.y;
o.x=(a*f-b*e)/(2*a*d-2*b*c);
o.y=(d*e-c*f)/(2*a*d-2*b*c);
r=dis(o,p1);
}
int main() {
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
random_shuffle(p+1,p+n+1);
o=p[1],r=0;
for(int i=2;i<=n;i++){
if(dis(p[i],o)>r+eps){
o=p[i],r=0;
for(int j=1;j<i;j++){
if(dis(p[j],o)>r+eps){
o.x=(p[i].x+p[j].x)/2;
o.y=(p[i].y+p[j].y)/2;
r=dis(p[i],p[j])/2;//取中点
for(int k=1;k<j;k++){
if(dis(p[k],o)>r+eps){//k在圆外就以i,j,k三点组成的三角形的外切圆来更新
calc(p[i],p[j],p[k]);
}
}
}
}
}
}
printf("%.9lf\n",r);
printf("%.9lf %.9lf\n",o.x,o.y);
return 0;
}
半平面交
P4196 [CQOI2006]凸多边形 /【模板】半平面交
对半平面交算法正确性解释的探索
半平面交
半平面交是指多个半平面的交集。因为半平面是点集,所以点集的交集仍然是点集。在平面直角坐标系围成一个区域。
多边形的核
如果一个点集中的点与多边形上任意一点的连线与多边形没有其他交点,那么这个点集被称为多边形的核。
把多边形的每条边看成是首尾相连的向量,那么这些向量在多边形内部方向的半平面交就是多边形的核。
极角排序
话说这不应该是Graham里的东西吗?
C语言有一个库函数叫做 atan2(double y,double x),可以返回 \theta\in (-\pi,\pi],\theta =\arctan \frac{y}{x},即坐标原点为起点,指向 (x,y) 的射线在坐标平面上与 x 轴正方向之间的角的角度。
排序时,如果遇到共线向量(且方向相同),则取靠近可行域的一个。
维护单调队列
因为半平面交是一个凸多边形,所以需要维护一个凸壳。因为后来加入的只可能会影响最开始加入的或最后加入的边(此时凸壳连通),只需要删除队首和队尾的元素,所以需要用单调队列。
我们遍历排好序了的向量,并维护另一个交点数组。当单队中元素超过2个时,他们之间就会产生交点。
如果双端队列里后两条向量的交点在这条向量的右侧,如果上一个交点在这条向量表示的半平面交的异侧,那么上一条边就没有意义了。那么弹出双端队列的最后一条向量,直到满足要求为止。
#include<bits/stdc++.h>
using namespace std;
const double eps=1e-7;
int n,cnt,tot,tail,head;
double ans;
struct node{
double x,y;
node(){}
node(double _x,double _y){x=_x,y=_y;}
bool operator<(const node &t)const{return y<t.y||(y==t.y&&x<t.x);}
node operator-(node &t){return node(x-t.x,y-t.y);}
bool operator==(const node &t)const{return x==t.x&&y==t.y;}
}_P,N[55],Ans[505];
double CPr(node A,node B) {
return A.x*B.y-A.y*B.x;
}
double CPr(node A,node B,node C) {//叉积
return CPr(B-A,C-A);
}
struct edge {
node x,y;
double angle;
edge() {}
edge(node A,node B) {
x=A,y=B;
angle=atan2((B-A).y,(B-A).x);
}
bool operator<(const edge &t)const {
if(fabs(angle-t.angle)<=eps)return CPr(x,t.x,t.y)>0;//极角相同比位置
return angle<t.angle;
}
} e[505],dq[505];
double S1,S2;
node getnode(edge A,edge B) {
S1=CPr(A.x,B.y,A.y);
S2=CPr(A.x,B.x,A.y);
return node((S1*B.x.x-S2*B.y.x)/(S1-S2),(S1*B.x.y-S2*B.y.y)/(S1-S2));
}
bool ch(edge A,edge B,edge C) {//求交点
_P=getnode(B,C);
return CPr(_P,A.x,A.y)<0;
}
int halfplanes(){
sort(e+1,e+cnt+1);
tot=1;
for(int i=2; i<=cnt; i++)//去重
if(fabs(e[i].angle-e[i-1].angle)>eps)
e[++tot]=e[i];
tail=2,head=1;
dq[1]=e[1];
dq[2]=e[2];
for(int i=3; i<=tot; i++) {
while(head<tail&&ch(e[i],dq[tail],dq[tail-1])) --tail;/*如果双端队列里后两条向量的交点在这条向量的右侧。那么弹出双端队列的最后一条向量,直到满足要求为止。*/
while(head<tail&&ch(e[i],dq[head],dq[head+1])) ++head;/*对双端队列前面的向量重复上述操作*/
dq[++tail]=e[i];
}
while(head<tail&&ch(dq[head],dq[tail-1],dq[tail])) --tail;
while(head<tail&&ch(dq[tail],dq[head],dq[head+1])) ++head;
for(int i=head; i<tail; i++) Ans[i-head+1]=getnode(dq[i],dq[i+1]);//求交点
if(tail-head>1) Ans[tail-head+1]=getnode(dq[tail],dq[head]);
return tot=tail-head+1;
}
signed main() {
scanf("%d",&n);
for(int i=1,m; i<=n; i++) {
scanf("%d",&m);
for(int j=1; j<=m; j++) scanf("%lf%lf",&N[j].x,&N[j].y);
for(int j=1; j<=m; j++) e[++cnt]=edge(N[j],N[j%m+1]);
}
tot=halfplanes();
for(int i=1; i<=tot; i++) ans+=CPr(Ans[i],Ans[i%tot+1]);
printf("%.3lf\n",fabs(ans)/2);
return 0;
}
半平面交求可行域
平面最近点对
正解是分治,可以做到 \mathcal{O}(n\log n)。
为什么,为什么计算几何都这么毒瘤!所以,我不打正解了!
比如:
- P1429 平面最近点对(加强版)
- P7883 平面最近点对(加强加强版)
- P1452 [USACO03FALL]Beauty Contest G /【模板】旋转卡壳
- P6247 [SDOI2012]最近最远点对
题解们
- 旋转卡壳模板题乱搞
- ZAK教你P7883人类智慧
- myee教你P6247乱搞
参考资料
- 计算几何 练习记录
- 计算几何初步
- 计算几何基础知识整理大全
- 计算几何的算法们
- wjyyy的计算几何 学习笔记合集【凸包】【旋转卡壳】【半平面交】
- xzy巨佬的计算几何
- 计算几何的板子们
- 计算几何作图工具