【kuangbin专题】计算几何_凸包

时间:2022-11-02 15:01:14

1.poj1113 Wall

题目:http://poj.org/problem?id=1113

题意:用一条线把若干个点包起来,并且线距离任何一个点的距离都不小于r。求这条线的最小距离是多少?

分析:这道题的答案是凸包周长加上一个圆周长,即包围凸包的一个圆角多边形,但是没弄明白那些圆角加起来为什么恰好是一个圆。每个圆角是以凸包对应的顶点为圆心,给定的L为半径,与相邻两条边的切点之间的一段圆弧。每个圆弧的两条半径夹角与对应的凸包的内角互补。假设凸包有n条边,则所有圆弧角之和为180°*n-180°*(n-2)=360°。故,围墙周长为=n条平行于凸包的线段+n条圆弧的长度=凸包周长+围墙离城堡距离L为半径的圆周长。

 #include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=;
const double eps=1e-;
const double pi=acos(-1.0);
inline double sqr(double x){return x*x;}
int sgn(double x){
if (fabs(x)<eps) return ;
if (x<) return -;
return ;
}
struct point{
double x,y;
point(){}
point(double _x,double _y):x(_x),y(_y){}
//判断两点相同
bool operator ==(const point &b) const{
return sgn(x-b.x)== && sgn(y-b.y)==;
}
//
point operator -(const point &b) const{
return point(x-b.x,y-b.y);
}
//叉积
double operator ^(const point &b) const{
return x*b.y-y*b.x;
}
//点积
double operator *(const point &b) const{
return x*b.x+y*b.y;
}
//重载小于号 (求最左下角的点
bool operator <(const point &b)const{
return sgn(x-b.x)== ? sgn(y-b.y)< : x<b.x;
}
};
struct line{
point s,e;
line(){}
line(point _s,point _e):s(_s),e(_e){}
};
double dis(point a,point b){
return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
}
struct polygon{
int n;
point p[maxn];
line l[maxn];
void add(point q){
p[n++]=q;
}
void getline(){
for (int i=;i<n;i++)
l[i]=line(p[i],p[(i+)%n]);
}
struct cmp{
point p;
cmp(const point &p0):p(p0){}
bool operator ()(const point &aa,const point &bb){
point a=aa,b=bb;
int d=sgn((a-p)^(b-p));
if (d==){
return sgn(dis(a,p)-dis(b,p))<;
}
return d>;
}
};
//极角排序 先找到左下角的点
//重载好point的'<'
void norm(){
point mi=p[];
for (int i=;i<n;i++) mi=min(mi,p[i]);
sort(p,p+n,cmp(mi));
}
//得到凸包,点编号为0--n-1
void Graham(polygon &convex){
norm();
int &top=convex.n;
top=;
if (n==){
top=; convex.p[]=p[]; return ;
}
if (n==){
top=; convex.p[]=p[]; convex.p[]=p[];
if (convex.p[]==convex.p[]) top--;
return ;
}
convex.p[]=p[]; convex.p[]=p[]; top=;
for (int i=;i<n;i++){
while (top> && sgn((convex.p[top-]-convex.p[top-])^(p[i]-convex.p[top-]))<=) top--;
convex.p[top++]=p[i];
}
if (convex.n== && (convex.p[]==convex.p[])) convex.n--;
}
};
polygon C;
int main(){
int n,L; cin >> n >> L;
double x,y;
for (int i=;i<n;i++){
cin >> x >> y;
C.add(point(x,y));
}
polygon ans;
C.Graham(ans);
ans.getline();
double res=;
for (int i=;i<ans.n;i++) res+=dis(ans.l[i].s,ans.l[i].e);
res+=*pi*L;
printf("%d\n",(int)(res+0.5));
return ;
}

poj1113

(为了套板子,写的很繁琐)

2.poj2007 Scrambled Polygon

题目:http://poj.org/problem?id=2007

题意:求一个凸多边形的凸包。要求起始点为输入的第一个点。

