Computational geometry-------cross product

Cross-product application

The geometric meaning of the cross product
|c|=|a×b|=|a| |b|sinα (α is the angle between the a and b vectors).
Insert picture description here
Given the vectors A and B, the result of A × B is shown in red According to the right-hand spiral rule, the finger points to A and closes in the direction of B. The thumb is positive for upwards and negative for downwards .

Vector p=(a,b), q=(c,d)
p × q = ad - bc

application

1. Calculate the area

Insert picture description here
Vector p=(a,b), q=(c,d)
p × q = ad - bc =-q × p
The size of the cross product is equal to 2 times the area of ​​the triangle (the result of the cross product is the absolute value )

2. Judging the relationship between the point and the straight line

Let vector P=(x1,y1), Q=(x2,y2)

P × Q
①If the result is positive, P is in the clockwise direction of Q;
②If the result is negative, P is in the counterclockwise direction of Q;
③If the result is zero, P and Q are collinear, that is, parallel, May be in the same direction or reverse

3. Find the intersection of straight lines

Suppose line 1 passes through two points (x1, y1), (x2, y2), and line 2 passes through two points (x3, y3), (x4, y4).
First use cross multiplication to determine whether the two lines intersect:

int a=x2-x1,b=y2-y1;
int c=x4-x3,d=y4-y3;
int cha=a*d-b*c;

If the cross product cha is not equal to 0, then intersect, if it is 0, then parallel or coincide.

Find the point of intersection:

int a1=y1-y2;
int b1=x2-x1;
int c1=x1*y2-x2*y1;

int a2=y3-y4;
int b2=x4-x3;
int c2=x3*y4-x4*y3;

int D1=a2*b1-a1*b2;
int D2=a1*b2-a2*b1;

double X=1.0*(b2*c1-b1*c2)/D1;
double Y=1.0*(a2*c1-a1*c2)/D2;
cout<<"交点为:"<<X<<' '<<Y<<endl;

Guess you like

Origin blog.csdn.net/qq_40534166/article/details/99899524