传送门:

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. 用js(JavaScript-jQuery)解析XML文件 无法成功 获得XML对象,字符串一些心得

    原文作者:aircraft 原文地址:https://www.cnblogs.com/DOMLX/p/7822962.html 解析XML文件遇到的问题 今天秦博士叫我解析一下XML文件,将里面的所有 ...

  2. TOJ 1856 Is It A Tree?

    Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...

  3. rockmongo配置文件config.php

    使用编辑器(比如notepad或者VI/VIM命令)打开RockMongo安装目录下的config.php,所有的配置都在这里. 认证 mongo_auth 和control_auth 在开始使用Ro ...

  4. pat00-自测1. 打印沙漏(20)

    00-自测1. 打印沙漏(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求你写个程序把给定的符号打 ...

  5. swpuctf-web部分学习总结

    1.用优惠码 买个 X ? (1)第一步: 这道题第一步主要知道利用php的随机种子数泄露以后就可以利用该种子数来预测序列,而在题目中会返回15位的优惠码,但是必须要24位的优惠码,因此要根据15位的 ...

  6. UML建模—EA创建Use Case(用例图)

    用例图主要用来描述“用户.需求.系统功能单元”之间的关系.它展示了一个外部用户能够观察到的系统功能模型图. 1.新建用例图 2.用例图工具: 3.一个简单用例: 用例图所包含的元素如下: 1. Act ...

  7. openLayers3 中实现多个Overlay

    此篇的目的是为了记录下用Overlay的一些操作. 其实实现多个就是创建多个div,然后给每个div绑定Overlay. //页面加载完函数 --显示个关键点的名称 window.onload = f ...

  8. Zookeeper之集群搭建(Linux)

    Zookeeper集群搭建(Linux环境) 条件准备:准备三台Linux服务器 vt-serv1.vt-serv2.vt-serv3(虚拟机/物理机均可,服务器数量一定要是单数,不要问我为什么,据说 ...

  9. PAT 1070 Mooncake

    题目意思能搞成这样我也是服了这个女人了 #include <cstdio> #include <cstdlib> #include <vector> #includ ...

  10. SQLAlchemy的使用---查询的更多操作

    # 查询更多操作 from create_table import User, engine from sqlalchemy.orm import sessionmaker Session = ses ...