Point multiplication and cross multiplication of vectors are useful mathematical tools, but they also have limitations. The key is that vector point Multiplication can get the angle between two vectors, instead of the rotation angle. This angle has no direction and the range is [0-Pi], which is often not what we want, in practice, we often need to calculate the exact angle from Vector P1 to the same direction as vector P2 in the counterclockwise direction. I define this angle as the rotation angle. The calculation of the rotation angle requires both the angle and the cross multiplication of two vectors to determine the angle-direction relationship between P1 and P2.
For the relationship between the cross-multiplication symbol and the angle direction of the vector, see 《 Algorithm Introduction, I only come to the conclusion that P1 * P2 = x1y2-X2 Y1 =-P2 * p1if P1 * P2 is positive, then P1 is clockwise from p2 with respect to the origin (0, 0); if this cross product is negative, then P1 is counterclockwise from P2. In addition, collinear is considered) but arecosine can clearly distinguish between 0 and PI, so as to draw a conclusion in advance as a special case.
PS. Because it is mainly used by OpenGL, the returned angle value is
/*************************************** *********************************/
/* Author: Navy @ HUST
* File: angle. c
* Date: 5/12/2007
* Desc: vector rotate angle calculation
/*************************************** * ********************************/# I nclude <stdio. h> # I nclude <math. h> double getrotateangle (double X1, double Y1, double X2, double Y2); int main (INT argc, char ** argv)
{
Double x1, x2, Y1, Y2;
Double Dist, Dot, degree, angle;
Freopen ("angle. In", "r", stdin );
While (scanf ("% lf", & X1, & Y1, & X2, & Y2) = 4 ){
Printf ("the rotate angle from P1 to P2 is %. 3lf \ n ",
Getrotateangle (x1, Y1, X2, Y2 ));
}
}
/*
* Rotation angle between two vectors
* First, define several mathematical concepts:
* 1. The polar axis rotates clockwise in the positive direction.
* 2. the angle Theta between two vectors refers to (a ^ B)/(| A | * | B |) = cos (theta), 0 <= Theta <= 180 degrees, and there is no direction
* 3. The rotation angle of the two vectors refers to the angle of rotation from the vector P1, Which is clockwise rotated to the vector P2. The range is 0 ~ 360 degrees
* Calculate the rotation angles from P1 to P2. The algorithm is as follows:
* First, get the angle between the two vectors through the dot multiplication and arccosine.
* Then, determine the positional relationship between two vectors by difference multiplication.
* If P2 is clockwise in P1, The arccose angle value is returned, ranging from 0 ~ 180.0 (according to the right hand theorem, a positive area can be formed)
* Otherwise, the value of 360.0-arecose is returned, and the value from 180 to 360 is returned. (According to the right hand theorem, the area is negative)
*/
Double getrotateangle (double X1, double Y1, double X2, double Y2)
{
Const double Epsilon = 1.0e-6;
Const double nypi = ACOs (-1.0 );
Double Dist, Dot, degree, angle;
// Normalize
Dist = SQRT (x1 * X1 + Y1 * Y1 );
X1/= DIST;
Y1/= DIST;
Dist = SQRT (X2 * X2 + y2 * Y2 );
X2/= DIST;
Y2/= DIST; // dot product
Dot = x1 * X2 + Y1 * Y2; If (FABS (Dot-1.0) <= epsilon)
Angles = 0.0;
Else if (FABS (DOT + 1.0) <= epsilon)
Angle = nypi;
Else {
Double Cross;
Angle = ACOs (DOT );
// Cross Product
Cross = x1 * Y2-X2 * Y1;
// Vector P2 is clockwise from Vector p1
// With respect to the origin (0.0)
If (Cross <0 ){
Angle = 2 * nypi-angle;
}
}
Degree = angle * 180.0/nypi;
Return degree;
}