传送门:

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. C++程序设计基础(2)变量

    注:读<程序员面试笔记>笔记总结 1.知识点 (1)C++变量命名只能包含字母.数字.下划线,其中开头不能是数字:大小写敏感:习惯上变量用小写字母,常量.宏定义用大写字母. (2)变量的作 ...

  2. C# url获取图片流转字符串

    //http url获取图片流转字符串 //string url = serverUrl.TrimEnd('/') + PUrl; //WebRequest request = WebRequest. ...

  3. FontSize sp 和 dp 的区别

    dp不会随着“设置->显示->字体大小”的改变而改变,sp会. sp会随着configeration的配置来scale, dp不会. 所以,什么时候用sp, 什么时候用dp需要斟酌.

  4. js系列之js简介

    该系列教程都来源于:廖雪峰老师的博客 JavaScript是世界上最流行的脚本语言,因为你在电脑.手机.平板上浏览的所有的网页,以及无数基于HTML5的手机App,交互逻辑都是由JavaScript驱 ...

  5. ECMAScript 6简介

    一.起步 1.扎实的HTML/CSS/Javascript基本功,这是前置条件. 2.不要用任何的构建项目工具,只用最简单的<script>,把教程里的例子模仿一遍,理解用法.不推荐上来就 ...

  6. mockito 初识

    转载:http://blog.csdn.net/zhoudaxia/article/details/33056093 在平时的开发工作中,经常会碰到开发进度不一致,导致你要调用的接口还没好,此时又需要 ...

  7. javascript实现数据结构:线索二叉树

    遍历二叉树是按一定的规则将树中的结点排列成一个线性序列,即是对非线性结构的线性化操作.如何找到遍历过程中动态得到的每个结点的直接前驱和直接后继(第一个和最后一个除外)?如何保存这些信息? 设一棵二叉树 ...

  8. boost库的配置——Linux篇

    Boost库分为两个部分来使用,一是直接使用对应的头文件,二是需要编译安装相应的库才可以使用. 下面是boost在Linux上安装和使用过程(整个boost库全部安装): (1)在www.boost. ...

  9. log4j 配置详解

    参考如下两个网址,讲的很详细,先看第一个再看第二个: log4j使用介绍:http://swiftlet.net/archives/683 java日志处理组件log4j--log4j.xml配置详解 ...

  10. Mysql Order By 注入总结

    前言 最近在做一些漏洞盒子后台项目的总结,在盒子多期众测项目中,发现注入类的漏洞占比较大.其中Order By注入型的漏洞也占挺大一部分比例,这类漏洞也是白帽子乐意提交的类型(奖金高.被过滤概率小). ...