Analyzing two rectangular easily determined intersection / overlapping C #

Recent applications need to use a simple rectangle intersection algorithm, so specifically to get out of a very simple algorithm for novice reference, why is the reference to the novice it because the efficiency of the algorithm is not very high, but this algorithm only the simple three Row. Program uses two methods to determine whether the overlap / intersect, if interested can look at, if they feel there is bug can leave a message. Code for reference only.

A method in C # rectangle is Rectangl (coordinates of the starting point, the size of the rectangle) or Rectangl (start point x coordinate, y coordinate of the starting point, the rectangular width, high squareness), starting point for the top left corner of the rectangular area.

 

method one

Tentatively called a "pound Law" it, which extend to form a rectangular quadrilateral a "well" (as shown) and is divided into nine areas, sub-area to determine whether another point of the rectangle within this rectangle.

1. When the rectangle start coordinate point generation during 3,6,7,8,9 area, the generated rectangular necessarily disjoint;

2. When generating coordinates of the starting point of a rectangular region when 1,2,4 ensure x-coordinate of the lower right corner of the rectangle is smaller than the new x coordinate of the starting point of the rectangle to be compared  or   y coordinate of the lower right corner of the rectangle is smaller than the new to be compared the starting y coordinate rectangular  to;

3. When located in the generated rectangular area 5, as long as the lower right corner of the rectangle new x and y coordinates to be compared is less than the lower right corner of a rectangular x and y coordinates can;

4. When considering the need to consider whether the two rectangles overlap section 3, when the contents need to consider whether the article 3 is determined.

 

Reference Code:

. 1              int In Flag = 0 ; // set flag value, the default overlap 
2              IF (rectangle1.X> rectangle2.X + rectangle2.Width || rectangle1.Y> rectangle2.Y + rectangle2.Height) In Flag ++; // initial point 3,6,7,8,9 region 
. 3              the else  IF (+ rectangle1.Width rectangle1.X <+ rectangle1.Height rectangle1.Y rectangle2.X || <rectangle2.Y) in Flag ++; // initial point in the region 
. 4              the else  IF (rectangle1.X> rectangle2.X && rectangle1.Y> rectangle2.Y && rectangle1.X + rectangle1.Width <rectangle2.X + + rectangle1.Height rectangle2.Width && rectangle1.Y <rectangle2.Y + rectangle2.Height) ++ in Flag; // initial point in the region of 5 
5  
. 6             IF (In Flag == 0 ) textBox1.Text = " overlap " ;
 . 7              the else textBox1.Text = " do not overlap " ;

The results demonstrate:

 

 

 

Method Two

This is much simpler, direct use IntersectsWith direct method can determine whether the overlap is not much simpler? But it only determines whether the two rectangles overlap, the contents of the case can be considered in the overlap, when using this method requires a conceivable method.

. 1  IF (rectangle1.IntersectsWith (rectangle2)) textBox1.Text = " overlap " ;
 2              the else textBox1.Text = " do not overlap " ;

 

The results demonstrate:

 

 

 postscript

 This code does not consider the issue in detail tangent to the border, coupled with the need to consider changing needs of readers, it is only new to offer some ideas or to consider a more complex application, and with source code to the end of the article for reference. In addition time is limited, the code is inevitable that some small problems hope readers generosity.

 

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

 

Guess you like

Origin www.cnblogs.com/shenyuanfeng/p/11774418.html