hnu 12311 hiscale 判圆角矩形相交

来源:互联网 发布:php https curl 编辑:程序博客网 时间:2024/06/05 23:02




InputThere are two lines for each test data. Each line of input contains five double numbers X1, Y1, X2, Y2, R (0 ≤ X1 < X2 ≤ 1000, 0 ≤ Y1 < Y2 ≤ 1000, 0 < R ≤ 1000) .To indicate the coordinate of left bottom, the coordinate of right top, and the width of moat. 

OutputOnly one line, 'YES' means we can build the two castles at the same time, 'NO' means can't. 

Sample Input
0 0 1 1 .712 2 3 3 .710 0 1 1 .52 0 3 1 .50 0 1 1 13 3 4 4 1
Sample Output
NOYESYES


#include <stdio.h>#include <string.h>#include <math.h>struct Point{    double x,y;    Point(double a = 0,double b = 0){x = a; y = b;}    int input()    {        return scanf("%lf%lf",&x,&y);    }};struct Line{    Point p1,p2;    Line(Point a = NULL,Point b = NULL){p1 = a; p2 = b;}};struct Rect{    Point in[4],out[8];    Line edge[4];    double r;    void init(Point a,Point b)    {        in[0] = a;        in[2] = b;        in[1] = Point(in[2].x,in[0].y);        in[3] = Point(in[0].x,in[2].y);        out[0] = Point(in[0].x - r,in[0].y);        out[1] = Point(in[3].x - r,in[3].y);        out[2] = Point(in[3].x, in[3].y + r);        out[3] = Point(in[2].x, in[2].y + r);        out[4] = Point(in[2].x + r,in[2].y);        out[5] = Point(in[1].x + r,in[1].y);        out[6] = Point(in[1].x, in[1].y - r);        out[7] = Point(in[0].x, in[0].y - r);        edge[0] = Line(out[0],out[1]);        edge[1] = Line(out[2],out[3]);        edge[2] = Line(out[5],out[4]);        edge[3] = Line(out[7],out[6]);    }};bool circle_cross(Point a,double r1,Point b,double r2){    double d = sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));    return r1 + r2 > d;}bool line_cross(Line a,int f1,Line b, int f2){    if((f1&1) == (f2&1))        return 0;    if(f1)        return a.p1.x < b.p1.x && a.p2.x > b.p1.x && b.p1.y < a.p1.y && b.p2.y > a.p1.y;    else        return a.p1.y < b.p1.y && a.p2.y > b.p2.y && b.p1.x < a.p1.x && b.p2.x > a.p1.x;}bool pointInRect(Point a,Rect r){    return a.x <= r.out[3].x && a.x >= r.out[2].x && a.y < r.out[2].y && a.y > r.out[6].y        || a.x < r.out[4].x && a.x > r.out[1].x && a.y <= r.out[1].y && a.y >= r.out[0].y;}bool rect_cross(Rect a,Rect b){    int i,j;    for(i = 0; i < 4; ++ i)        for(j = 0; j < 4; ++j)        {            if(circle_cross(a.in[i],a.r,b.in[j],b.r))///判圆相交                return 0;            if(line_cross(a.edge[i],i&1,b.edge[j],j&1))///判线相交                return 0;        }    for(i = 0; i < 8; ++i)        if(pointInRect(a.out[i],b) || pointInRect(b.out[i],a))///判点在矩形里            return 0;    return 1;}int main(){    Rect a,b;    Point a1,a2,a3,a4;    while(a1.input() != EOF &&a2.input() != EOF && scanf("%lf",&a.r) != EOF          && a3.input() != EOF && a4.input() != EOF && scanf("%lf",&b.r) != EOF)    {        a.init(a1,a2);        b.init(a3,a4);        printf(rect_cross(a,b) ? "YES\n" : "NO\n");    }    return 0;}



原创粉丝点击