训练赛二 hdu 1221(图形相交)

Rectangle and Circle

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


 

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

Ignatius.L   |   We have carefully selected several similar problems for you:  1225 1220 1224 1223 1226 

 

Statistic | Submit | Discuss | Note

题意:求矩形和圆是否相交;

心得:1、注意圆是否在矩形里面或者矩形是否在圆里面;

2、求出圆心到矩形的最大和最小长度,分别是矩形到圆心到矩形的四条边的距离中的最大值和最小值。

3、用排除法更好确定

#include<iostream>
#include<cstdio>
using namespace std;
double x,y,r;
double dis(double x1,double y1,double x2,double y2)
{
	double res=0;
	if(x1==x2)
	{
		if(y1>y2) swap(y1,y2);
		if(y<y1) res=(x-x1)*(x-x1)+(y-y1)*(y-y1);
		else if(y>y2) res=(x-x1)*(x-x1)+(y-y2)*(y-y2);
		else res=(x1-x)*(x1-x);
		return res;
	}
	if(y1==y2)
	{
		if(x1>x2) swap(x1,x2);
		if(x<x1) res=(x-x1)*(x-x1)+(y-y1)*(y-y1);
		else if(x>x2) res=(x-x2)*(x-x2)+(y-y1)*(y-y1);
		else res=(y1-y)*(y1-y);
		return res;
	}
	return 0;
}
int f(double x1,double y1,double x2,double y2) //排除圆包含矩形的情况 
{
	double t1=(x-x1)*(x-x1)+(y-y1)*(y-y1);
	double t2=(x-x1)*(x-x1)+(y-y2)*(y-y2);
	double t3=(x-x2)*(x-x2)+(y-y2)*(y-y2);
	double t4=(x-x2)*(x-x2)+(y-y1)*(y-y1);
	if(t1<r*r&&t2<r*r&&t3<r*r&&t4<r*r) return 1;
	return 0;
}
int main(void)
{
	double t1,t2,t3,t4;
	double x1,y1,x2,y2;
	int p,i;
	scanf("%d",&p);
	for(i=0;i<p;i++)
	{
		scanf("%lf %lf %lf %lf %lf %lf %lf",&x,&y,&r,&x1,&y1,&x2,&y2);
		if(f(x1,y1,x2,y2))
		{
			printf("NO\n");
			continue;
		}
		t1=dis(x1,y1,x1,y2);
		t2=dis(x1,y1,x2,y1);
		t3=dis(x2,y2,x1,y2);
		t4=dis(x2,y2,x2,y1);
		if(t1>r*r&&t2>r*r&&t3>r*r&&t4>r*r) printf("NO\n"); //排除矩形包含圆和两者不想交的情况。 
		else printf("YES\n");
	}
	return 0;
}

参考文章:https://blog.csdn.net/u013163567/article/details/24737979

猜你喜欢

转载自blog.csdn.net/qq_41829060/article/details/82839326