UVA-11796-计算几何

计算几何 专栏收录该内容
3 篇文章 0 订阅

题目大意:有两条狗分别沿着自己的折线段跑,他们都是匀速运动并且同时开始同时到达,问中间过程的他们两者距离的最大值减去最小值的值是多少;

题目解析:首先他们运动的过程可以分解成在某一段时间内都在线段上运动,那么在线段上运动,我们就可以考虑运动的相对性,一个看成静止不动,另一个还是匀速运动,那么这就是个点到线段的距离问题了;

AC代码:

#include<bits/stdc++.h>
using namespace std;
struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B)   {return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (Vector A,Vector B)   {return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (Vector A,double p)   {return Vector(A.x*p,A.y*p);}
Vector operator / (Vector A,double p)   {return Vector(A.x/p,A.y/p);}
bool operator < (const Point& a,const Point& b)
{
    return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
const double eps=1e-10;
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    else return x<0?-1:1;
}
bool operator == (const Point& a,const Point& b)
{
    return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;}                     //点的点积
double Length(Vector A) {return sqrt(Dot(A,A));}                            //向量的长度
double Angle(Vector A,Vector B) {return acos(Dot(A,B)/Length(A)/Length(B));} //向量之间的角度
double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;}                    //点的叉积
double Area2(Point A,Point B,Point C){return Cross(B-A,C-A);}                    //三点构成的三角形面积的两倍
Vector Rotate(Vector A,double rad)    {return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));}     //向量逆时针旋转
Vector Normal(Vector A)                                                            //向量的法线
{
    double L = Length(A);
    return Vector(-A.y/L,A.x/L);
}

//定义直线P+tv,计算两直线的交点,前提是两直线不平行
Point GetLineIntersection(Point P,Point v,Point Q,Point w)
{
    Vector u=P-Q;
    double t=Cross(w,u)/Cross(v,w);
    return P+v*t;
}
 //点到直线的距离
double DistanceToLine(Point P,Point A,Point B)
{
    Vector v1=B-A,v2=P-A;
    return fabs(Cross(v1,v2))/Length(v1);
}
 //点到线段的距离
double DistanceToSegement(Point P,Point A,Point B)
{
    if(A==B)    return Length(P-A);
    Vector v1=B-A,v2=P-A,v3=P-B;
    if(dcmp(Dot(v1,v2))<0)  return Length(v2);
    else if(dcmp(Dot(v1,v3))>0) return Length(v3);
    else return fabs(Cross(v1,v2))/Length(v1);
}
 //点在直线上的投影
Point GetLineProjection(Point P,Point A,Point B)
{
    Vector v=B-A;
    return A+v*(Dot(v,P-A)/Dot(v,v));
}
 //判断两直线是否规范相交
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
    double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
    return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
//判断点是否在线段上并且不在线段的端点上
bool OnSegment(Point p,Point a1,Point a2)
{
    return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;
}
 //计算多边形的有向面积
 double PolygonArea(Point* p,int n)
 {
     double area=0;
     for(int i=1;i<n-1;i++)
     {
         area+=Cross(p[i]-p[0],p[i+1]-p[0]);
     }
     return area/2;
 }

const int maxn=70;
const int inf=0x3fffffff;
double Max,Min;
Point p[maxn],q[maxn];
void update(Point p,Point a,Point b)
{
    Min=min(Min,DistanceToSegement(p,a,b));
    Max=max(Max,Length(p-a));
    Max=max(Max,Length(p-b));
}
int main()
{
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        for(int i=0;i<a;i++)    scanf("%lf%lf",&p[i].x,&p[i].y);
        for(int i=0;i<b;i++)    scanf("%lf%lf",&q[i].x,&q[i].y);
        double lena=0,lenb=0;
        for(int i=1;i<a;i++)   lena+=Length(p[i]-p[i-1]);
        for(int i=1;i<b;i++)   lenb+=Length(q[i]-q[i-1]);
        Max=-inf;
        Min=inf;
        int sa=0,sb=0;
        Point pa=p[0],pb=q[0];
        while(sa<a-1&&sb<b-1)
        {
            double la=Length(p[sa+1]-pa);
            double lb=Length(q[sb+1]-pb);
            double t=min(la/lena,lb/lenb);
            Point va=(p[sa+1]-pa)/la*t*lena;
            Point vb=(q[sb+1]-pb)/lb*t*lenb;
            update(pa,pb,pb+vb-va);
            pa=pa+va;
            pb=pb+vb;
            if(pa==p[sa+1]) sa++;
            if(pb==q[sb+1]) sb++;
        }
        printf("Case %d: %.0lf\n",cas++,Max-Min);
    }
    return 0;
}


  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值