传送门:

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

Rectangle and Circle

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

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
 
    判断圆和矩形是不是相交的
 
    给出圆心点,半径,矩形对角线两点
 
    我们只要求出圆心到矩形的最短距离L和圆心到矩形的最长距离R.
    如果L>r(r为圆半径),圆肯定与矩形不相交.
    如果R<r,圆包含了矩形,依然与矩形不相交.
    如果L<=r且R>=r,那么圆肯定与矩形相交.
 
    最短距离 圆心到边上点的距离的最小值
    最长距离:圆心到四个点距离的最大值
 
    注意矩形是平行xy轴的,计算方便了很多
 
code:
#include<bits/stdc++.h>
using namespace std;
double dis(double x1,double y1,double x2,double y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double f(double x,double y,double x1,double y1,double x2,double y2)//圆心到矩形边的距离最小值
{
if(x1==x2)
{
if(y<=max(y1,y2)&&y>=min(y1,y2))
{
return fabs(x-x1);
}else
{
double b=dis(x,y,x1,y1);
double c=dis(x,y,x2,y2);
double result=min(b,c);
return result;
}
}else if(y1==y2)
{
if(x<=max(x1,x2)&&x>=min(x1,x2))
{
return fabs(y-y1);
}else
{
double b=dis(x,y,x1,y1);
double c=dis(x,y,x2,y2);
double result=min(b,c);
return result;
}
}
}
int main()
{
double x,y,r,x1,y1,x2,y2;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lf %lf %lf %lf %lf %lf %lf",&x,&y,&r,&x1,&y1,&x2,&y2);
double x3=x1,y3=y2;
double x4=x2,y4=y1;
double l1=f(x,y,x1,y1,x3,y3);
double l2=f(x,y,x1,y1,x4,y4);
double l3=f(x,y,x2,y2,x3,y3);
double l4=f(x,y,x2,y2,x4,y4); double L=min(l1,min(l2,min(l3,l4))); double r1=dis(x,y,x1,y1);
double r2=dis(x,y,x2,y2);
double r3=dis(x,y,x3,y3);
double r4=dis(x,y,x4,y4); double R=max(r1,max(r2,max(r3,r4))); if(L>r)
printf("NO\n");
else if(R<r)
printf("NO\n");
else if(L<=r&&R>=r)
printf("YES\n");
}
return ;
}

HDU 1221 Rectangle and Circle(判断圆和矩形是不是相交)的更多相关文章

  1. 判断圆和矩形是否相交C - Rectangle and Circle

    Description Given a rectangle and a circle in the coordinate system(two edges of the rectangle are p ...

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

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

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

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

  4. Judge Route Circle --判断圆路线

    Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...

  5. HDU 1110 Equipment Box (判断一个大矩形里面能不能放小矩形)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1110 Equipment Box Time Limit: 2000/1000 MS (Java/Oth ...

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

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

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

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

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

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

  9. A Round Peg in a Ground Hole - POJ 1584 (判断凸多边形&判断点在多边形内&判断圆在多边形内)

    题目大意:首先给一个圆的半径和圆心,然后给一个多边形的所有点(多边形按照顺时针或者逆时针给的),求,这个多边形是否是凸多边形,如果是凸多边形在判断这个圆是否在这个凸多边形内.   分析:判断凸多边形可 ...

随机推荐

  1. 读书笔记-NIO的工作方式

    读书笔记-NIO的工作方式 1.BIO是阻塞IO,一旦阻塞线程将失去对CPU的使用权,当前的网络IO有一些解决办法:1)一个客户端对应一个处理线程:2)采用线程池.但也会出问题. 2.NIO的关键类C ...

  2. MYSQL冷知识——ON DUPLICATE KEY 批量增删改

    一 有个需求要批量增删改,并且是混合的,也就是仅不存在才增. 删简单,因为有个deleteStaute之类的字段,删除本质上就是就是一个修改 所以就是实现批量混合增改,然而组长说mysql不支持混合增 ...

  3. 移动端H5页面注意事项

    1. 单个页面内容不能过多 设计常用尺寸:7501334 / 6401134,包含了手机顶部信号栏的高度. 移动端H5活动页面常常需要能够分享到各种社交App中,常用的有微信.QQ等. 使用移动设备查 ...

  4. Oracle 查询当前系统时间十分钟之前的记录,时间比较SQL

    select * from t_register r ));

  5. Java开发团队管理细则

    软件开发是团队协作,多人开发很容易造成协调问题,因此,做一些必要的开发规范,有助于帮助新员工成长,也有助于提高开发效率,防止各种问题影响开发进度. 1. 代码规范 建议每位java开发人员都读一下&l ...

  6. etcd单机集群

    etcd在单机部署集群,可以先弄清楚配置文件参数的意思.起3个集成监听不同的端口号 1. 启动 在/etc/soft/etcd/node1文件夹中,创建脚本start1.sh etcd --name ...

  7. Mysql5.7的安装配置问题

    前些日子安装和配置mysql,遇到一些问题,在这里记录一下. 1.首先,把zip的mysql解压. 2.设置环境变量PATH中添加你的mysql解压目录. 3.在mysql根目录下新建my.ini文件 ...

  8. 【代码笔记】Java深入学习——实现客户端发送文件到服务器的文件传输

    Server.java package com.huaxin.lesson02; import java.io.FileOutputStream; import java.io.InputStream ...

  9. 深入理解JavaScript函数

    本篇文章主要介绍了"深入理解JavaScript函数",主要涉及到JavaScript函数方面的内容,对于深入理解JavaScript函数感兴趣的同学可以参考一下. JavaScr ...

  10. win7上装红米4手机驱动提示空间不足

    首先说:小米的垃圾支持.我在支持页面上看着 miuiV4或V5版本,再看我手机上9.5的版本.就感觉有些不妙. 下载下来后,点击安装程序,提示我安装空间不足......我F盘可用空间140G,不够你造 ...