Determine whether a circle and a rectangle are intersecting (non-area intersection)
Question link: http://acm.nyist.net/JudgeOnline/problem.php? Pid = 1, 1165
Question.
The question is very simple. I will give you a rectangle and a circle and ask if they are at the intersection. Note that the intersection here is not the intersection of area. That is to say, the circle in the rectangle (and not tangent) is not intersecting. Or the rectangle is in the circle (and the four points of the rectangle are not on the circle.
So how can we judge it?
The middle contour is the side of the rectangle, and the distance between the outward and inner is the circle radius r Line (of course, the four corners are certainly not standard ).
If the center of the circle is in the red area, it will surely be in contact with the circle...
Of course, if we cannot draw this image at all. That is to say, the possible situation is that the circle includes the rectangle. Otherwise, if the distance from the center of the circle to a certain side is smaller than the radius r, it will be intersection.
Code:
#include
#include #include
#include
#include
using namespace std;const double eps = 1e-8;const double pi = acos(-1);struct POINT{ double x, y; POINT(double a, double b){ x = a; y = b; } POINT() {}};struct Seg{ POINT a, b; Seg() {} Seg(POINT x, POINT y){ a = x; b =y; }};struct Line{ POINT a, b; Line() {} Line(POINT x, POINT y){ a = x; b = y; }};struct Cir{ POINT o; double r; Cir() {} Cir(POINT oo, double rr){ o = oo; r = rr; }};struct Rec{ POINT p1, p2, p3, p4; Rec() { } Rec(POINT a, POINT b, POINT c, POINT d){ p1 = a; p2 = b; p3 = c; p4 = d; }};int dcmp(double x){ if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;}double x, y, r;double x1, yy1, x2, y2;double cross(POINT o, POINT a, POINT b){ return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y);}double dis(POINT a, POINT b){ return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));}double PointToLine(POINT p, Line l){ return fabs(cross(p, l.a, l.b)) / dis(l.a, l.b);}double PointToSeg(POINT p, Seg s){ POINT tmp = p; tmp.x += s.a.y - s.b.y; tmp.y += s.b.x - s.a.x; if(cross(s.a, p, tmp) * cross(s.b, p, tmp) >= eps){ return min(dis(p, s.a), dis(p, s.b)); } return PointToLine(p, Line(s.a, s.b));}//bool Circle_Rectangle_cross(Cir O, Rec R){ if(dcmp(dis(O.o, R.p1) - O.r) < 0 && dcmp(dis(O.o, R.p2) - O.r) < 0 && dcmp(dis(O.o, R.p3) - O.r) < 0 && dcmp(dis(O.o, R.p4) - O.r) < 0) return false; if(dcmp(PointToSeg(O.o, Seg(R.p1, R.p2)) - O.r) <= 0) return true; if(dcmp(PointToSeg(O.o, Seg(R.p2, R.p3)) - O.r) <= 0) return true; if(dcmp(PointToSeg(O.o, Seg(R.p3, R.p4)) - O.r) <= 0) return true; if(dcmp(PointToSeg(O.o, Seg(R.p4, R.p1)) - O.r) <= 0) return true; return false;}int main(){// freopen("1.txt", "r", stdin);// freopen("2.txt", "w", stdout); int T; scanf("%d", &T); while(T -- ){ Cir O; Rec R; scanf("%lf %lf %lf", &O.o.x, &O.o.y, &O.r); scanf("%lf %lf %lf %lf", &R.p1.x, &R.p1.y, &R.p2.x, &R.p2.y); scanf("%lf %lf %lf %lf", &R.p3.x, &R.p3.y, &R.p4.x, &R.p4.y); if(Circle_Rectangle_cross(O, R)) puts("Yes!"); else puts("No!"); } return 0;}