您的位置:首页 > 其它

hdoj 1221 Rectangle and Circle(判点的位置)

2012-03-17 02:58 381 查看
【题目大意】:给出一个平行于x轴长方形,和圆,判断其是否相交

【解题思路】:简单题,1)判长方形四个点是否都在圆内,有就不相交

2)判圆心到长方形每条边的距离是否都大于圆的半径,是就不相交

【代码】:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>

using namespace std;

#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))

struct Point {
double x, y;
Point() { }
Point(double a, double b) {
x = a, y = b;
}
};

Point r1,p1,p2,p3,p4;

inline int sig(double k) {
return k < -eps ? -1 : k > eps;
}

inline double getDist(Point a, Point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

inline double getDist1(Point a,Point b){
double tmp1,tmp;
tmp=getDist(r1,a);
tmp=min(tmp,getDist(r1,b));
double l,r,ll,rr;
l=min(a.y,b.y);
r=max(a.y,b.y);
ll=min(a.x,b.x);
rr=max(a.x,b.x);
if (a.x==b.x && r1.y>=l && r1.y<=r) {tmp=min(tmp,fabs(a.x-r1.x));}
else {if (r1.x>=ll && r1.x<=rr) tmp=min(tmp,fabs(a.y-r1.y));}
return tmp;
}

int main(){
double x,y,r,x1,yy,x2,y2;
int T;
cin >> T;
while (T--){
scanf("%lf%lf%lf%lf%lf%lf%lf",&x,&y,&r,&x1,&yy,&x2,&y2);
r1=Point(x,y);
p1=Point(x1,yy);
p2=Point(x2,yy);
p3=Point(x1,y2);
p4=Point(x2,y2);
bool flag=true;
if (sig(getDist(p1,r1)-r)==-1 && sig(getDist(p2,r1)-r)==-1 && sig(getDist(p3,r1)-r)==-1 && sig(getDist(p4,r1)-r)==-1)
flag=false;
if (sig(getDist1(p1,p2)-r)==1 && sig(getDist1(p1,p3)-r)==1 && sig(getDist1(p2,p4)-r)==1 && sig(getDist1(p3,p4)-r)==1)
flag=false;
if (flag) cout <<"YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: