Monthly Match Topic Link: http://acm.nyist.net/JudgeOnline/problem.php?pid=1165
Exercises
The question is simple, give you a rectangle and a circle and ask you if they intersect. Notice that the intersection here is not an area intersection. In other words, the circle is disjoint (and not tangent) within the rectangle. Or the rectangle is disjoint within the circle (and the four points of the rectangle are not on a circle).
So, how do we judge it?
The middle contour line is the edge of the rectangle, the outer and inner distances are the circle radius r Dash (of course, the four corners are certainly not very standard).
If the center is in the red area, it will definitely intersect with the circle ...
Of course, if we can't draw this graphic at all. In other words, the possible situation is that the circle is included in the rectangle, otherwise, if there is a distance from one side of the center is less than the radius r, it will intersect.
Code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include < Cmath>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;}
Determine if circles and rectangles intersect (non-area intersection)