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

来源:互联网 发布:中国动物资源数据库 编辑:程序博客网 时间:2024/05/16 11:37

【题目大意】:给出一个平行于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;}


原创粉丝点击