Determine whether two line segments intersect

Two segments and only one common point, and this point is not any endpoint of a line when said two line segments is strictly intersect.

That endpoint can be used as the line does not intersect the intersection of strict, but not strict intersect article does not discuss, discuss only with strict intersection (even if they are not very different in the algorithm).

In determining whether two line segments intersect, we often experiment with quick rejection straddles two experimental methods, rapid rejection experiments can quickly exclude dropped segments do not intersect, but did not become law necessary and sufficient conditions for the segment intersected , after the rapid rejection of the experiment will be able to fully connect straddles the experiment to determine whether two line segments intersect, but in fact only a way to straddle this experiment can also be used as necessary and sufficient conditions to determine the line intersect.

1. Fast exclusion experiment:
Here Insert Picture Description

Assuming line segment P1, P2 is a rectangular diagonal for R, and then to Q1, Q2 T diagonal as rectangular, two rectangular when two line segments do not intersect certainly do not intersect, i.e., a line segment intersecting a necessary condition rectangles intersect.

Coordinates P1 (p1x, p1y), P2 coordinates (p2x, p2y), the coordinates of Q1 (q1x, q1y), the coordinates of Q2 (q2x, q2y).

That rectangles intersect condition is:

min(p1x,p2x) <= max(q1x,q2x) &&
min(q1x,q2x) <= max(p1x,p2x) &&
min(p1y,p2y) <= max(q1y,q2y) &&
min(q1y,q2y) <= max(p1y,p2y);

As long as written in the programming min (int a, int b) with max (int a, int b) these two functions, the above conditions and then put if () statement can be initially identified segments have no intersecting possibility.

(1) rapid rejection experiments
provided in the diagonal line P1P2 rectangle R, as provided in the line segment Q1Q2 diagonal rectangle T, R and T are disjoint if, obviously not intersect two segments.
(2) straddles the experiment
if two line segments intersect, the two segments necessarily mutually straddling each other. If the straddle P1P2 of Q1Q2, the vector (P1 - Q1) and (P2 - Q1) is located in the vector (Q2 - Q1) on both sides, namely (P1 - Q1) × (Q2 - Q1) * (P2 - Q1) × ( Q2 - Q1) <0. It can be rewritten as a formula (P1 - Q1) × (Q2 - Q1) * (Q2 - Q1) × (P2 - Q1)> 0. When (P1 - Q1) × (Q2 - Q1) = 0 , it indicates that (P1 - Q1) and (Q2 - Q1) collinear, but as it has been by flash exclusion test, so the line segment P1 certain of Q1Q2; Similarly, (Q2 - Q1) × (P2 - Q1) = 0 must be described on the line segment P2 Q1Q2. It is judged based Q1Q2 straddling P1P2 is :( P1 - Q1) × (Q2 - Q1) * (Q2 - Q1) × (P2 - Q1)> = 0. Similarly Q1Q2 straddling determination is based P1P2 :( Q1 - P1) × (P2 - P1) * (P2 - P1) × (Q2 - P1)> = 0. :

https://dev.gameres.com/Program/Abstract/Geometry.htm# vector cross product

Published 52 original articles · won praise 2 · Views 894

Guess you like

Origin blog.csdn.net/qq_44714572/article/details/97759038