分析:rt。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const double eps=1e-;
const int maxn=;
inline double sqr(int x){return x*x*1.0;}
int sgn(double x){
if (fabs(x)<eps) return ;
if (x<) return -;
return ;
}
struct point{
int x,y;
point(){}
point(int _x,int _y):x(_x),y(_y){}
bool operator ==(const point &b)const{
return (x==b.x && y==b.y);
}
point operator -(const point &b)const{
return point(x-b.x,y-b.y);
}
int operator ^(const point &b)const{
return x*b.y-y*b.x;
}
int operator *(const point &b)const{
return x*b.x+y*b.y;
}
//重载小于号 (求最左下角的点
bool operator <(const point &b)const{
return sgn((x-b.x)*1.0)== ? sgn((y-b.y)*1.0)< : x<b.x;
}
};
double dis(point a,point b){
return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
}
struct polygon{
int n;
point p[maxn];
void add(point q){
p[n++]=q;
}
struct cmp{
point p;
cmp(const point &p0):p(p0){}
bool operator ()(const point &aa,const point &bb){
point a=aa,b=bb;
int k=(a-p)^(b-p);int d;
if (k==) d=;else if (k<) d=-; else d=;
if (d==){
return sgn(dis(a,p)-dis(b,p))<;
}
return d>;
}
};
//极角排序 先找到左下角的点
//重载好point的'<'
void norm(){
point mi=p[];
for (int i=;i<n;i++) mi=min(mi,p[i]);
sort(p,p+n,cmp(mi));
}
//得到凸包,点编号为0--n-1
void Graham(polygon &convex){
norm();
int &top=convex.n;
top=;
if (n==){
top=; convex.p[]=p[]; return ;
}
if (n==){
top=; convex.p[]=p[]; convex.p[]=p[];
if (convex.p[]==convex.p[]) top--;
return ;
}
convex.p[]=p[]; convex.p[]=p[]; top=;
for (int i=;i<n;i++){
while (top> && sgn(((convex.p[top-]-convex.p[top-])^(p[i]-convex.p[top-]))*1.0)<=) top--;
convex.p[top++]=p[i];
}
if (convex.n== && (convex.p[]==convex.p[])) convex.n--;
}
};
polygon C;
int main(){
int x,y; point p;
C.n=;
cin >> x >> y; p=point(x,y); C.add(p);
while (cin >> x >> y) C.add(point(x,y));
polygon ans;
C.Graham(ans); int k;
for (int i=;i<ans.n;i++) if (ans.p[i]==p){k=i;break;}
for (int i=;i<ans.n;i++){
printf("(%d,%d)\n",ans.p[(k+i)%ans.n].x,ans.p[(k+i)%ans.n].y);
}
return ;
}

poj2007

3.poj1228

  题目:http://poj.org/problem?id=1228

题意:输入一个凸包上的点(没有凸包内部的点,要么是凸包顶点,要么是凸包边上的点),判断这个凸包是否稳定。所谓稳定就是判断能不能在原有凸包上加点。

分析:问凸包是否稳定。

 #include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const double eps=1e-;
const int maxn=;
int sgn(double x){
if (fabs(x)<eps) return ;
if (x<) return -;
return ;
}
struct point{
double x,y;
point(){}
point(double _x,double _y):x(_x),y(_y){}
point operator -(const point &b)const{
return point(x-b.x,y-b.y);
}
double operator ^(const point &b)const{
return x*b.y-y*b.x;
}
}p[maxn],pp[maxn];
bool cmp(point a,point b) {return a.x < b.x || (a.x == b.x && a.y < b.y);}
int convexhull(point p[],int n,point pp[]){
sort(p,p+n,cmp);
int m=;
for (int i=;i<n;i++){
while (m> && ((pp[m-]-pp[m-])^(p[i]-pp[m-]))<) m--;
pp[m++]=p[i];
}
int k=m;
for (int i=n-;i>=;i--){
while (m>k && ((pp[m-]-pp[m-])^(p[i]-pp[m-]))<) m--;
pp[m++]=p[i];
}
return m-;
}
bool check(point p[],int n){
for (int i=;i<n-;i++){
if (((p[i-]-p[i])^(p[i+]-p[i]))!= &&
((p[i]-p[i+])^(p[i+]-p[i+]))!=) return false; //保证至少有三个点在同一条边上
}
return true;
}
int main(){
int t,n; double x,y; cin >> t;
while (t--){
cin >> n;
for (int i=;i<n;i++) cin >> p[i].x >> p[i].y;
if (n<){cout << "NO\n";continue;}
int cnt=convexhull(p,n,pp);
if (check(pp,cnt)) cout << "YES\n"; else cout << "NO\n";
}
return ;
}

poj1228

4

5.poj3348 Cows

题目:http://poj.org/problem?id=3348

题意:给出一些点圈出一个最大面积每50平方养一头牛问最多能养多少牛。

分析:凸包+多边形面积。

 #include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=;
