A unit vector
In 3D Cartesian coordinates, basically a vertex is a position in XYZ coordinate space, and a given position in space is defined by a single XYZ, and XYZ is a vector.
Unit vector
If a vector is not a unit vector, and we scale it to 1, then this process is called normalization, and to normalize a vector is to reduce its magnitude to 1, also called a unit vector. #OpenGL how to define vector Math3D library, there are two data types, can represent a 3 and 4 dimensional vector.
- M3DVector3f can represent a three-dimensional vector (x,y,z).
- M3DVector4f can represent a four-dimensional vector (x,y,z,w).
In the typical case, the w coordinate is set to 1.0. The values of x,y, and z are scaled by dividing by w. Dividing by 1.0 essentially doesn’t change the values of x,y, and z.
Reference code
// Three dimensional vector/four dimensional vector declaration
typedef float M3DVector3f[3];
typedef float M3DVector4f[4];
// Declare a three-dimensional vector M3DVector3f: type vVector: variable name M3DVector3f vVector;
// Declare a four-dimensional vector and initialize a four-dimensional vector
M3DVector4f vVertex = {0.0.1.1};
// Declare a three-component vertex array, such as generating a triangle
M3DVector3f vVerts[] = {
0.5 f.0.0 f.0.0 f.0.5 f.0.0 f.0.0 f.0.0 f.0.5 f.0.0 f
};
Copy the code
Vector 2, dot product
Vectors can be added or subtracted. But there is one operation in vectors that is very valuable to use in development, called dot product. The dot product can only happen between two vectors;
The dot product between two unit vectors yields a scalar (not a three-dimensional vector, a scalar). It’s the Angle between two vectors.
Prerequisites:
- Two vectors have to be unit vectors.
- Action: Dot product between 2 3d vectors.
- Structure: Returns a value in the range -1, 1. This value is the cosine of the Angle.
How do you unit vectors?
(x/|xyz|, y/|xyz|, z/|xyz|);
Copy the code
Using a nonzero phasor divided by its magnitude (the length of the vector), you get a unit vector with the same direction.
//1. M3dDotProduct3 is the dot product between two vectors;
float m3dDotProduct3(const M3DVector3f u,const M3DVector3f v);
/ / 2. M3dGetAngleBetweenVector3 the radian of Angle between two vectors can be obtained values;
float m3dGetAngleBetweenVector3(const M3DVector3f u,const
M3DVector3f v);
Copy the code
Vector, cross product
The cross product between vectors is also a very useful calculation method in business development. The cross product between the two vectors will give you another vector, and the new vector will be perpendicular to the plane defined by the two vectors, but it won’t have to be a unit vector.
- Premise: 2 ordinary vectors;
- Action: vector and vector cross product;
- Vector (vector perpendicular to the plane defined by the original 2 vectors);
//1. The m3dCrossProduct3 function obtains the cross product between two vectors to produce a new vector.
void m3dCrossProduct3(M3DVector3f result,const M3DVector3f u ,const M3DVector3f v);
Copy the code