http://acm.hdu.edu.cn/showproblem.php?pid=1221

题意:

给出一个圆和一个矩形,判断2图形是否相交,(相切也算)。注意思考的要全面

代码:

View Code
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 using namespace std;
 5 struct st
 6 {
 7     double x,y;
 8 };
 9 double r;
10 double dis(st A,st B)
11 {
12     return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * ( A.y - B.y));
13 }
14 double fun(st A,st B,st o)
15 {
16     if(A.x == B.x)
17     {
18         if(o.y < min(A.y,B.y) || o.y > max(A.y,B.y))
19         return min(dis(A,o),dis(B,o));
20         return fabs(A.x - o.x);
21     }
22     else if(A.y == B.y)
23     {
24         if(o.x < min(A.x,B.x) || o.x > max(A.x,B.x))
25         return min(dis(A,o),dis(B,o));
26         return fabs(A.y - o.y);
27     }
28     return 0;
29 }
30 double f(st A,st B,st C,st D,st o)
31 {
32     double d = 0;
33     if(dis(A,o) > d)
34     d = dis(A,o);
35     if(dis(B,o) > d)
36     d = dis(B,o);
37     if(dis(C,o) > d)
38     d = dis(C,o);
39     if(dis(D,o) > d)
40     d = dis(D,o);
41     return d;
42 }
43 int main()
44 {
45     int t;
46     scanf("%d",&t);
47     while(t --)
48     {
49         st a,b,a1,b1,o,p;
50         scanf("%lf %lf %lf %lf %lf %lf %lf",&o.x,&o.y,&r,&a.x,&a.y,&b.x,&b.y);
51         if(a .x > b.x)
52         {
53             p = a;
54             a = b;
55             b = p;
56         }
57         a1.x = b.x;a1.y = a.y;
58         b1.x = a.x; b1.y = b.y;
59         double d1 = 2000000000;
60         if(fun(a1,b,o) < d1)
61         d1 = fun(a1,b,o);
62         if(fun(a,b1,o) < d1)
63         d1 = fun(a,b1,o);
64         if(fun(a1,a,o) < d1)
65         d1 = fun(a1,a,o);
66         if(fun(b1,b,o) < d1)
67         d1 = fun(b1,b,o);
68         if(d1 > r)
69         puts("NO");
70         else
71         {
72             if(r >= d1 && r <= f(a,a1,b,b1,o))
73             puts("YES");
74             else
75             puts("NO");
76         }
77     }
78     return 0;
79 }

 

posted @ 2013-03-20 09:28  fly_lovelove  阅读(293)  评论(0编辑  收藏  举报