计算几何
基础知识
向量基础与三角函数
参考代码:
#include<bits/stdc++.h>
#define mems(a,b) memset(a,b,sizeof a)
#define PDD pair<double,double>
#define x first
#define y second
using namespace std;
const int N=210;
const double eps=1e-8;
int n;
PDD q[N],a[N],b[N];
int sign(double x){
if(fabs(x)<eps) return 0;
if(x<0) return -1;
return 1;
}
int cmp(double x,double y){
if(fabs(x-y)<eps) return 0;
if(x<y) return -1;
return 1;
}
double cross(double x1,double y1,double x2,double y2){
return x1*y2-x2*y1;
}
double area(PDD a,PDD b,PDD c){
return cross(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);
}
bool check(){
for(int i=0;i<n*2;i++)
for(int j=i+1;j<n*2;j++){
if(cmp(q[i].x,q[j].x)==0 && cmp(q[i].y,q[j].y)==0) continue;
bool flag=true;
for(int k=0;k<n;k++)
if(sign(area(q[i],q[j],a[k]))*sign(area(q[i],q[j],b[k]))>0){//在同一侧
flag=false;
break;
}
if(flag) return true;
}
return false;
}
int main(){
int T;
cin>>T;
while(T--){
scanf("%d",&n);
for(int i=0,k=0;i<n;i++){
double x1,y1,x2,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
q[k++]={x1,y1};
q[k++]={x2,y2};
a[i]={x1,y1};
b[i]={x2,y2};
}
if(check()) puts("Yes!");
else puts("No!");
}
return 0;
}
二维凸包
形象的定义:给定很多点,现在你有一个橡皮筋,把这些点套住,然后松手,橡皮筋围成的凸多边形就是这些点的凸包。
性质:是围住这些点的多边形中周长最小的一个多边形。
Andrew 算法
题目。
算法步骤:
-
以横坐标为第一关键字,纵坐标为第二关键字将点排序。
-
从左到右做维护上半部分,从右到左维护下半部分,用栈维护当前的凸壳是哪一部分:先把第一个点和第二个点加入到栈中。如果下一个点在上一个点和当前点构成向量的右侧,就直接加入,否则删除当前点和当前向量,直到下一个点在向量右侧,如果下一个点正好在向量上,就取决于题目。
-
用第一个点更新最后一个点。
参考代码:
#include<bits/stdc++.h>
#define mems(a,b) memset(a,b,sizeof a)
#define PDD pair<double,double>
#define x first
#define y second
using namespace std;
const int N=200010;
int n;
PDD q[N];
int stk[N],top;
PDD operator-(PDD a,PDD b){
return {a.x-b.x,a.y-b.y};
}
double get_dist(PDD a,PDD b){
double dx=a.x-b.x;
double dy=a.y-b.y;
return sqrt(dx*dx+dy*dy);
}
double cross(PDD a,PDD b){
return a.x*b.y-a.y*b.x;
}
double area(PDD a,PDD b,PDD c){
return cross(b-a,c-a);
}
double andrew(){
sort(q,q+n);
int top=0;
for(int i=0;i<n;i++){
while(top>=2 && area(q[stk[top-1]],q[stk[top]],q[i])>=0) top--;
stk[++top]=i;
}
int k=top;
for(int i=n-1;i>=0;i--){
while(top>k && area(q[stk[top-1]],q[stk[top]],q[i])>=0) top--;
stk[++top]=i;
}
double res=0;
for(int i=2;i<=top;i++) res+=get_dist(q[stk[i-1]],q[stk[i]]);
return res;
}
int main(){
cin>>n;
for(int i=0;i<n;i++) scanf("%lf%lf",&q[i].x,&q[i].y);
double ans=andrew();
printf("%.2lf",ans);
return 0;
}
例题
题目。
根据圆的切线定理,可以得出此题所有曲线的和就是一个完整的圆。
那么剩下的每条直边都等于所在两个圆的圆心距(因为可以构成一个矩形),再直接求凸包即可。
参考代码:
#include<bits/stdc++.h>
#define mems(a,b) memset(a,b,sizeof a)
#define PDD pair<double,double>
#define x first
#define y second
using namespace std;
const int N=40010;
const double pi=acos(-1);
int n,cnt;
PDD q[N];
int stk[N],top;
int dx[]={1,1,-1,-1};//右上角开始,顺时针标号
int dy[]={1,-1,-1,1};
PDD operator-(PDD a,PDD b){
return {a.x-b.x,a.y-b.y};
}
PDD rotate(PDD a,double b){//旋转
return {a.x*cos(b)+a.y*sin(b),-a.x*sin(b)+a.y*cos(b)};
}
double cross(PDD a,PDD b){
return a.x*b.y-a.y*b.x;
}
double area(PDD a,PDD b,PDD c){
return cross(b-a,c-a);
}
double get_dist(PDD a,PDD b){
double dx=a.x-b.x;
double dy=a.y-b.y;
return sqrt(dx*dx+dy*dy);
}
double andrew(){
sort(q,q+cnt);
for(int i=0;i<cnt;i++){
while(top>=2 && area(q[stk[top-1]],q[stk[top]],q[i])>=0) top--;
stk[++top]=i;
}
int k=top;
for(int i=cnt-1;i>=0;i--){
while(top>k && area(q[stk[top-1]],q[stk[top]],q[i])>=0) top--;
stk[++top]=i;
}
double res=0;
for(int i=2;i<=top;i++) res+=get_dist(q[stk[i-1]],q[stk[i]]);
return res;
}
int main(){
cin>>n;
double a,b,r;
cin>>a>>b>>r;
a=a/2-r;
b=b/2-r;
while(n--){
double x,y,z;
scanf("%lf%lf%lf",&x,&y,&z);
for(int i=0;i<4;i++){
PDD t=rotate({dx[i]*b,dy[i]*a},-z);
q[cnt++]={x+t.x,y+t.y};
}
}
double ans=andrew();
printf("%.2lf",ans+2*pi*r);
return 0;
}
半平面交
定义:给出很多有向直线,每条直线都会将整个平面划分成左右两部分(面向直线方向),而只保留直线左侧的部分,最后所有保留部分的交集,就是半平面交。一个例子(浅蓝色的部分就是半平面交):
显然,每次求完半平面交,如果形成了一个多边形,那么这个多边形一定是凸多边形。
算法模板
题目。
我们可以将所有凸多边形拆分成很多条边的半平面交,然后再求所有边的半平面交即可。
如果有多条方向完全一致的直线,只保留左侧的直线即可。
求半平面交步骤:
-
先将所有向量按角度排序,可以用
atan2(y,x)函数,值域为(-\pi,\pi] 。 -
按顺序扫描所有向量,用双端队列维护半平面交的轮廓:如果现在的向量与上一个向量的交点再下一个向量的右侧,那么就删除这个向量,否则继续保留。注意,队头队尾都要维护、更新。
-
用队头更新队尾,用队尾更新队头。
参考代码:
#include<bits/stdc++.h>
#define mems(a,b) memset(a,b,sizeof a)
#define PDD pair<double,double>
#define x first
#define y second
using namespace std;
const int N=510;
const double eps=1e-8;
int n,m;
int cnt;
struct Line{
PDD st,ed;
}line[N];
PDD pg[N],ans[N];
int q[N];
PDD operator-(PDD a,PDD b){
return {a.x-b.x,a.y-b.y};
}
int sign(double x){
if(fabs(x)<eps) return 0;
if(x<0) return -1;
return 1;
}
double dcmp(double x,double y){
if(fabs(x-y)<eps) return 0;
if(x<y) return -1;
return 1;
}
double get_angle(Line a){//求角度
return atan2(a.ed.y-a.st.y,a.ed.x-a.st.x);
}
double cross(PDD a,PDD b){
return a.x*b.y-a.y*b.x;
}
double area(PDD a,PDD b,PDD c){
return cross(b-a,c-a);
}
bool cmp(Line a,Line b){
double A=get_angle(a),B=get_angle(b);
if(!dcmp(A,B)) return (area(a.st,a.ed,b.ed)<0);//角度相同,更靠左的在前面
return A<B;
}
PDD get_line_intersection(PDD p,PDD v,PDD q,PDD w){//求交点
PDD u=p-q;
double t=cross(w,u)/cross(v,w);
return {p.x+v.x*t,p.y+v.y*t};
}
PDD get_line_intersection(Line a,Line b){
return get_line_intersection(a.st,a.ed-a.st,b.st,b.ed-b.st);
}
bool on_right(Line a,Line b,Line c){//判断bc的交点是否在a的右侧
PDD o=get_line_intersection(b,c);
return sign(area(a.st,a.ed,o))<=0;
}
double half_plane_intersection(){
sort(line,line+cnt,cmp);
int hh=0,tt=-1;
for(int i=0;i<cnt;i++){
if(i>0 && !dcmp(get_angle(line[i]),get_angle(line[i-1]))) continue;//角度相同
while(hh+1<=tt && on_right(line[i],line[q[tt-1]],line[q[tt]])) tt--;//队里至少有两条边,且队尾两条直线的交点在当前直线的右侧
while(hh+1<=tt && on_right(line[i],line[q[hh]],line[q[hh+1]])) hh++;//队头也要维护
q[++tt]=i;
}
while(hh+1<=tt & on_right(line[q[hh]],line[q[tt-1]],line[q[tt]])) tt--;//队头更新队尾
while(hh+1<=tt & on_right(line[q[tt]],line[q[hh]],line[q[hh+1]])) hh++;//队尾更新队头
q[++tt]=hh;
int k=0;
for(int i=hh;i<tt;i++) ans[k++]=get_line_intersection(line[q[i]],line[q[i+1]]);
double res=0;
for(int i=1;i+1<k;i++) res+=area(ans[0],ans[i],ans[i+1]);
return res/2;
}
int main(){
cin>>n;
while(n--){
scanf("%d",&m);
for(int i=0;i<m;i++) scanf("%lf%lf",&pg[i].x,&pg[i].y);
for(int i=0;i<m;i++) line[cnt++]={pg[i],pg[(i+1)%m]};
}
double res=half_plane_intersection();
printf("%.3lf",res);
return 0;
}
例题
题目。
我们可以把所有赛车所走的距离用一个一次函数表示出来:
参考代码:
#include<bits/stdc++.h>
#define mems(a,b) memset(a,b,sizeof a)
#define LD long double
#define PII pair<int,int>
#define PDD pair<LD,LD>
#define x first
#define y second
using namespace std;
const int N=10010;
const LD eps=1e-18;//此题精度逆天
int n,cnt;
int q[N],ans[N];
int ki[N],vi[N];
struct Line{
PDD st,ed;
vector<int> ids;//判重
}line[N];
PDD operator-(PDD a,PDD b){
return {a.x-b.x,a.y-b.y};
}
int sign(LD x){
if(fabs(x)<eps) return 0;
if(x<0) return -1;
return 1;
}
int dcmp(LD x,LD y){
if(fabs(x-y)<eps) return 0;
if(x<y) return -1;
return 1;
}
LD cross(PDD a,PDD b){
return a.x*b.y-a.y*b.x;
}
LD area(PDD a,PDD b,PDD c){
return cross(b-a,c-a);
}
LD get_angle(Line a){
return atan2(a.ed.y-a.st.y,a.ed.x-a.st.x);
}
bool cmp(Line a,Line b){
LD A=get_angle(a),B=get_angle(b);
if(dcmp(A,B)==0) return (area(a.st,a.ed,b.ed)<0);
return A<B;
}
PDD get_line_intersection(PDD p,PDD v,PDD q,PDD w){
PDD u=p-q;
LD t=cross(w,u)/cross(v,w);
return {p.x+v.x*t,p.y+v.y*t};
}
PDD get_line_intersection(Line a,Line b){
return get_line_intersection(a.st,a.ed-a.st,b.st,b.ed-b.st);
}
bool on_right(Line a,Line b,Line c){
auto o=get_line_intersection(b,c);
return (area(a.st,a.ed,o)<0);//并列的时候也算在第一
}
void half_plane_intersection(){
sort(line,line+cnt,cmp);
int hh=0,tt=-1;
for(int i=0;i<cnt;i++){
if(i && !dcmp(get_angle(line[i-1]),get_angle(line[i]))) continue;
while(hh+1<=tt && on_right(line[i],line[q[tt-1]],line[q[tt]])) tt--;
while(hh+1<=tt && on_right(line[i],line[q[hh]],line[q[hh+1]])) hh++;
q[++tt]=i;
}
while(hh+1<=tt && on_right(line[q[hh]],line[q[tt-1]],line[q[tt]])) tt--;
while(hh+1<=tt && on_right(line[q[tt]],line[q[hh]],line[q[hh+1]])) hh++;
int k=0;
for(int i=hh;i<=tt;i++)
for(auto id:line[q[i]].ids) ans[k++]=id;
sort(ans,ans+k);
cout<<k<<endl;
for(int i=0;i<k;i++) printf("%d ",ans[i]);
}
int main(){
map<PII,vector<int>> ids;
cin>>n;
for(int i=1;i<=n;i++) scanf("%d",&ki[i]);
for(int i=1;i<=n;i++) scanf("%d",&vi[i]);
for(int i=1;i<=n;i++) ids[{ki[i],vi[i]}].push_back(i);
line[cnt++]={{0,11451},{0,0},{}};
for(auto [k,v]:ids) line[cnt++]={{0,k.x},{1,k.x+k.y},v};
half_plane_intersection();
return 0;
}
最小圆覆盖
定义:在平面上找一个半径最小的圆,使得这个圆把给出的所有点都覆盖住,问这个圆是什么。
性质:
-
最小圆覆盖是唯一的。
-
若已经求出点集
S 的最小圆覆盖,且点P\notin S ,则P 一定在\{P\}\cup S 的最小圆覆盖的边上。
算法模板
题目。
众所周知,如果找出圆上的三个点,那么这个圆就确定了,所以只要找出圆上的三个点即可。
算法步骤:
-
将点集随机化。
-
从
2 到n 枚举所有点i ,如果i 在已求出的最小圆覆盖的内部,则圆不变;否则i 一定在新圆1 ~i 的边上,那么现在已经找出了一个点,是需要再找出两个点即可:先将现在的最小圆覆盖设为i ,再从1 到i-1 枚举每个点j ,如果j 不在现在的最小圆覆盖内,则j 一定在能覆盖1 ~j 和i 且i 在边上的最小圆覆盖的边上。那么先将圆设为以P_i,P_j 的距离为直径的圆,继续从1 到j-1 的枚举k ,如果k 不在圆内,则k 必在能够覆盖1 ~k-1 和i,j ,且i,j 在边上的最小圆覆盖边上。 -
现在一直三个点
A,B,C ,先求一下AB,AC 的中垂线,两条中垂线的交点就是圆心。求中垂线:让\vec{AB} 顺时针旋转90\degree ,这个方向就是AB 中垂线的方向;AB 中垂线的起点坐标就是\displaystyle(\frac{x_A+x_B}{2},\frac{y_A+y_B}{2}) 。再求两条直线的交点即可。
参考代码:
#include<bits/stdc++.h>
#define mems(a,b) memset(a,b,sizeof a)
#define PDD pair<double,double>
#define x first
#define y second
using namespace std;
const int N=100010;
const double eps=1e-12;
const double pi=acos(-1);
int n;
PDD q[N];
struct Circle{
PDD p;
double r;
};
PDD operator-(PDD a,PDD b){
return {a.x-b.x,a.y-b.y};
}
PDD operator+(PDD a,PDD b){
return {a.x+b.x,a.y+b.y};
}
PDD operator*(PDD a,double t){
return {a.x*t,a.y*t};
}
PDD operator/(PDD a,double t){
return {a.x/t,a.y/t};
}
double operator*(PDD a,PDD b){
return a.x*b.y-a.y*b.x;
}
int sign(double x){
if(fabs(x)<eps) return 0;
if(x<0) return -1;
return 1;
}
int dcmp(double x,double y){
if(fabs(x-y)<eps) return 0;
if(x<y) return -1;
return 1;
}
PDD get_line_intersection(PDD p,PDD v,PDD q,PDD w){
PDD u=p-q;
double t=w*u/(v*w);
return p+v*t;
}
PDD rotate(PDD a,double b){
return {a.x*cos(b)+a.y*sin(b),-a.x*sin(b)+a.y*cos(b)};
}
pair<PDD,PDD> get_line(PDD a,PDD b){//求a到b的中垂线
return {(a+b)/2,rotate(b-a,pi/2)};
}
double get_dist(PDD a,PDD b){
double dx=a.x-b.x;
double dy=a.y-b.y;
return sqrt(dx*dx+dy*dy);
}
Circle get_circle(PDD a,PDD b,PDD c){
pair<PDD,PDD> u=get_line(a,b),v=get_line(a,c);
PDD p=get_line_intersection(u.x,u.y,v.x,v.y);
return {p,get_dist(p,a)};
}
int main(){
cin>>n;
for(int i=0;i<n;i++) scanf("%lf%lf",&q[i].x,&q[i].y);
random_shuffle(q,q+n);
Circle c({q[0],0});
for(int i=1;i<n;i++)
if(dcmp(c.r,get_dist(c.p,q[i]))<0){//点i不在圆内
c={q[i],0};
for(int j=0;j<i;j++)
if(dcmp(c.r,get_dist(c.p,q[j]))<0){
c={(q[i]+q[j])/2,get_dist(q[i],q[j])/2};
for(int k=0;k<j;k++)
if(dcmp(c.r,get_dist(c.p,q[k]))<0) c=get_circle(q[i],q[j],q[k]);
}
}
printf("%.10lf\n%.10lf %.10lf",c.r,c.p.x,c.p.y);
return 0;
}
例题
题目。