POJ - 3608 Bridge Across Islands(凸包+旋转卡壳模板 求两凸包点之间的最近距离)

链接:https://cn.vjudge.net/problem/POJ-3608

题意:求两凸包点之间的最近距离,注意这个意思是说,如果求点到线段的距离,那么要保证垂足落到线段上。

思路:枚举一个凸包的边,去找另一凸包中的最远点,和求在一个凸包中的对踵点差不多。

PS:本想自己整理个模板,发现还是kuangbin大大的模板好用。

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 1e4+10;
const double eps = 1e-8;
int sgn(double x)
{
	if(fabs(x)<eps) return 0;
	else if(x<0) return -1;
	else return 1;
}
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;
	}
	double operator *(const Point& b)const//点乘 
	{
		return x*b.x+y*b.y;
	}		
}p[N],q[N],st[N],c[N];
struct Line
{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e)
    {
        s = _s;
        e = _e;
    }
};

int n,pos,top,m;
double x;
double dis(Point a,Point b)
{
	return sqrt((a-b)*(a-b));
}
bool cmp(const Point& a,const Point& b)
{
	x=(a-c[0])^(b-c[0]);
	if(sgn(x)==0)
	{
		return dis(c[0],a)<dis(c[0],b);
	}
	else if(x>0) return 1;
	else return 0;
}
//线段L上,距离P最近的点 
Point NearestPointToLineSeg(Point P,Line L)
{
    Point result;
    double t = ((P-L.s)*(L.e-L.s))/((L.e-L.s)*(L.e-L.s));
    if(t >= 0 && t <= 1)
    {
        result.x = L.s.x + (L.e.x - L.s.x)*t;
        result.y = L.s.y + (L.e.y - L.s.y)*t;
    }
    else
    {
        if(dis(P,L.s) < dis(P,L.e))
            result = L.s;
        else result = L.e;
    }
    return result;
}
//点p0到线段p1p2的距离
double pointtoseg(Point p0,Point p1,Point p2)
{
    return dis(p0,NearestPointToLineSeg(p0,Line(p1,p2)));
}
//平行线段p0p1和p2p3的距离
double dispallseg(Point p0,Point p1,Point p2,Point p3)
{
    double ans1 = min(pointtoseg(p0,p2,p3),pointtoseg(p1,p2,p3));
    double ans2 = min(pointtoseg(p2,p0,p1),pointtoseg(p3,p0,p1));
    return min(ans1,ans2);
}
//得到向量a1a2和b1b2的位置关系
double Get_angle(Point a1,Point a2,Point b1,Point b2)
{
    Point t = b1 - ( b2 - a1 );
    return (a2-a1)^(t-a1);
} 
//求凸包时比较 
bool check(Point a,Point b,Point c)
{
	x=(b-a)^(c-a);
	return sgn(x)<=0;
}
//将点按逆时针排序 
void sort_point(Point *p,int n)
{
		pos=0;		
		for(int i=0;i<n;i++)
		{
			scanf("%lf%lf",&p[i].x,&p[i].y);
			if(p[i].y<p[pos].y || (p[i].y==p[pos].y&&p[i].x<p[pos].x))
				pos=i;
		}
		swap(p[0],p[pos]);
		for(int i=0;i<n;i++)
			c[i]=p[i];
		sort(c+1,c+n,cmp);
		for(int i=0;i<n;i++)
			p[i]=c[i];	
}
//求凸包 
void getconv(Point *p,int& n)
{
	st[0]=p[0];
	st[1]=p[1];
	top=1;
	for(int i=2;i<n;i++)
	{
		//逆时针必须向左转 
		while(top>1&&check(st[top-1],st[top],p[i])) 
			top--;			
		st[++top]=p[i];
	}	
	n=top+1;
	for(int i=0;i<n;i++)
		p[i]=st[i];
}
double getarea(Point a,Point b,Point c)
{
	return (a-c)^(b-c);	
}
//旋转卡壳,求两凸包间最近距离
//枚举一个凸包的边,去找另一凸包中的最远点 
double rc(Point *p,int np,Point *q,int nq)
{

	double ans=1e40,tmp;
	int pp=0,qq=0,temp;
	//ymin即0
	//找ymax 
	for(int i=0;i<nq;i++)
		if(sgn(q[i].y-q[qq].y)>0)
			qq=i;
	for(int i=0;i<np;i++)
	{
         //两种写法,都是看距离能不能变远 
		//while(sgn(tmp = get_angle(p[pp],p[(pp+1)%np],q[qq],q[(qq+1)%nq])) < 0 )
        while(sgn(tmp = getarea(p[pp],p[(pp+1)%np],q[qq])-getarea(p[pp],p[(pp+1)%np],q[(qq+1)%nq]))<0) 
		    qq = (qq + 1)%nq;
        //分类讨论,不能直接求点到线段的距离,因为距离该点最近的点可能不在线段上,
		//即过点作垂线时,垂足不在线段上    
        if(sgn(tmp) == 0)
            ans = min(ans,dispallseg(p[pp],p[(pp+1)%np],q[qq],q[(qq+1)%nq]));
        else ans = min(ans,pointtoseg(q[qq],p[pp],p[(pp+1)%np]));
		pp=(pp+1)%np;
	}
	return ans;
} 

int main(void)
{
	while(~scanf("%d%d",&n,&m)&&(n||m))
	{	
		sort_point(p,n);
		sort_point(q,m);		
		getconv(p,n);
		getconv(q,m);
		printf("%.5f\n",min(rc(p,n,q,m),rc(q,m,p,n)));	
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值