In software development of geographic information systems, it is often necessary to obtain the intersection, intersection, closed area and closed collection among points, lines, and surfaces, the algorithm based on vector dot multiplication and cross multiplication is efficient when the conditions such as the vertex position and the normal vector are given in actual work.
First, the derivation of the basic formula is given.
Vector combination rate and exchange rate:
U + (V + W) = (U + V) + W;
U + V = V + U;
If the start and end points of a line segment are set to p and q, any point of R in the middle can be expressed:
R = P + R (Q-P); (0 <r <1)
A common parametric equation composed of P0, P1, and P2 can be expressed as one of the following forms:
P (S, T) = (1-s-t) P0 + SP1 + TP2, where S, T is the parameter
The vector length is defined:
Set v = (a, B); then | v | = SQRT (A * A + B * B); (that is, the square of the sum of squares of data in each axis)
The normalization of vectors is u = V/| v |, so that the vector length is 1.
Point multiplication definition of a vector:
Vw = v1w1 + v2w2, V = (V1, V2), W = (W1, W2 );
This definition can be applied from two-dimensional space to three-dimensional and other high-order spaces.
We know that p (x, y) on a two-dimensional plane rotates an angle α on the coordinate axis, and the new coordinate is P (xnew, ynew)
If the distance from point P is R, and the angle between the point and the original X axis is delta
X = rcos delta, y = rsin delta;
The new coordinates can be obtained: xnew = rcos (delta-α) = rcos Delta cos α + rsin Delta sin α = xcos α + Ysin α;
Ynew = rsin (delta-α) = rsin Delta cos α-rcos Delta sin α = ycos α-xsin α;
The following formula is widely used:
Vw = | v | w | cos cosine, cosine is the intersection of two vectors;
If v = (V1, 0) falls on the X axis, vw = v1w1 + 0w2 = v1w1;
If the V point falls on the axis, | v | = V1, | w | cos kernel = W1; therefore, vw = | v | w | cos kernel;
If it is normal, that is, V = (V1, V2), then we can rotate the original axis angle after Delta so that the V point falls on the new x axis,
Because: (v1w1 + v2w2)/| v | = w1cos Delta + w2sin Delta = W point X axis value under the new coordinate system = | w | cos then
Therefore, v1w1 + v2w2 = | v | w | cos kernel;
In normal use, cos cosine = VW/| v | w | can be used to determine the intersection of two vectors. If VW is 0, it indicates that the two vectors are perpendicular to each other. If the VW is greater than 0, the intersection is acute | <90 degrees.
Let the vector V (a, B), after rotating it 90 degrees, u = (acos90 + bsin90, ycos90-xsin90) = (B,-); this method is used to obtain the vertical vector of the vector.
The formula is derived from vw = | v | w | cos cosine, and so on. | v | w | what is sin cosine? The derivation is as follows:
| W | sin frequency = Y axis value of w in the new coordinate system = w2cos Delta-w1sin Delta = w2v1/| v |-w1v2/| v | =
(V1w2-v2w1)/| v |, that is
V1w2-v2w1 = | v | w | sin cosine, cosine is the intersection of two vectors;
In normal use, this formula can be used to calculate the area of a triangle and a quadrilateral. It can also determine the positional relationship between the two vectors. If the calculated v1w2-v2w1 is 0, it means that the intersection of the two vectors is 0 degrees or 180 degrees, that is, collinearity. If the calculation result is greater than 0, the intersection of the two vectors is between 0 degrees and 180 degrees.
Reference code for finding the positional relationship between vectors and Triangle Area:
// Input: three points P0, P1, and p2
// Return:> 0 for P2 left of the line through P0 and P1
// = 0 for P2 on the line
// <0 for P2 right of the line
Double cdemalgorithm: isleft (point P0, Point P1, point P2)
{
Return (p1.x-Snapshot X) * (p2.y-Snapshot y)
-(P2.x-p0.x) * (p1.y-p0.y); // use the right-hand rule to determine the direction of the Cross multiplication.
}
Double cdemalgorithm: orientation2d_triangle (point v0, point V1, point V2)
{
Return isleft (v0, V1, V2); // you can call this operation to determine the positional relationship.
}
Double cdemalgorithm: area2d_triangle (point v0, point V1, point V2)
{
Return isleft (v0, V1, V2)/2.0; // calculate the Triangle Area
}
Double cdemalgorithm: dist3d_line_to_line (line L1, line l2)
{
Vector u = l1.p1-l1.p0;
Vector v = l2.p1-l2.p0;
Vector W = l1.p0-l2.p0;
Double A = dot (u, u); // always> = 0
Double B = dot (u, v );
Double C = dot (V, V); // always> = 0
Double D = dot (u, W );
Double E = dot (V, W );
Double D = A * C-B * B; // always> = 0
Double SC, TC;
// Compute the line parameters of the two closest points
If (d <EPS) {// almost parallel
SC = 0.0;
TC = (B> C? D/B: E/C );//
}
Else {
SC = (B * E-C * D)/d;
TC = (A * E-B * D)/d;
}
// SC and TC indicate the proportional parameters on the two straight lines respectively.
Vector dp = W + (SC * u)-(TC * V); // = L1 (SC)-L2 (TC)
Return SQRT (dot (DP, DP); // return the closest distance
}
Figure: