Line segment intersection, dot product, cross product

Line segment intersection

Give the coordinates of two points of several line segments, and output the number of intersection points of the two line segments.
Condition 1: The first line segment is completely above (below) the second line segment, it can be judged that there is no intersection.
Condition 2: If AB and CD intersect, then A and B must be on both sides of CD, so (CD×CA)(CD×CB) <0.
C and D must be on both sides of AB, so (AC×AB)(AD×AB) <0.
Suppose A(x1,y1), B(x2,y2), C(x3,y3), D(x4,y4).
CA = (x1-x3, y1-y3), CB = (x2-x3, y2-y3).
CD = (x4-x3, y4-y3).
AC = (x3-x1, y3-y1), AD = (x4-x1, y4-y1).
AB = (x2-x1, y2-y1).
So, (CD×CA)(CD×CB) = ((x4-x3) (y1-y3)-(x1-x3) (y4-y3))((x4-x3) (y2-y3)-(x2 -x3) (y4-y3))> 0 holds,
or (AC×AB)(AD×AB) = ((x3-x1) (y2-y1)-(x2-x1) (y3-y1))( (x4-x1) (y2-y1)-(x2-x1) (y4-y1))> 0 is true, there
is no intersection.
All other situations have intersections.

Dot multiplication:

x=(a1,a2,…,an),y=(b1,b2,…,bn)
x·y=a1b1+……+anbn .

Cross product:

Let the coordinates of the two vectors be u (u1, u2, u3), v(v1, v2, v3).
Then u×v = (u2 v3-u3 v2)-(u1 v3-u3 v1) + (u1 v2-u2 v1).
Two-dimensional vector cross product formula: x1 y2-x2 y1.
Obtain
the clockwise and reverse relationship between the two vectors: if l1 × l2> 0, then l1 is in the clockwise direction of l2;
if l1 × l2 <0, then l1 is in the counterclockwise direction of l2;
if l1 × l2 = 0, Then l1 and l2 are collinear (may be in the same direction, or reverse);

Guess you like

Origin blog.csdn.net/qq_47783181/article/details/112908676