• hust 1015 Intersection


    题目描述

    Given you a circle and a rectangular in a plane.

    You task is to find out how many intersecting points between the circle and the rectangular?

    输入

    Multiple cases ended with EOF.

    The first line contains three integers x, y, r, which indicates the coordinates of the center of the circle and the radius of the circle.

    The second line contains four integers x1, y1, x2, y2, which indicates the left-upper and the right-bottom coordinates of the rectangular. You can assume the edges of the rectangular are parallel to x-axis and y-axis, and all the integers are not exceeding 1000 by the absolute value.

    输出

    Only one integer, which is the intersecting points between the circle and the rectangular.

    样例输入

    0 0 1
    0 2 2 0

    样例输出

    2
    这是一道简单的计算几何的题目,一直都不敢写计算几何的题,今天开始鼓起勇气写这个题,发现不是很难啊,只有4条边,一条一条的判断,注意处理一下顶点在圆上的情况就可以了
    代码比较长,基本上是计算几何必须用的模板了
    #include<map>
    #include<set>
    #include<stack>
    #include<queue>
    #include<cmath>
    #include<vector>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #define  inf 0x0f0f0f0f
    
    using namespace std;
    
    const double pi=acos(-1.0);
    const double eps=1e-8;
    typedef pair<int,int>pii;
    
    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));
    }
    
    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)//三角形面积2倍
    {
         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);
    }
    
    Point GetLineIntersection(Point P,Vector v,Point Q,Vector 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 DistanceToSegment(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);
         double c2=Cross(a2-a1,b2-b1);
         double c3=Cross(b2-b1,a1-b1);
         double c4=Cross(b2-b1,a2-b1);
         return (dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0);
    }
    
    double dis_point_point(Point A,Point B)//点到点的距离
    {
         return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
    }
    
    Point O,A,B,C,D;
    double r,c1,c2,c3,c4,r1,r2,r3,r4;
    void read()
    {
         B.x=A.x; B.y=C.y; D.x=C.x; D.y=A.y;
         c1=dis_point_point(A,O);
         c2=dis_point_point(B,O);
         c3=dis_point_point(C,O);
         c4=dis_point_point(D,O);
         r1=DistanceToSegment(O,A,B);
         r2=DistanceToSegment(O,B,C);
         r3=DistanceToSegment(O,C,D);
         r4=DistanceToSegment(O,D,A);
    }
    
    int main()
    {
         //freopen("in.txt","r",stdin);
         while(scanf("%lf%lf%lf",&O.x,&O.y,&r)!=EOF)
         {
              scanf("%lf%lf%lf%lf",&A.x,&A.y,&C.x,&C.y);
              read();
              int ans=0;
              if (r1<r)
              {
                   if (c1>r && c2>r) ans+=2;
                   else if(c1>r && c2<r) ans+=1;
                   else if (c1<r && c2>r) ans+=1;
                   else if (c1<r && c2<r) ans+=0;
              }
              else if (r1==r)
              {
                   if (c1>r && c2>r) ans++;
              }
    
              if (r2<r)
              {
                   if (c2>r && c3>r) ans+=2;
                   else if(c2>r && c3<r) ans+=1;
                   else if (c2<r && c3>r) ans+=1;
                   else if (c2<r && c3<r) ans+=0;
              }
              else if (r2==r)
              {
                   if (c2>r && c3>r) ans++;
              }
    
              if (r3<r)
              {
                   if (c3>r && c4>r) ans+=2;
                   else if(c3>r && c4<r) ans+=1;
                   else if (c3<r && c4>r) ans+=1;
                   else if (c3<r && c4<r) ans+=0;
              }
              else if (r3==r)
              {
                   if (c3>r && c4>r) ans++;
              }
    
              if (r4<r)
              {
                   if (c4>r && c1>r) ans+=2;
                   else if(c4>r && c1<r) ans+=1;
                   else if (c4<r && c1>r) ans+=1;
                   else if (c4<r && c1<r) ans+=0;
              }
              else if (r4==r)
              {
                   if (c4>r && c1>r) ans++;
              }
              if (c1==r) ans++;
              if (c2==r) ans++;
              if (c3==r) ans++;
              if (c4==r) ans++;
              printf("%d
    ",ans);
         }
         return 0;
    }

    作者 chensunrise

  • 相关阅读:
    linux service 例子
    YII2自动初始化脚本
    ubuntu 如何在命令行打开当前目录
    mysql 储存过程
    Mysql 随笔记录
    Lack of free swap space on Zabbix server
    意外发现PHP另一个显示转换类型 binary
    常用的排序代码
    线程的实现方式之内核支持线程和用户级线程
    寻找二叉树中的最低公共祖先结点----LCA(Lowest Common Ancestor )问题(递归)
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3831392.html
Copyright © 2020-2023  润新知