inline int sqr(int x){return x*x;}
struct point{
int x,y;
point(){}
point(int _x,int _y):x(_x),y(_y){}
//判断两点相同
bool operator ==(const point &b) const{
return x-b.x== && y-b.y==;
}
//
point operator -(const point &b) const{
return point(x-b.x,y-b.y);
}
//叉积
int operator ^(const point &b) const{
return x*b.y-y*b.x;
}
//点积
int operator *(const point &b) const{
return x*b.x+y*b.y;
}
//重载小于号 (求最左下角的点
bool operator <(const point &b)const{
return x-b.x==?y-b.y<:x<b.x;
}
};
struct line{
point s,e;
line(){}
line(point _s,point _e):s(_s),e(_e){}
};
int dis(point a,point b){
return (int)sqrt(1.0*sqr(a.x-b.x)+1.0*sqr(a.y-b.y));
}
struct polygon{
int n;
point p[maxn];
void add(point q){p[n++]=q;}
struct cmp{
point p;
cmp(const point &p0):p(p0){}
bool operator ()(const point &aa,const point &bb){
point a=aa,b=bb;
int k=(a-p)^(b-p);
if (k==){
return dis(a,p)-dis(b,p)<;
}
return k>;
}
};
//极角排序 先找到左下角的点
//重载好point的'<'
void norm(){
point mi=p[];
for (int i=;i<n;i++) mi=min(mi,p[i]);
sort(p,p+n,cmp(mi));
}
//得到凸包,点编号为0--n-1
void Graham(polygon &convex){
norm();
int &top=convex.n;
top=;
if (n==){
top=; convex.p[]=p[]; return ;
}
if (n==){
top=; convex.p[]=p[]; convex.p[]=p[];
if (convex.p[]==convex.p[]) top--;
return ;
}
convex.p[]=p[]; convex.p[]=p[]; top=;
for (int i=;i<n;i++){
while (top> && ((convex.p[top-]-convex.p[top-])^(p[i]-convex.p[top-]))<=) top--;
convex.p[top++]=p[i];
}
if (convex.n== && (convex.p[]==convex.p[])) convex.n--;
}
int getarea(){
int sum=;
for (int i=;i<n;i++) sum+=(p[i]^(p[(i+)%n]));
return sum/;
}
};
polygon C;
int main(){
int n,x,y; cin >> n; C.n=;
for (int i=;i<n;i++){
cin >> x >> y;
C.add(point(x,y));
}
polygon ans; C.Graham(ans);
cout << ans.getarea()/ << endl;
return ;
}

poj3348

6.poj1259/hdoj6219

题意:给出n个点,求出最大面积的凸包且凸包内不包含原点集中的点。

分析:rt。求最大面积空凸包。

 #include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const double eps=1e-;
const int maxn=;
inline double sqr(double x){return x*x;}
int sgn(double x){
if (fabs(x)<eps) return ;
if (x<) return -;
return ;
}
struct point{
double x,y;
point(){}
point(double _x,double _y):x(_x),y(_y){}
//判断两点相同
bool operator ==(const point &b) const{
return sgn(x-b.x)== && sgn(y-b.y)==;
}
//
point operator -(const point &b) const{
return point(x-b.x,y-b.y);
}
//叉积
double operator ^(const point &b) const{
return x*b.y-y*b.x;
}
//点积
double operator *(const point &b) const{
return x*b.x+y*b.y;
}
//重载小于号 (求最左下角的点
bool operator <(const point &b)const{
return sgn(x-b.x)== ? sgn(y-b.y)< : x<b.x;
}
}p[maxn],pp[maxn];
point tmp;
double dp[maxn][maxn];
double empty_convex(point p[],int n,point o){
double ans=;
for (int i=;i<n;i++) for (int j=;j<n;j++) dp[i][j]=;
for (int i=;i<n;i++){
int j=i-;
while (j>= && ((p[i]-o)^(p[j]-o))==) j--;
bool flag=(j==i-);
while (j>=){
int k=j-;
while (k>= && ((p[i]-p[k])^(p[j]-p[k]))>) k--;
double area=fabs((p[i]-o)^(p[j]-o))/2.0;
if (k>=) area+=dp[j][k];
if (flag) dp[i][j]=area;
ans=max(ans,area);
j=k;
}
if (flag){
for (int j=;j<i;j++) dp[i][j]=max(dp[i][j],dp[i][j-]);
}
}
return ans;
}
double dist(point a,point b){
return sqrt((a-b)*(a-b));
}
bool cmp_angle(point a,point b){
double res=(a-tmp)^(b-tmp);
if (res) return res>;
return dist(a,tmp)<dist(b,tmp);
}
double largest_empty_convex(point p[],int n){
double ans=;
for (int i=;i<n;i++){
tmp=p[i];
int cnt=;
for (int j=;j<n;j++){
if (p[j].y>tmp.y || p[j].y==tmp.y && p[j].x>tmp.x) pp[cnt++]=p[j];
}
sort(pp,pp+cnt,cmp_angle); //根据极角排序
ans=max(ans,empty_convex(pp,cnt,tmp));
}
return ans;
}
int main(){
int t,n; cin >> t;
while (t--){
cin >> n;
for (int i=;i<n;i++) cin >> p[i].x >> p[i].y;
double ans=largest_empty_convex(p,n);
printf("%.1f\n",ans);
}
return ;
}

poj1259