HDU 1221 Rectangle and Circle

题意:判断圆是否与矩形相交。

分析:分别判断矩形的四条边是否与圆相交即可,即转化为判断线段与圆是否相交。

#include <stdio.h>
#include <string.h>
#include <math.h>
const double eps = 1e-8;
double rx[4],ry[4];
double cx,cy,r;

double mul(double x1,double y1,double x2,double y2)
{
    return x1*y2-x2*y1;
}
double dis(double x1,double y1,double x2,double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double ab(double s)
{
    return s>0?s:-s;
}
bool sec(int i,int j)
{
    double s=ab(mul(rx[j]-rx[i],ry[j]-ry[i],cx-rx[i],cy-ry[i]));
    double t1=dis(rx[i],ry[i],cx,cy);
    double t2=dis(rx[j],ry[j],cx,cy);
    double t3=dis(rx[i],ry[i],rx[j],ry[j]);
    double dd=s/t3;
    double tm1=sqrt(t1*t1-dd*dd);
    double tm2=sqrt(t2*t2-dd*dd);
    double d;
    if(tm1>t3||tm2>t3)
    {
        if(t1>t2)
            d=t2;
        else d=t1;
    }
    else d=dd;
    if(d<=r+eps)
        return true;
    return false;
}
bool in(int i)
{
   // printf("%lf %lf \n",rx[i],ry[i]);
    if (dis(rx[i],ry[i],cx,cy)<r-eps)
        return true;
    return false;
}
int main()
{
    int t;
    int i;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%lf %lf %lf %lf %lf %lf %lf",&cx,&cy,&r,&rx[0],&ry[0],&rx[2],&ry[2]);
        rx[1] = rx[0],ry[1] = ry[2];
        rx[3] = rx[2],ry[3] = ry[0];

        for (i=0; i<4; i++)
            if (!in(i))
                break;
        if (i==4)
        {
            printf("NO\n");
            continue;
        }
    for (i=0; i<4; i++)
        if (sec(i,(i+1)%4))
            break;
      if (i!=4)
        printf("YES\n");
      else printf("NO\n");
    }
    return 0;
}

 

posted @ 2013-01-29 15:36  'wind  阅读(236)  评论(0编辑  收藏  举报