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. Problem D Ananagrams(map的使用)

    题目链接:Problem D 题意:输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另一个单词.在判断是否满足条件时,字母不区分大小写. 但是输出时应保留原始大小写, ...

  2. hdu 4638 Group 莫队算法

    题目链接 很裸的莫队, 就不多说了... #include<bits/stdc++.h> using namespace std; #define pb(x) push_back(x) # ...

  3. java时间验证工具

    可以验证2014-02-21这种错误

  4. DC游戏《斑鸠》原创赏析[转载]

    游戏背景:      凤来之国本来只是边远地区的一个小国.但现在他们却自称为得到“神之力”的“神通者”,在“选民思想”“和平统一”之类的名义下开始了对各地的武力侵略.      事情的起因是因为凤来之 ...

  5. Nginx 的 Echo 模块 —— echo-nginx-module(转)

    Nginx 有个 echo 模块可以用来输出一些简单的信息,例如: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 ...

  6. [HDU 4666]Hyperspace[最远曼哈顿距离][STL]

    题意: 许多 k 维点, 求这些点之间的最远曼哈顿距离. 并且有 q 次操作, 插入一个点或者删除一个点. 每次操作之后均输出结果. 思路: 用"疑似绝对值"的思想, 维护每种状态 ...

  7. protubuf在cocos2dx的应用安装

    将protubuf放置在external文件夹 在vs工程里的luacocos2d里新建筛选器protubuf,将pb.c加入到该筛选器下. 将pb.c文件的属性里设置改文件编译为C文件. 将luas ...

  8. D - 金樽清酒斗十千(搜索dfs)

    D - 金樽清酒斗十千 Time Limit:2000MS     Memory Limit:524288KB     64bit IO Format:%I64d & %I64u Submit ...

  9. 【剑指Offer学习】【面试题36:数组中的逆序对】

    题目:在数组中的两个数字假设前面一个数字大于后面的数字.则这两个数字组成一个逆序对.输入一个数组.求出这个数组中的逆序对的总数. 举例分析 比如在数组{7, 5, 6, 4 中, 一共存在5 个逆序对 ...

  10. 用Html5结合Qt制作一款本地化EXE游戏-太空大战(Space War)

    本次来说一说如何利用lufylegend.js引擎制作一款html5游戏后将其通过Qt转换成EXE程序.步骤其实非常简单,接下来就一步步地做一下解释和说明. 首先我们来开发一个有点类似于太空大战的游戏 ...