The given rectangle is defined as follows:

struct rect{ int// represents the upper-left horizontal coordinate of the rectangle int// represents the upper-left vertical coordinate of the rectangle int// indicates rectangle width int // represents the rectangle height };

**Now give three rectangles, the intersection of three rectangles, if there is no intersection, then the rectangle's x,y,w and H are assigned to-1. For example, the following illustration shows the rectangle represented by a thick line box that intersects three rectangles.**

**Problem Solving Ideas:**

Problem-solving thinking is very important, there is no collective thinking, the topic is certainly not to be made. The following gives my problem-solving ideas:

(1) Determine whether the three rectangles have a intersection. This is a difficult point, how to do it? You can arrange the three rectangles by x in the x-axis direction from left to right, to determine whether the 22 rectangle has an intersection in the X-axis direction, and if any pair does not intersect then three rectangles have no intersection. The judging method is that if RECTB.X>=RECTA.X+RECTA.W, then there is no intersection between RECTA and RECTB.

Similarly, in the direction of the y-axis to do the same judgment;

(2) The intersection rectangle of any two rectangles is calculated, and then the intersection rectangle and the third rectangle are then intersected, and the final intersection rectangle is obtained.

With the correct and clear ideas, you can write code, the following gives my implementation, for users to reference.

1#include <iostream>2 using namespacestd;3#include <vector>4#include <algorithm>5 6 structrect{7 intX//represents the upper-left horizontal coordinate of a rectangle8 intY//represents the upper-left vertical coordinate of a rectangle9 intW//represents the width of a rectangleTen intH//represents the height of a rectangle One }; A - //Sort by x increment - BOOLComparex (Constrect& Recta,Constrect&RECTB) { the returnrecta.x<rectb.x; - } - - //Sort by y increment + BOOLComparey (Constrect& Recta,Constrect&RECTB) { - returnrecta.y<rectb.y; + } A at //determine if three rectangles intersect - BOOLIsintersect (Constrect& Recta,Constrect& RECTB,Constrect&RECTC) { -Rect Rectleft,rectxmid,rectright;//left-to-right rectangle -Rect Recttop,rectymid,rectbelow;//top-to-bottom rectangle - - //sort rectangles by x left to right invector<ConstRect>Vec; - Vec.push_back (recta); to Vec.push_back (RECTB); + Vec.push_back (RECTC); - sort (Vec.begin (), Vec.end (), Comparex); therectleft=vec[0],rectxmid=vec[1],rectright=vec[2]; * $ //no intersection of any two rectangles in horizontal directionPanax Notoginseng if(rectxmid.x>=rectleft.x+rectleft.w| | rectright.x>=rectxmid.x+rectxmid.w| | rectright.x>=rectleft.x+RECTLEFT.W) - return false; the + //Similarly, sort the rectangles by Y A sort (Vec.begin (), Vec.end (), Comparey); therecttop=vec[0],rectymid=vec[1],rectbelow=vec[2]; + - //no intersection of any two rectangles in the vertical direction $ if(rectymid.y>=recttop.y+recttop.h| | rectbelow.y>=rectymid.y+rectymid.h| | rectbelow.y>=recttop.y+rectTop.h) $ return false; - return true;//three rectangles have an intersection - } the - //the intersection of two rectangles, provided that two rectangles must have an intersectionWuyiRect Intersection (Constrect& Recta,Constrect&RECTB) { the Rect Resrect; -resrect.x=recta.x>rectb.x?recta.x:rectb.x;//Select the X of the rightmost rectangle as the X of the intersection Wuresrect.y=recta.y>rectb.y?recta.y:rectb.y;//Select y of the bottom rectangle as the intersection of Y - //Select the right side of the left rectangle (the smaller x coordinate) as the right side of the intersection rectangle, so you can find the width of the intersection rectangle Aboutresrect.w=recta.x+recta.w<rectb.x+rectb.w?recta.x+recta.w-resrect.x:rectb.x+rectb.w-Resrect.x; $ //Similarly, select the bottom of the upper rectangle (the lower y coordinate) as the bottom of the intersection rectangle so that the height of the intersection rectangle can be calculated -resrect.h=recta.y+recta.h<rectb.y+rectb.y?recta.y+recta.h-resrect.y:rectb.y+rectb.h-Resrect.y; - returnResrect; - } A + the //find the intersection of three rectangles -Rect Threeintersection (Constrect& Recta,Constrect& RECTB,Constrect&RECTC) { $ Rect Res; the BOOLIsintersectbool=Isintersect (RECTA,RECTB,RECTC); the if(Isintersectbool) {//have intersect theRect rectab=intersection (RECTA,RECTB); theres=intersection (RECTAB,RECTC); - } in Else theres.x=res.y=res.w=res.h=-1; the returnRes; About}

The test results are as follows:

intMain () {Rect recta,rectb,rectc; //test Case 1//recta.x=0,recta.y=0,recta.w=1,recta.h=1; //rectb.x=1,rectb.y=1,rectb.w=1,rectb.h=1; //rectc.x=2,rectc.y=2,rectc.w=1,rectc.h=1; //Test Case 2recta.x=0, recta.y=0, recta.w=2, recta.h=2; rectb.x=1, rectb.y=1, rectb.w=1, rectb.h=1; rectc.x=1, rectc.y=1, rectc.w=1, rectc.h=1; Rect Resrect=threeintersection (RECTA,RECTB,RECTC); if(resrect.x!=-1){//have intersectcout<<"Resrect.x:"<<resRect.x<<Endl; cout<<"Resrect.y:"<<resRect.x<<Endl; cout<<"RESRECT.W:"<<resRect.x<<Endl; cout<<"resRect.h:"<<resRect.x<<Endl; } Elsecout<<"Not intersect"<<Endl; GetChar ();}

Test Case 1 output: not intersect;

Test Case 2 Output:

Resrect.x:1

Resrect.y:1

Resrect.w:1

Resrect.h:1

An algorithm problem-to find the intersection rectangle of three rectangles.