The intersection of Any Polygon and rectangle is actually to judge whether multiple line segments are intersecting with this rectangle. The simple point is to determine whether a line segment is intersecting with each side of the rectangle. Now let's take a look at the intersection of a line segment and a line segment in the rectangle (above the horizontal line ):
(A/B = D/C in the figure. If I cannot understand it, I am speechless. I will go back to the middle school textbook of the ry .) The known red line segment in the figure is the line segment to be detected. The starting point is star, and the focus is end. Its coordinates are known. The blue line in the figure is the line to help you understand. In the figure, y0, X1, and X2 are the parameters of a line in the rectangle.CodeLet's take a look:
Public Static Bool Pointinline (vector2 point, vector2 [] vertices, Float Accuracy)
{
If (Point = Null | (Vertices = Null | Vertices. Length < 2 ))
Return False ;
Rect pointrect = New Rect ();
Pointrect. Width = Accuracy * 2 ;
Pointrect. Height = Accuracy * 2 ;
Pointrect. Center = Point;
ReturnLineintersectrect (vertices, pointrect );
}
The point parameter is the point to be detected, vertices is the set of detected line segments, and accuracy is its accuracy. If it is 0, it is accurate to the point. If there is some ambiguity, you can assign a value to this parameter. (When developing images, You need to place the cursor over an online line, which is too demanding. Therefore, you usually set the exact value. The smaller the value, the more accurate it is .) From this code, we can understand that N line segments vertices are used to check whether the rectangle with certain accuracy exists. Let's look at the code of lineintersectrect:
Public Static Bool Lineintersectrect (vector2 [] vertices, rect)
{
If (Vertices = Null | Vertices. Length < 2 ) | Rect = Rect. Empty)
Return False ;
For ( Int I = 0 ; I < Vertices. Length - 1 ; I ++ )
{
If (Checkrectline (vertices [I], vertices [I + 1 ], Rect ))
Return True ;
}
Return False ;
}
This code is easy to understand, that is, it checks every two points in the array whether it is intersecting with this rectangle. Next let's take a look at the code for checkrectline to detect its line segments and rectangles:
Private Static Bool Checkrectline (vector2 start, vector2 end, rect)
{
Bool Result = False ;
If (Rect. Contains (start) | Rect. Contains (end ))
Result = True ;
Else
{
Result | = Checkrectlineh (START, end, rect. lefttop. Y, rect. lefttop. X, rect. rightbottom. X );
Result | = Checkrectlineh (START, end, rect. rightbottom. Y, rect. lefttop. X, rect. rightbottom. X );
Result | = Checkrectlinev (START, end, rect. lefttop. X, rect. lefttop. Y, rect. rightbottom. y );
Result | = Checkrectlinev (START, end, rect. rightbottom. X, rect. lefttop. Y, rect. rightbottom. y );
}
Return Result;
}
The method of intersection between a line segment and a rectangle is used to check whether the four sides of a line segment and a rectangle overlap. Here, the checkrectlineh method is used to detect the top and bottom lines of the rectangle. Then, checkrectlinev is used to detect the left and right lines of the rectangle. Let's take a look at the code of the checkrectlineh function, and then look at the figure for analysis:
Private Static Bool Checkrectlineh (vector2 start, vector2 end, Float Y0, Float X1, Float X2)
{
// Straight line above the point
If (Y0 < Start. Y) && (Y0 < End. y ))
Return False ;
// Straight line below the point
If (Y0 > Start. Y) && (Y0 > End. y ))
Return False ;
// Horizontal line
If (Start. Y = End. Y)
{
// The horizontal line is at the same level as the point.
If (Y0 = Start. Y)
{
// Straight line on the left of the point
If (Start. x < X1) && (End. x < X1 ))
Return False ;
// The straight line is on the right of the vertical line X2
If (Start. x > X2) && (End. x > X2 ))
Return False ;
// Part or all of a straight line is between the point and the X2 vertical line.
Return True ;
}
Else // The horizontal line is different from the point.
{
Return False ;
}
}
// Diagonal lines
Float X = (End. x - Start. X) * (Y0 - Start. Y) / (End. Y - Start. Y) + Start. X;
Return (X > = X1) && (X <= X2 ));
}
After reading the code, let's look at the diagram at the beginning and help with the analysis. We can think of this function to check whether the intersection of X1 and Y0 and that of X2 and Y0 are intersection with the red line, if the intersection is exceeded, determine whether the intersection is within the range between X1 and X2. (According to the called parameters, we can know that it is the upper and lower edges.) according to the graph, we can get:
A = (end. x-start. x), B = (end. y-start. y), c = (y0-start. y), D = start. X + X. Thus, we can conclude that
X = (end. X-start. X) * (y0-start. Y)/(end. Y-start. Y) + start. X.
Similarly, the checkrectlinev analysis is reversed by X and Y. The following code is provided: (The analysis will not be detailed)
Private Static Bool Checkrectlinev (vector2 start, vector2 end, Float X0, Float Y1, Float Y2)
{
If (X0 < Start. X) && (X0 < End. X ))
Return False ;
If (X0 > Start. X) && (X0 > End. X ))
Return False ;
If (Start. x = End. X)
{
If (X0 = Start. X)
{
If (Start. Y < Y1) && (End. Y < Y1 ))
Return False ;
If (Start. Y > Y2) && (End. Y > Y2 ))
Return False ;
Return True ;
}
Else
{
Return False ;
}
}
Float Y = (End. Y - Start. Y) * (X0 - Start. X) / (End. x - Start. X) + Start. Y;
Return (Y > = Y1) && (Y <= Y2 ));
}
Note: the source code of vortex2d is used for analysis. vector2 is actually a point type, but this vector2 encapsulates more methods. Its rect type is not the system rect, but also the rect type that vortex2d encapsulates more methods. If you encounter compilation errors when using this part of the code, make sure that you modify the settings. Here is an idea. Once you understand this idea, you naturally know how to modify your own code.
Original Works from the effort to be lazy, reprint please explain the source of the article: http://www.cnblogs.com/kfarvid/