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

本文介绍了一种算法,用于判断二维坐标系中给定的矩形与圆是否相交,通过计算圆心到矩形四边的距离,利用排除法确定两者是否相交,并附带源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/u013163567/article/details/24737979

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值