Intersecting points of line segments in a plane

Question: In the plane coordinate system, given two line segments, and the coordinates of the endpoints of the two line segments are known, determine whether the two line segments intersect? If they intersect, find the coordinates of the intersection point.

First, we list all the scenarios when two line segments intersect, as shown in the figure below:

 There are 8 scenarios in total.

If we analyze these scenarios carefully, we will find that the 5th, 6th, 7th, and 8th scenarios have a common feature: there is a coincidence of line segment endpoints, while the remaining four scenarios do not exist. of.

In the case of overlapping endpoints

Let's first analyze the scenario with overlapping endpoints. 

When an endpoint is found to overlap, we then judge whether the remaining endpoints are also equal.

If the other endpoint is also equal, it is regarded as the seventh scenario , that is, the two line segments are the same line segment and completely overlap;

If the other end points are not equal, we connect the two unequal points into a vector (in the 5th, 6th, and 8th scenes in the figure below, it means that the points A and C are connected into a vector, of course, the same is true for connecting into a \overrightarrow{AC}vector \overrightarrow{CA}In the figure, we use red to indicate the new vector connected by non-coincident points, and use green to indicate the vector represented by the original line segment), as shown in the following figure:

 We then \overrightarrow{AC}cross-multiply the vector with any of the original line segment vectors

If the result of the cross product is not 0, it means that the new vector \overrightarrow{AC}is not parallel to the line segment used for the cross product, so it can be inferred that it is the fifth scenario .

 If the result of the cross product is 0, it means that the new vector \overrightarrow{AC}is parallel to the line segment of the cross product, that is, enter the sixth and eighth cases.

At this time, we use point multiplication to judge whether the remaining two points are on the same side of the coincidence point (that is, whether points A and C in the figure are on the same side of the coincidence point). Take the coincident point as the tail of the vector, that is, the composition of the vector \overrightarrow{BA}and the vector \overrightarrow{BC}(it can be called a vector \overrightarrow{DC}, because point B and point D coincide) for point multiplication.

If the inner product is greater than 0, it is the same side, that is, the eighth scenario ; if the inner product is less than 0, that is the sixth scenario .

Cases where there is no overlap of endpoints

After discussing the situation where there is overlap of endpoints, let's discuss the scenario where there is no overlap of endpoints.

 We first take one of the two line segments as the basic calculation vector. Assuming that the line segment AB is taken as the basic calculation vector \overrightarrow{AB}, we sequentially determine whether the two endpoints on another line segment are collinear with the line segment AB by means of cross multiplication, such as

\overrightarrow{AB}\times \overrightarrow{AC}\overrightarrow{AC}(Vectors can also be used in the formula \overrightarrow{BC}),

If the outer product is equal to 0, it means point C is collinear with line segment AB;

If the outer product is not equal to 0, it means that point C is not collinear with line segment AB.

In the same way, \overrightarrow{AB}\times \overrightarrow{AD}(the vector in the formula \overrightarrow{AD}can also be a vector \overrightarrow{BD}) is the same, you can find out whether the point D is collinear with the line segment AB.

In the same way, we choose another line segment as the basic calculation vector \overrightarrow{CD}, and execute the above process again, we can know the relative position of point A and point B and line segment CD

Combining the two calculation results, we can know the relative position between the two line segments.

\overrightarrow{AB}When calculating vectors based on vectors , we can know the positions of points C and D relative to line segment AB;

\overrightarrow{CD}When calculating vectors based on vectors , we can know the positions of points A and B relative to line segment CD;

The necessary and sufficient conditions for the first scenario are:

The line segment AB intersects the line where CD is located, that is, point A and point B are respectively on both sides of line CD;

Moreover, the line segment CD intersects the line AB, that is, point C and point D are on both sides of line AB;

The second scenario is that there is only one point collinear with another line segment, then we only need to judge whether the coordinate range of the collinear point is within the coordinate range of the two endpoints of the line segment, and if so, the intersection point is this co-linear line point.

The third and fourth scenarios mean that the four points are all collinear, so we only need to sort and judge the coordinates of each endpoint to see if it is the third scenario, the fourth scenario, or other scenarios that have no common intersection.

So far, we have sorted out all eight scenarios. The overall flow chart is as follows:

Calculate intersection coordinates 

Except for the first scenario, the other seven scenarios are very easy to find the intersection point and intersection interval. For the first scenario, our remaining work is mainly how to calculate the intersection coordinates.

 First of all, we connect the endpoints of the two intersecting line segments, and take one of the original line segments as the bottom line, and the two endpoints on the other line segment are respectively vertical high lines to the bottom line, as shown in the figure above. We take the line segment AB as the base, h1 is the height from point C to AB, and h2 is the height from point D to AB.

S_{\bigtriangleup ABC}=1/2\left \| \overrightarrow{AB} \right \|h_{1}=1/2\left | \overrightarrow{CA}\times \overrightarrow{CB} \right |

S_{\bigtriangleup ABD}=1/2\left \| \overrightarrow{AB} \right \|h_{2}=1/2\left | \overrightarrow{DA}\times \overrightarrow{DB} \right |

set t=h_{2}:(h_{1}+h_{2})=\left | \overrightarrow{DA}\times \overrightarrow{DB}\right |:(\left | \overrightarrow{DA}\times \overrightarrow{DB}\right |+\left | \overrightarrow{CA}\times \overrightarrow{CB}\right |),

but\overrightarrow{DO}=t(\overrightarrow{DC})

\left\{\begin{matrix} x_{O}-x_{D}=t(x_{C}-x_{D})\\ y_{O}-y_{D}=t(y_{C}-y_{D}) \end{matrix}\right.

The coordinates of the intersection point O can be obtained(x_{O},y_{O})

So far, the overall solution idea has been completed.

Today is New Year's Eve in 2023. I wish everyone a new year in the Year of the Rabbit.

I'll add the code later, if I haven't forgotten.

Guess you like

Origin blog.csdn.net/m0_74178120/article/details/128746324