Rectangle and Circle
Problem Description Given a rectangle and a circle in the coordinate system (two edges of the RE Ctangle are parallel with the x-axis, and the other two are-parallel with the y-axis), your have to tell if their borders I Ntersect.
Note:we call them intersect even if they are just. The circle is located by its centre and radius, and the rectangle are located by one of its diagonal.
input The A positive integer P which indicates the numb Er of test cases. Then P test Cases follow. Each test cases consists of the seven real numbers they are. That means the centre of a circle was (x,y) and the radius of the circle is R, and one of the rectangle ' s Diagonal's is (x1,y 1)-(X2,Y2).
Output for each test case, if the rectangle and the circle intersects, just output ' YES ' in ' a single ', or you sho Uld output "NO" in a single line.
Sample Input
2 1 1 1, 1 2 4 3 1 1 1 1 3 4 4.5
Sample Output
YES NO Source Hangzhou Conference on the third Program Design competition
Analysis: The sufficient and necessary conditions for the intersection of a circle and a rectangle are: the shortest distance from point to four segments Dmin<=radius && to the longest distance of four segments Dmax>=radius.
1221 ac #include #include #include #include using namespace std;
struct point{double x,y;
Point (double x=0,double y=0);
};
typedef point Vector; Point::P oint (double x,double y) {this->x=x,this->y=y;}//vector and vector +-dot product vector operator + (const vector& A,
Const vector& B) {return Vector (A.X+B.X,A.Y+B.Y);} Vector operator-(const vector& a,const vector& B) {return Vector (A.X-B.X,A.Y-B.Y);} double Dot (const VECTOR& ;
A,const vector& b) {return (A.X*B.X+A.Y*B.Y);} double Cross (const vector& a,const vector& B) {//Fork product result still vector, here is modulo
return (A.X*B.Y-B.X*A.Y);
}//vector relationship operator BOOL operator < (const vector& A,const vector& B) {if (a.x0) return Length (V3);
else return Fabs (Cross (V1,V2))/length (v1);
Double Distancemaxtosegment (Point p,point a,point B) {//dot to segment farthest distance Vector v2=p-a,v3=p-b;
return min (Length (v2), Length (v3)); }//Line segment "regular intersection", that is, the intersection is not four endpoints bool Segmentproperintersection (point a1,point a2,point b1,point b2) {Double C1=cross (a2-A1,B1-A1), C2=cross (A2-A1,B2-A1), C3=cross (B2-B1,A1-B1), C4=cross (B2-B1,A2-B1);
Return dcmp (C1) *dcmp (C2) <0&&dcmp (C3) *dcmp (C4) <0; BOOL Onsegment (Point p,point a1,point A2) {//DOT is return on line dcmp (Cross (a1-p,a2-p)) ==0&&dcmp (Dot (a1-p,a2-p)) &
lt;0;
int ncase;
Point Circle,v1,v2,v3,v4;
Double radius;
void F_init () {cin>>circle.x>>circle.y>>radius;
cin>>v1.x>>v1.y>>v3.x>>v3.y;
V2.X=V3.X,V2.Y=V1.Y;
V4.X=V1.X,V4.Y=V3.Y;
} bool F_calc () {double dis[4],dis_max[4];
Dis[0]=distancetosegment (CIRCLE,V1,V2);
Dis[1]=distancetosegment (CIRCLE,V2,V3);
Dis[2]=distancetosegment (CIRCLE,V3,V4);
Dis[3]=distancetosegment (CIRCLE,V4,V1);
Sort (dis,dis+4);
Dis_max[0]=distancemaxtosegment (CIRCLE,V1,V2);
Dis_max[1]=distancemaxtosegment (CIRCLE,V2,V3);
Dis_max[2]=distancemaxtosegment (CIRCLE,V3,V4);
Dis_max[3]=distancemaxtosegment (CIRCLE,V4,V1);
Sort (dis_max,dis_max+4); if (dis[0]radius| | DCMP (Dis_max[3]-radius) ==0)
return true;
return false;
int main (int argc, char *argv[]) {//ifstream in ("In.txt"); Cin.rdbuf (In.rdbuf ());
cin>>ncase;
while (ncase--) {f_init ();
if (F_calc ()) cout<< "yes\n";
else cout<< "no\n";
return 0;
}