HDOJ1221 计算几何入门题

Rectangle and Circle

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3237 Accepted Submission(s): 851

Problem Description
Given a rectangle and a circle in the coordinate system(two edges of the rectangle are parallel with the X-axis, and the other two are parallel with the Y-axis), you have to tell if their borders intersect.

Note: we call them intersect even if they are just tangent. The circle is located by its centre and radius, and the rectangle is located by one of its diagonal.

Input
The first line of input is a positive integer P which indicates the number of test cases. Then P test cases follow. Each test cases consists of seven real numbers, they are X,Y,R,X1,Y1,X2,Y2. That means the centre of a circle is (X,Y) and the radius of the circle is R, and one of the rectangle’s diagonal is (X1,Y1)-(X2,Y2).

Output
For each test case, if the rectangle and the circle intersects, just output “YES” in a single line, or you should output “NO” in a single line.

Sample Input
2
1 1 1 1 2 4 3
1 1 1 1 3 4 4.5

Sample Output
YES
NO

Author
weigang Lee

Source
杭州电子科技大学第三届程序设计大赛

Recommend
Ignatius.L | We have carefully selected several similar problems for you: 1225 1223 1226 1222 1798

第一次做计算几何题目,感觉代码量比之前都大很多。这题还算是最最最简单的了。。。

#include 
#include 
#include 
using namespace std;

struct node{
   double x,y;
}cir,a,b,c,d;
double r;

double dis(node &a, node &b){
    return sqrt(pow(a.x-b.x,2) + pow(a.y-b.y,2));
}

int main(){
   std::ios::sync_with_stdio(false);
   int p;
   node tmp;
   cin >> p;
   while (p--) {
       cin >> cir.x >> cir.y >> r >> a.x >> a.y >> b.x >> b.y;
       if (a.x>b.x) {tmp = a; a=b; b=tmp;}
       c.x = a.x; c.y = b.y;
       d.x = b.x; d.y = a.y;

       //这个最优先考虑,因为有可能圆在矩形内部,先判断后两种情况会输出YES,实际却是NO
       if (dis(a,cir)cout << "NO" << endl;
           continue;
       }

       if (a.x<=cir.x && b.x>=cir.x) {
          if (fabs(a.y-cir.y)<=r || fabs(b.y-cir.y)<=r) {
               cout <<"YES" << endl;
               continue;
          }
       }

       if ((a.y<=cir.y && b.y>=cir.y) ||(a.y>=cir.y && b.y<=cir.y)) {
         if (fabs(a.x-cir.x)<=r || fabs(b.x-cir.x)<=r) {
               cout <<"YES" << endl;
               continue;
          }
       }


       if (dis(a,cir)<=r || dis(b,cir)<=r || dis(c,cir)<=r || dis(d,cir)<=r) {
            cout <<"YES" << endl;
            continue;
       }

       cout << "NO" << endl;
   }
   return 0;
}

你可能感兴趣的:(计算几何)