Bridge Across Islands POJ - 3608
题意:求2个不相交凸多边形的最近距离
思路:
算法证明不会,只会用,只能描述过程
1.求出凸包P中y最小的序号idp, 凸包Q中y最大的序号idq
2.P和Q按着逆时针的顺序,枚举凸包P的所有边.当枚举边e时,找到距离该直线最近的点(叉积)
3.维护最小值,分别是4个点到对面直线的最短距离
关于旋转卡壳的总结 , 传送门
#include<cstdio>
#include<vector>
#include<cmath>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+6;
const int MOD=1e9+7;
template <class T>
bool sf(T &ret){ //Faster Input
char c; int sgn; T bit=0.1;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
if(c==' '||c=='\n'){ ret*=sgn; return 1; }
while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
ret*=sgn;
return 1;
}
int sign(double x){
return abs(x)<1e-7?0:x<0?-1:1;
}
struct Point{
double x,y;
Point(double x=0, double y=0) : x(x), y(y) {}
Point operator - (const Point &rhs) const{
return Point(x-rhs.x,y-rhs.y);
}
bool operator == (const Point &rhs) const{
return sign(x-rhs.x)==0&&sign(y-rhs.y)==0;
}
bool operator < (const Point &rhs)const{
if(x==rhs.x) return y<rhs.y;
else return x<rhs.x;
}
}p[N];
typedef Point Vector;
double cross(Vector A,Vector B){
return A.x*B.y-A.y*B.x;
}
int n;
typedef vector<Point> Polygon;
Polygon convex_hull(Polygon P){
sort(P.begin(),P.end());
P.erase(unique(P.begin(), P.end()), P.end());
int n=P.size(),k=0;
Polygon Q(n*2);
for(int i=0;i<n;++i){
while(k>1&&cross(Q[k-2]-Q[k-1],Q[k-2]-P[i])<=0) k--;
Q[k++]=P[i];
}
int t=k;
for(int i=n-2;i>=0;--i){
while(k>t && cross(Q[k-2]-Q[k-1],Q[k-2]-P[i])<=0) k--;
Q[k++]=P[i];
}
Q.resize(k-1);
return Q;
}
double dis(Point a,Point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double dot(Point a,Point b){
return a.x*b.x+a.y*b.y;
}
double point_to_seg(Point p, Point a, Point b) { //点到线段的距离,两点式
if (a == b) return dis(p , a);
Vector V1 = b - a, V2 = p - a, V3 = p - b; //点p到线段ab的距离
if (sign(dot(V1, V2)) < 0) return dis(p,a); //|pa|
else if (sign(dot(V1, V3)) > 0) return dis(p,b); //|pb|
else return fabs(cross(V1, V2)) / dis(b,a);
}
double rc(Polygon p,Polygon q){
int n=(int)p.size();
int m=(int)q.size();
p.pb(p[0]);
q.pb(q[0]);
int idp=0,idq=0;
double miny=p[idp].y;
for(int i=1;i<n;i++)
if(sign(p[i].y-miny)<0) miny=p[i].y,idp=i;
double maxy=p[idq].y;
for(int i=1;i<m;i++)
if(sign(q[i].y-maxy)>0) maxy=q[i].y,idq=i;
double res=1e20;
for(int i=0;i<n;i++){
while( sign(cross(q[idq]-p[idp],p[idp+1]-p[idp]) - cross(q[idq+1]-p[idp],p[idp+1]-p[idp])) >0 ) idq++,idq%=m;;
res=min(res, point_to_seg(p[idp],q[idq],q[idq+1]));
res=min(res, point_to_seg(p[idp+1],q[idq],q[idq+1]));
res=min(res, point_to_seg(q[idq],p[idp],p[idp+1]));
res=min(res, point_to_seg(q[idq+1],p[idp],p[idp+1]));
idp++,idp%=n;
}
return res;
}
int main(void){
int n,m;
while(scanf("%d%d",&n,&m)==2 && (n+m)!=0){
Polygon p;
for(int i=1;i<=n;i++){
double x,y;
scanf("%lf%lf",&x,&y);
p.pb({x,y});
}
p=convex_hull(p);
Polygon q;
for(int i=1;i<=m;i++){
double x,y;
scanf("%lf%lf",&x,&y);
q.pb({x,y});
}
q=convex_hull(q);
printf("%.5f\n",rc(p,q));
}
}
/*
3 3
0 0
0 0.5
1 0
1.5 1.5
0 3
3 3
1.56525
*/