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
 
问题:在一个坐标系中,有一个圆和一个矩形,判断圆和矩形是否相交
   输入圆心坐标,圆的半径,和矩形中一对对角线中两个点的坐标
   
思路:圆和矩形相交有大概可分为两种情况
   情况一:矩形的顶点在圆内,
   情况二:矩形的顶点不在圆内,矩形的边和圆相交
   判断方法:刚开始想矩形的顶点到圆心的距离小于r就是第一种情况了,后来发现还有一种情圆很大很大以至于矩形在圆里边
   
          刚开始的时候还认为圆心到矩形的每条边的垂直距离小于圆的半径是第二种情况了,
          后来发现圆心(x, y)不满足 (x1 < x < x2)&&(y1 < y < y2)的时候,圆心到矩形每条边的垂直距离也可能小于半径
        所以把这两个判定条件所造成的多出的情况摘出来,然后再判断剩下的情况就行了
 
代码:
#include <stdio.h>
#include <string.h>
#include <math.h> using namespace std; double a, b, xa, ya, xb, yb, r; double far(double n1, double m1, double n2, double m2)
{
double ans;
ans = (n1 - n2) * (n1 - n2) +(m1 - m2) * (m1 - m2);
ans = sqrt(ans);
return ans;
} double max(double x, double y)
{
if (x > y)
return x;
else
return y;
} double min(double x, double y)
{
if (x < y)
return x;
else
return y;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%lf%lf%lf%lf%lf%lf%lf", &a, &b, &r, &xa, &ya, &xb, &yb);
if (
far(xa, ya, a, b) < r &&
far(xa, yb, a, b) < r &&
far(xb, ya, a, b) < r &&
far(xb, yb, a, b) < r //矩形在圆里面
)
{
printf("NO\n");
continue;
}
else if (
far(xa, ya, a, b) > r &&
far(xa, yb, a, b) > r &&
far(xb, ya, a, b) > r &&
far(xb, yb, a, b) > r &&
far(xa, ya, xb, ya) > *r &&
far(xa, ya, xa, yb) > *r //圆在矩形里面
)
{
printf("NO\n");
continue;
}
else if (
far(xa, ya, a, b) <= r ||
far(xa, yb, a, b) <= r ||
far(xb, ya, a, b) <= r ||
far(xb, yb, a, b) <= r //顶点在圆内
)
{
printf("YES\n");
continue;
}
else if(
(far(xa, b, a, b) <= r && b < max(ya, yb) && b > min(ya, yb))||
(far(xb, b, a, b) <= r && b < max(ya, yb) && b > min(ya, yb))||
(far(a, ya, a, b) <= r && a < max(xa, xb) && a > min(xa, xb))||
(far(a, yb, a, b) <= r && a < max(xa, xb) && a > min(xa, xb)) //顶点不在圆内但是边和圆相交
)
{
printf("YES\n");
continue;
}
else
{
printf("NO\n");
continue;
} }
return ;
}

判断圆和矩形是否相交C - Rectangle and Circle的更多相关文章

  1. HDU 1221 Rectangle and Circle(判断圆和矩形是不是相交)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1221 Rectangle and Circle Time Limit: 2000/1000 MS (J ...

  2. poj1410(判断线段和矩形是否相交)

    题目链接:https://vjudge.net/problem/POJ-1410 题意:判断线段和矩形是否相交. 思路:注意这里的相交包括线段在矩形内,因此先判断线段与矩形的边是否相交,再判断线段的两 ...

  3. PHP判断两个矩形是否相交

    <?php $s = is_rect_intersect(1,2,1,2,4,5,0,3); var_dump($s); /* 如果两个矩形相交,那么矩形A B的中心点和矩形的边长是有一定关系的 ...

  4. C# 判断两个矩形是否相交

    源代码 public bool JudgeRectangleIntersect(double RecAleftX, double RecAleftY, double RecArightX, doubl ...

  5. HDU 1221 Rectangle and Circle 考虑很多情况,good题

    http://acm.hdu.edu.cn/showproblem.php?pid=1221 114 92 31 95 13 96 3 这题只需要判断圆和矩形是否相交,然后在里面是不算相交的. 那么就 ...

  6. cocos2d-x JS 各类点、圆、矩形之间的简单碰撞检测

    这里总结了一下点.圆.矩形之间的简单碰撞检测算法 (ps:矩形不包括旋转状态) 点和圆的碰撞检测: 1.计算点和圆心的距离 2.判断点与圆心的距离是否小于圆的半 isCollision: functi ...

  7. poj 1410 Intersection (判断线段与矩形相交 判线段相交)

    题目链接 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12040   Accepted: 312 ...

  8. 【LeetCode】1401. 圆和矩形是否有重叠 Circle and Rectangle Overlapping

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 利用公式 日期 题目地址:https://leetco ...

  9. Rectangle and Square(判断正方形、矩形)

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=42#problem/D 改了N多次之后终于A了,一直在改判断正方形和矩形那,判断 ...

随机推荐

  1. MongDb添加嵌套文档

         想添加嵌套文档,就需要创建2个实体类.如下图 usermodel.Student = student; 其中Student的类型就是StudentModel: 第一个实体类         ...

  2. mysql修改用户名和密码

    修改用户名 mysql> use mysql;  选择数据库Database changedmysql> update user set user="dns" wher ...

  3. Hadoop MultipleOutputs 结果输出到多个文件夹 出现数据不全,部分文件为空

    如题:出现下图中的情况(设置reduceNum=5) 感觉很奇怪,排除了很久,终于发现是一个第二次犯的错误:丢了这句 this.mOutputs.close(); 加上这句,一切恢复正常!

  4. Oracle分区知识

    查询分区名称.表空间的SQL USER_SEGMENTS SELECT SEGMENT_NAME,PARTITION_NAME,TABLESPACE_NAME FROM USER_SEGMENTS; ...

  5. JS笔记 入门第一

    WHY? 一.你知道,为什么JavaScript非常值得我们学习吗? 1. 所有主流浏览器都支持JavaScript. 2. 目前,全世界大部分网页都使用JavaScript. 3. 它可以让网页呈现 ...

  6. MVC-03 控制器(5)

    八.动作过滤器 有时在运行Action之前或之后会需要运行一些逻辑运算,以及处理一些运行过程中所生成的异常状况,为了满足这个需求,ASP.NET MVC提供动作过滤器(Action Filter)来处 ...

  7. zookeeper 同步

    <pre name="code" class="html">一个节点上的数据发生变化后,通知其他节点 server 1: [root@wx03 bi ...

  8. IPTABLES 映射问题

    今天要做一个新的映射:将内网的一个8090口映射到外网的8087口. 在 /ETC/RC.LOCAL中最后插入: iptables -t nat -A PREROUTING -d outIP -p t ...

  9. Mac OS使用技巧之十五:快捷方便的Mini Dock

    Mini Dock是前面忘记了提,这里做一些补充.       Mini Dock是Mac OSX的一个值得大书特书的亮点.尽管windows下也有类似的东西,但Mac下却提供了更为全面的功能.通过M ...

  10. Connection for controluser as defined in your configuration failed.

    在mysql中使用事件调度器(计划任务), 语句写好了,运行也ok,可是却没有预期的结果.网上总结了非常多计划任务失效的原因.没有一种适合我. 在phpmyadmin中打开事件表,发现以下一串红色的提 ...