首页 > 代码库 > hdu1221

hdu1221

求矩形和圆是否相交或相切。

#include <stdio.h>#include <math.h>#define judge(x,y) x<y||fabs(x-y)<0.00000001double dis(int x,int y,int a,int b){    return (x-a)*(x-a)+(y-b)*(y-b);}int main(){    int T;    scanf("%d",&T);    while(T--)    {        double x,y,r,a,b,c,d,x1,y1,x2,y2;        scanf("%lf%lf%lf%lf%lf%lf%lf",&x,&y,&r,&a,&b,&c,&d);        x1=a<c?a:c;        x2=a>=c?a:c;        y1=b<d?b:d;        y2=b>=d?b:d;        if(judge(dis(x,y,x1,y1),r*r)&&judge(dis(x,y,x2,y1),r*r)&&judge(dis(x,y,x1,y2),r*r)&&judge(dis(x,y,x2,y2),r*r))//矩形在圆形内的情况        {        if(dis(x,y,x1,y1)<r*r&&dis(x,y,x2,y1)<r*r&&dis(x,y,x1,y2)<r*r&&dis(x,y,x2,y2)<r*r)            printf("NO\n");        else            printf("YES\n");        }        else if(x<x1&&y<y1)//左下角        {            double l=dis(x,y,x1,y1);            if(judge(l,r*r))printf("YES\n");            else printf("NO\n");        }        else if(x<x1&&y>y2)//左上角        {            double l=dis(x,y,x1,y2);            if(judge(l,r*r))printf("YES\n");            else printf("NO\n");        }        else if(x>x2&&y>y2)//右上角        {            double l=dis(x,y,x2,y2);            if(judge(l,r*r))printf("YES\n");            else printf("NO\n");        }        else if(x>x2&&y<y1)//右下角        {            double l=dis(x,y,x2,y1);            if(judge(l,r*r))printf("YES\n");            else printf("NO\n");        }        else if(x<x1)//正左        {            double l=fabs(x-x1);            if(judge(l,r*r))printf("YES\n");            else printf("NO\n");        }        else if(y>y2)//正上        {            double l=fabs(y-y2);            if(judge(l,r*r))printf("YES\n");            else printf("NO\n");        }        else if(x>x2)//正右        {            double l=fabs(x-x2);            if(judge(l,r*r))printf("YES\n");            else printf("NO\n");        }        else if(y<y1)//正下        {            double l=fabs(y-y1);            if(judge(l,r*r))printf("YES\n");            else printf("NO\n");        }        else//圆心在矩形内        {            double xx=judge((x-x1),(x2-x))?(x-x1):(x2-x);            double yy=judge((y-y1),(y2-y))?(y-y1):(y2-y);            double small=judge(xx,yy)?xx:yy;            if(judge(small,r))printf("YES\n");            else printf("NO\n");        }    }    return  0;}


 

hdu1221