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

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值