Recently, we need to use the simple application of rectangle intersection algorithm, so we take a very simple algorithm for the novice’s reference. Why is it for the novice’s reference? Because the efficiency of this algorithm is not very high, but this algorithm only has three simple lines. The program uses two methods to judge whether there is overlap / intersection. If you are interested, you can take a look and leave a message if you think there is a bug. The code is for reference only.

The method of rectangle in C is rectangl (starting point coordinate, rectangle size) or rectangl (starting point X coordinate, starting point y coordinate, rectangle width, rectangle height), and the starting point is the upper left corner of the rectangle area.

##### Method 1

Let’s call it “Jingzi method”. Lengthen the four sides of one rectangle to form a “Jingzi” (as shown in the figure) and divide it into nine areas, and then determine whether the points of the other rectangle are in the rectangle.

1. When the starting coordinate of the generated rectangle is in the 3, 6, 7, 8, 9 region, the generated rectangle must not intersect;

2. When the coordinates of the starting point of the generated rectangle are in the 1, 2, 4 area, ensure that**The X coordinate of the lower right corner of the new rectangle is smaller than the X coordinate of the starting point of the rectangle to be compared**Or**The Y coordinate of the lower right corner of the new rectangle is smaller than the Y coordinate of the starting point of the rectangle to be compared**Yes.

3. When the generated rectangle is in the region of 5, as long as the X and Y coordinates of the lower right corner of the new rectangle are smaller than the X and Y coordinates of the lower right corner of the rectangle to be compared;

4. It is not necessary to consider Article 3 when considering whether the two rectangles overlap. When it is necessary to consider whether they are included, it is necessary to use Article 3 to judge.

Reference code:

```
Int flag = 0; // set the tag value to overlap by default
If (rectangle 1. X > rectangle 2. X + rectangle 2. Width | rectangle 1. Y > rectangle 2. Y + rectangle 2. Height) Flag + +; // the initial point is in 3, 6, 7, 8, 9 areas
Else if (rectangle 1. X + rectangle 1. Width < rectangle 2. X | rectangle 1. Y + rectangle 1. Height < rectangle 2. Y) Flag + +; // the initial point is in the 1 area
Else if (rectangle 1. X > rectangle 2. X & & rectangle 1. Y > rectangle 2. Y & & rectangle 1. X + rectangle 1. Width < rectangle 2. X + rectangle 2. Width & & rectangle 1. Y + rectangle 1. Height < rectangle 2. Y + rectangle 2. Height) Flag + +; // the initial point is in 5 areas
If (flag = = 0) textbox1.text = "overlap";
Else textbox1.text = "do not overlap";
```

Results demonstration:

##### Method two

This is much simpler. Using intersectswith directly can determine whether there is overlap. Is it much simpler? However, it can only judge whether the two rectangles overlap, and the situation of inclusion is also included in the overlap. When using this method, method 1 can be considered.

```
If (rectangle 1. Intersectswith (rectangle 2)) textbox1.text = "overlap";
Else textbox1.text = "do not overlap";
```

Results demonstration:

##### Epilogue

This code does not consider the problem of boundary tangency in detail. It needs readers to consider and modify it again. It only provides some new ideas or considers more complex promotion and application. At the same time, the source code is attached at the end of the paper for reference. In addition, due to the limited time and level, the code inevitably has some small problems that we hope readers will understand.

Source code: https://files-cdn.cnblogs.com/files/shenyuanfeng/rectangleintersect.zip