HDU 1221 Rectangle and Circle 考虑很多情况,good题
http://acm.hdu.edu.cn/showproblem.php?pid=1221
1
14 92 31 95 13 96 3
这题只需要判断圆和矩形是否相交,然后在里面是不算相交的。
那么就有好几种情况了。
1、整个矩形在圆形里,NO,(我的做法是所有点到圆心距离小于半径)
2、整个圆在矩形里,NO,一个圆选出5个点,圆心,和最上下左右,如果这些点都在,则不行。我的做法直接PointInPolygon。
3、否则,只要有公共点,就直接YES。然后,如果圆那5个点有一个在矩形,或者矩形的4个点到圆心的距离小于半径,即可。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
#define MY "H:/CodeBlocks/project/CompareTwoFile/DataMy.txt", "w", stdout
#define ANS "H:/CodeBlocks/project/CompareTwoFile/DataAns.txt", "w", stdout
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const double eps = 1e-;
bool same(double x, double y) {
return fabs(x - y) < eps;
}
struct coor {
double x,y;
coor() {}
coor(double xx,double yy):x(xx),y(yy) {}
double operator ^(coor rhs) const { //计算叉积(向量积),返回数值即可
return x*rhs.y - y*rhs.x;
}
coor operator -(coor rhs) const { //坐标相减,a-b得到向量ba,返回一个向量(坐标形式)
return coor(x-rhs.x,y-rhs.y);
}
double operator *(coor rhs) const { //数量积,返回数值即可
return x*rhs.x + y*rhs.y;
}
bool operator ==(coor rhs) const {
return same(x,rhs.x)&&same(y,rhs.y); //same的定义其实就是和eps比较
}
} a[], cir[];
double dis(coor a, coor b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
} bool OnSegment (coor a,coor b,coor cmp) { //判断点cmp是否在线段ab上
double min_x = min(a.x,b.x), min_y = min(a.y,b.y);
double max_x = max(a.x,b.x), max_y = max(a.y,b.y);
if (cmp.x>=min_x && cmp.x<=max_x && cmp.y>=min_y && cmp.y<=max_y) return true;
else return false;
} int PointInPolygon (coor p[],int n,coor cmp) {
int cnt = ; //记录单侧有多少个交点,这里的p[],必须有顺序
for (int i=; i<=n; ++i) {
int t = (i+)>n ? :(i+); //下标为1要这样MOD
coor p1=p[i],p2=p[t];
//printf ("%lf %lf %lf %lf***\n",p1.x,p1.y,p2.x,p2.y);
if (OnSegment(p1,p2,cmp)) {
coor t1 = p1-cmp,t2 = p2-cmp; //同时要叉积等于0,这是在线段上的前提
if ((t1^t2)==) return ;// 2表明在多边形上,可以适当省略
}
if (cmp.y >= max(p1.y,p2.y)) continue;//交点在延长线上和在凸顶点上的都不要
if (cmp.y < min(p1.y,p2.y)) continue;//交点在凹顶点上就要,这里没取等
if (same(p1.y,p2.y)) continue; //与cmp.y是平行的
double x = (cmp.y-p1.y)*(p1.x-p2.x)/(p1.y-p2.y) + p1.x; //求交点 p1.y != p2.y不会除0
if (x>cmp.x) cnt++;//只统计一侧的交点
}
return cnt&;//0表明点在多边形外,1表明点在多边形内
} void work() {
double x, y, R;
scanf("%lf%lf%lf", &x, &y, &R);
double xx1, xx2, yy1, yy2;
scanf("%lf%lf%lf%lf", &xx1, &yy1, &xx2, &yy2);
int lena = ;
a[++lena] = coor(xx1, yy1);
a[++lena] = coor(xx1, yy2);
a[++lena] = coor(xx2, yy2);
a[++lena] = coor(xx2, yy1); int lencir = ;
cir[++lencir] = coor(x + R, y);
cir[++lencir] = coor(x, y + R);
cir[++lencir] = coor(x - R, y);
cir[++lencir] = coor(x, y - R);
cir[++lencir] = coor(x, y);
int up = ;
for (int i = ; i <= lencir; ++i) {
if (PointInPolygon(a, , cir[i])) {
up++;
}
if (PointInPolygon(a, , cir[i]) == && i != lencir) {
printf("YES\n");
return;
}
}
if (up == lencir) {
printf("NO\n");
return;
}
up = ;
for (int i = ; i <= lena; ++i) {
double tdis = dis(a[i], coor(x, y));
if (same(tdis, R)) {
printf("YES\n");
return;
}
if (tdis < R) up++;
}
if (up == lena) {
printf("NO\n");
return;
} for (int i = ; i <= lena; ++i) {
double tdis = dis(a[i], coor(x, y));
if (same(tdis, R) || tdis < R) {
printf("YES\n");
return;
}
} for (int i = ; i <= lencir; ++i) {
if (PointInPolygon(a, , cir[i])) {
printf("YES\n");
return;
}
}
printf("NO\n");
// cout << dis(coor(5,5), coor(7, 7)) << endl;
return;
}
int main() {
#ifdef local
freopen("data.txt","r",stdin);
freopen(MY);
#endif
int t;
scanf("%d", &t);
while (t--) {
work();
}
return ;
}
开始的时候,判断有一个点在就行的时候,(就是相切的时候),把圆心也算进去了,坑了半天。
https://www.desmos.com/calculator 分享个画图工具,矩形的话,就直接是x>=10 x <= 99这样画
HDU 1221 Rectangle and Circle 考虑很多情况,good题的更多相关文章
- HDU 1221 Rectangle and Circle(判断圆和矩形是不是相交)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1221 Rectangle and Circle Time Limit: 2000/1000 MS (J ...
- libgdx学习记录25——Rectangle与Circle是否重叠
Rect与Circle重叠有三种情况: 1. Rect至少有一个角在Circle里面 2. Circle与Rect的左边或右边相交,或者Circle在Rect内 3. Circle与Rect的顶边或底 ...
- Leaflet:Path、Polyline、Polygon、Rectangle、Circle、CircleMarker
下边介绍Vector Layer Path(Layer) Path是其他Vector Layer的父类,比如Polyline.Polygon.Rectangle.Circle.CircleMarker ...
- hdu 5288||2015多校联合第一场1001题
pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...
- hdu 5343 MZL's Circle Zhou SAM
MZL's Circle Zhou 题意:给定两个长度不超过a,b(1 <= |a|,|b| <= 90000),x为a的连续子串,b为y的连续子串(x和y均可以是空串):问x+y形成的不 ...
- 判断圆和矩形是否相交C - Rectangle and Circle
Description Given a rectangle and a circle in the coordinate system(two edges of the rectangle are p ...
- HDU 5343 MZL's Circle Zhou
MZL's Circle Zhou Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on HDU. Orig ...
- opencv —— line、ellipse、rectangle、circle、fillPoly、putText 基本图形的绘制
绘制线段:line 函数 void line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, ...
- HDU 5343 MZL's Circle Zhou 后缀自动机+DP
MZL's Circle Zhou Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
随机推荐
- ICMP协议 广播以查询局域网内的所有主机
看到了很多局域网内的主机扫描工具,在想怎么去实现这样一个工具.前几天看了Ping源码--ICMP协议的实例,ICMP可以用来探测网联网内的任一主机,ICMP和广播地址结合来扫描局域网内的所有主机不是很 ...
- mysql数据库隔离级别及其原理、Spring的7种事物传播行为
一.事务的基本要素(ACID) 1.原子性(Atomicity):事务开始后所有操作,要么全部做完,要么全部不做,不可能停滞在中间环节.事务执行过程中出错,会回滚到事务开始前的状态,所有的操作就像没有 ...
- 基于MATLAB的滤波算法
目前比较经典的图像去噪算法主要有以下三种: 均值滤波:也称线性滤波,主要思想为邻域平均法,即用几个像素灰度 的平均值来代替每个像素的灰度.有效抑制加性噪声,但容易引起图像模糊, 可以对其进行改进, ...
- vue开发总结(一)
vue使用快一个多月了,从移动端到PC端,踩过的坑也不少.项目的开发是基于element ui 与 mint ui 组件库,下面总结下项目中的一些知识点: 一.网页数据请求 首次进入页面,需要请求数据 ...
- Oracle:impdb导入
最近有现场给我一份用expdp导出dmp文件,我用imp导入时,报错.因为导出dmp的数据库是11g,导入的数据库也是11g, 但客户端安装的是10g,不能用imp导入:所以只能试着用impdp导入: ...
- hdu 5615 Jam's math problem(十字相乘判定)
d. Jam有道数学题想向你请教一下,他刚刚学会因式分解比如说,x^2+6x+5=(x+1)(x+5) 就好像形如 ax^2+bx+c => pqx^2+(qk+mp)x+km=(px+k)(q ...
- emacs环境配置
Cscope: 首先官网上下载cscope的源码包,解压进入,按照INSTALL的说明: ./configure make make install 但是在make时报如下错误:fatal error ...
- LibSVM学习详细说明
代码文件主要针对Matlab进行说明,但个人仍觉得讲解的支持向量机内容非常棒,可以做为理解这一统计方法的辅助资料; LibSVM是台湾林智仁(Chih-Jen Lin)教授2001年开发的一套支持向量 ...
- 理解分布式id生成算法SnowFlake
理解分布式id生成算法SnowFlake https://segmentfault.com/a/1190000011282426#articleHeader2 分布式id生成算法的有很多种,Twitt ...
- 微信小程序在线制作 自己制作微信小程序
小程序是个什么东西?怎么自己制作微信小程序?微信小程序在线制作难吗?最近老是听这类问题,耳朵都长茧子了. 百牛信息技术bainiu.ltd整理发布于博客园 接下来作为一个技术人员的角度就为大家分析一下 ...