计算两个凸包之间的最小距离,旋转卡壳法详解在旋转卡壳的用法之计算两个凸 包上的最近距离

#include <iostream>
#include<cstdio>
#include<string.h>
#include<cmath>
using namespace std;
const double eps=1e-10;
const double INF=1e10;
struct point
{
double x,y;
point (double a=0,double b=0)
{
x=a;
y=b;
}
};
double min_val(double a,double b)
{
return a>b?b:a;
}
int dcmp(double a)
{
if(fabs(a)<eps)return 0;
else return a>0?1:-1;
}
point operator -(point a,point b){ return point (a.x-b.x,a.y-b.y); }
bool operator ==(const point &a,const point &b)
{
return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
double cross(point a,point b)
{
return a.x*b.y-a.y*b.x;
}
void readdate(point a[],int n,point b[],int m)
{
int i;
for(i=0;i<n;i++)
scanf("%lf%lf",&a[i].x,&a[i].y);
for(i=0;i<m;i++)
scanf("%lf%lf",&b[i].x,&b[i].y);
}
void change(point a[],int b)
{
int i;
point temp;
for(i=0;i<b/2;i++)
{
temp=a[i];
a[i]=a[b-1-i];
a[b-1-i]=temp;
}
}
void work(point p[],int n,point q[],int m)
{
int i;
for(i=0;i<n-2;i++)
if(dcmp(cross(p[i+1]-p[i],p[i+2]-p[i])>0)) break;
else if(dcmp(cross(p[i+1]-p[i],p[i+2]-p[i])<0))
{
change(p,n);
break;
}
for(i=0;i<m-2;i++)
if(dcmp(cross(q[i+1]-q[i],q[i+2]-q[i])>0))break;
else if(dcmp(cross(q[i+1]-q[i],q[i+2]-q[i])<0))
{
change(q,m);
break;
}
}
double Dot(point A,point B){return A.x*B.x+A.y*B.y;}
double Length(point A){return sqrt(Dot(A,A));}
double dist(point a,point b,point p)
{
if(a==b) return Length(p-a);
point v1=b-a,v2=p-a,v3=p-b;
if(dcmp(Dot(v1,v2))<0)return Length(v2);
if(dcmp(Dot(v1,v3))>0)return Length(v3);
return fabs(cross(v1,v2))/Length(v1); }
double Mid(point a1,point a2,point b1,point b2)
{
return min_val(min_val(dist(a1,a2,b1),dist(a1,a2,b2)),
min_val(dist(b1,b2,a1),dist(b1,b2,a2)));
}
double solve(point P[],int n,point Q[],int m)
{
double ans=INF,temp;
int i,minP=0,maxQ=0;
for(i=0;i<n;i++)
if(P[i].y<P[minP].y)
minP=i;
for(i=0;i<m;i++)
if(Q[i].y>Q[maxQ].y)
maxQ=i;
Q[m]=Q[0];
P[n]=P[0];
for(i=0;i<n;i++)
{
while((temp=cross(Q[maxQ+1]-P[minP+1],P[minP]-P[minP+1])
-cross(Q[maxQ]-P[minP+1],P[minP]-P[minP+1]))>eps)
maxQ=(maxQ+1)%m;
if(temp<-eps)
ans=min_val(ans,dist(P[minP],P[minP+1],Q[maxQ]));
else ans=min_val(ans,Mid(P[minP],P[minP+1],Q[maxQ],Q[maxQ+1]));
minP=(minP+1)%n;
}
return ans;
}
int main()
{
point p[10005],q[10005];
int n,m;
double a,b;
while(scanf("%d%d",&n,&m)==2)
{
if(m==0&&n==0)break;
readdate(p,n,q,m);
work(p,n,q,m);
a=solve(p,n,q,m);
b=solve(q,m,p,n);
printf("%.5lf\n",min_val(a,b));
}
return 0;
}

POJ3608的更多相关文章

  1. 【poj3608】 Bridge Across Islands

    http://poj.org/problem?id=3608 (题目链接) 题意 求两凸包间最短距离 Solution 难写难调,旋转卡壳,还真是卡死我了. 先分别选出两凸包最上点和最下点,从这两点开 ...

  2. 「POJ-3608」Bridge Across Islands (旋转卡壳--求两凸包距离)

    题目链接 POJ-3608 Bridge Across Islands 题意 依次按逆时针方向给出凸包,在两个凸包小岛之间造桥,求最小距离. 题解 旋转卡壳的应用之一:求两凸包的最近距离. 找到凸包 ...

  3. POJ3608(旋转卡壳--求两凸包的最近点对距离)

    题目:Bridge Across Islands 分析:以下内容来自:http://blog.csdn.net/acmaker/article/details/3178696 考虑如下的算法, 算法的 ...

  4. poj3608 Bridge Across Islands

    地址:http://poj.org/problem?id=3608 题目: Bridge Across Islands Time Limit: 1000MS   Memory Limit: 65536 ...

  5. 【旋转卡壳】poj3608 Bridge Across Islands

    给你俩凸包,问你它们的最短距离. 咋做就不讲了,经典题,网上一片题解. 把凸包上的点逆时针排序.可以取它们的平均点,然后作极角排序. 旋转卡壳其实是个很模板化的东西…… 先初始化分别在凸包P和Q上取哪 ...

  6. 旋转卡壳求两个凸包最近距离poj3608

    #include <iostream> #include <cmath> #include <vector> #include <string.h> # ...

随机推荐

  1. JavaScript—当前时间

    当前时间-倒计时下载 效果: 代码: <!doctype html> <html> <head> <meta http-equiv="Content ...

  2. CListCtrl使用(转)

    CListCtrl使用技巧 以下未经说明,listctrl默认view 风格为report 1. CListCtrl 风格 LVS_ICON: 为每个item显示大图标      LVS_SMALLI ...

  3. 【CF878E】Numbers on the blackboard 并查集

    [CF878E]Numbers on the blackboard 题意:给你一个长度为n个数列,你每次可以进行如下操作: 选取两个相邻的数x,y(x在y左面),然后将这两个数去掉,用x+2y替换它. ...

  4. HTML display:inline-block

    元素转换 display:block;   行内转块 Display:inline;   块转行内 Display:inline-block;  块或行内转行内块 链接伪类 a:link{属性:值;} ...

  5. iOS - UIScreen的 bound、frame、scale属性

    A UIScreen object contains the bounding rectangle of the device’s entire screen. When setting up you ...

  6. iOS - 开源框架、项目和学习资料汇总(网络篇)

    网络连接 1. AFNetworking – ASI不升级以后,最多人用的网络连接开源库,[推荐]iOS网络编程之AFNetworking使用,iOS开发下载文件速度计算.2. Alamofire – ...

  7. 关于JavaScript转义字符('、 " 、\" 、\')【原创】

    先插入一条广告,博主新开了一家淘宝店,经营自己纯手工做的发饰,新店开业,只为信誉!需要的亲们可以光顾一下!谢谢大家的支持!店名: 小鱼尼莫手工饰品店经营: 发饰.头花.发夹.耳环等(手工制作)网店: ...

  8. POJ-1189 钉子和小球(动态规划)

    钉子和小球 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7452 Accepted: 2262 Description 有一个 ...

  9. HDU 6312 - Game - [博弈][杭电2018多校赛2]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6312 Problem Description Alice and Bob are playing a ...

  10. POJ 1149 - PIGS - [最大流构图]

    Time Limit: 1000MS Memory Limit: 10000K Description Mirko works on a pig farm that consists of M loc ...