This paper provides a method to determine the intersection of two line segments in the same plane. For example:
If the two line segments overlap, the two lines must cross each other.
If P1P2 cross-sets Q1Q2, the vectors (P1-Q1) and (P2-Q1) are on both sides of the vector (Q2-Q1,
(P1-Q1) × (Q2-Q1) * (P2-Q1) × (Q2-Q1) <0.
The preceding formula can be changed to (P1-Q1) × (Q2-Q1) * (Q2-Q1) × (P2-Q1)> 0.
When (P1-Q1) × (Q2-Q1) = 0, it indicates that (P1-Q1) and (Q2-Q1) are in the same line, but because they have passed the fast rejection test, therefore, P1 must be on the online segment Q1Q2;
Similarly, (Q2-Q1) × (P2-Q1) = 0 indicates that P2.
Therefore, the basis for determining P1P2 cross-site Q1Q2 is:
(P1-Q1) × (Q2-Q1) ** (Q2-Q1) × (P2-Q1)> = 0.
Similarly, the basis for determining q1q2 cross-site P1P2 is:
(Q1-P1) × (P2-P1) * (P2-P1) × (Q2-P1)> = 0.
Code implementation:
# Include <iostream> <br/> # include <math. h> <br/> using namespace std; </p> <p> # define ABS_FLOAT_0 0.0001 </p> <p> struct pointf <br/>{< br/> float x; <br/> float y; <br/>}; <br/> struct Vectorf3D <br/>{< br/> float x; <br/> float y; <br/> float z; <br/>}; </p> <p> bool IsIntersectLine (<br/> const pointf p1, // start point of Line Segment 1 <br/> const pointf p2, // Line Segment 1 end point <br/> const pointf q1, // Line Segment 2 start point <br/> const pointf q2 // Line Segment 2 end point <br />) <Br/>{< br/> Vectorf3D v1, v2, v3, v4; <br/> v1.x = (p1.x-q1.x ); <br/> v1.y = (p1.y-q1.y); <br/> v1.z = 0.0; </p> <p> v2.x = (q2.x-q1.x ); <br/> v2.y = (q2.y-q1.y); <br/> v2.z = 0.0; </p> <p> // calculate v1, <br/> v3.x = v1.y * v2.z-v1.z * v2.y; <br/> v3.y = v1.z * v2.x-v1.x * v2.z; <br/> v3.z = v1.x * v2.y-v1.y * v2.x; </p> <p> v1.x = (p2.x-q1.x ); <br/> v1.y = (p2.y-q1.y); <br/> v1.z = 0.0; </p> <p> // Calculate v2 and v1. <br/> v4.x = v2.y * v1.z-v2.z * v1.y; <br/> v4.y = v2.z * v1.x-v2.x * v1.z; <br/> v4.z = v2.x * v1.y-v2.y * v1.x; </p> <p> // calculate v3, point product of v4 <br/> float fTemp1 = v3.x * v4.x + v3.y * v4.y + v3.z * v4.z; </p> <p> v1.x = (q1.x-p1.x ); <br/> v1.y = (q1.y-p1.y); <br/> v1.z = 0.0; </p> <p> v2.x = (p2.x-p1.x ); <br/> v2.y = (p2.y-p1.y); <br/> v2.z = 0.0; </p> <p> // calculate v1, v2 cross multiplication <br/> v3.x = v1.y * v2.z- V1.z * v2.y; <br/> v3.y = v1.z * v2.x-v1.x * v2.z; <br/> v3.z = v1.x * v2.y-v1.y * v2.x; </p> <p> v1.x = (q2.x-p1.x); <br/> v1.y = (q2.y-p1.y); <br/> v1.z = 0.0; </p> <p> // calculate v2, v1's cross multiplication <br/> v4.x = v2.y * v1.z-v2.z * v1.y; <br/> v4.y = v2.z * v1.x-v2.x * v1.z; <br/> v4.z = v2.x * v1.y-v2.y * v1.x; </p> <p> // computing v3, point product of v4 <br/> float fTemp2 = v3.x * v4.x + v3.y * v4.y + v3.z * v4.z; </p> <p> if (fTemp1> = ABS_FLOAT_0) & (fTemp2> = ABS_FLOAT_0) <br/>{< br/> return true; <br/>}< br/> else <br/>{< br/> return false; <br/>}</p> <p> void main (void) <br/>{< br/> pointf p1, p2, q1, q2; <br/> p1.x = 0.0; <br/> p1.y = 0.0; <br/> p2.x = 30.0; <br/> p2.y = 30.0; </p> <p> q1.x = 0.0; <br/> q1.y = 20.0; <br/> q2.x = 20.0; <br/> q2.y = 0.0; </p> <p> if (IsIntersectLine (p1, p2, q1, q2) <br/>{< br/> cout <"intersection of line segments! "<Endl; <br/>}< br/> else <br/> {<br/> cout <" line segments are not intersection! "<Endl; <br/>}< br/>}