传送门:

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. AI从入门到放弃:CNN的导火索,用MLP做图像分类识别?

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 作者:郑善友 腾讯MIG后台开发工程师 导语:在没有CNN以及更先进的神经网络的时代,朴素的想法是用多层感知机(MLP)做图片分类的识别:但 ...

  2. 面试题 数据库sql

    一.建表的结构和数据,在sqlserver直接用就行了 USE [test] GO /****** Object: Table [dbo].[TEACHER] Script Date: 05/16/2 ...

  3. 接收时间戳model [JsonConverter(typeof(UnixDateTimeConverter))]

    /// <summary>        /// 创建时间        /// </summary>        [JsonProperty("createtim ...

  4. Windows未能启动 由于关键系统驱动程序丢失或损坏 电脑无法开机

    该错误导致系统无法开机,其实也好解决 错误描述: Windows未能启动.原因可能是最近更改了硬盘或软件.解决此问题的步骤…… 1.…… 2.…… 3.…… …… 文件:\windows\system ...

  5. Starting MySQL. ERROR! The server quit without updating PID file如何解决

    今天数据库突然挂了.重启提示: Starting MySQL. ERROR! The server quit without updating PID file (/usr/local/mysql/v ...

  6. WPF MVVM 如何在ViewModel中操作View中的控件事件

    (在学习Wpf的时候,做一个小例子,想在TextBox改变后,检验合法性,并弹出提示.在找了很多贴后,发现这个小例子,抄袭过来,仅供参考. 最后也找到了适合自己例子的办法:在出发TextChanged ...

  7. Sql server 0x80131904

    这是一个比较让人纠结的错误. 背景:添加了网站的分支,要拿一个现有的数据库做一个新的数据库的东东. 首先就是还原备份,然后做代码的修改.然而在登录的时候报了这个错误.0x80131904 什么插入的列 ...

  8. Scrapy框架之日志等级和请求传参

    一.Scrapy的日志等级 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息. 1.日志等级(信息种类) ERROR:错误 WARN ...

  9. 关于 C# 中接口的一些小结

    < 关于 C# 中“接口”的一些小结 > 对于 C# 这样的不支持多重继承的语言,很好的体现的层次性,但是有些时候多重继承的确有一些用武之地.   比如,在 Stream 类 . 图形设备 ...

  10. SharePoint 2013 - Workflow Manager

    1. Workflow Manager可以与SharePoint 安装在同一台机器上,只是不建议这么做:由于Workflow Manager 需要使用数据库,我个人将其安装在 SQL Server机器 ...