210 likes | 525 Views
3D Vector & Matrix. Chapter 2. Vector. Definition: Vector is a line segment that has the direction. The length of the line segment is called the magnitude or size of the vector. . If two vectors have the same magnitude and direction, then they are considered as the same vectors.
E N D
3D Vector & Matrix Chapter 2
Vector Definition: Vector is a line segment that has the direction. The length of the line segment is called the magnitude or size of the vector. If two vectors have the same magnitude and direction, then they are considered as the same vectors
Vector3(float, float, float) 3D DirectX has class If we set the beginning point of a vector at origin, then the ending point of the vector is uniquely defined. Therefore we can use vector to define a point, like vertex. public Vector3 ( float valueX, float valueY, float valueZ )
Addition of Vector3 We can directly use + to add two vectors.
Subtraction of Vector3 We can directly use - to subtract two vectors.
Dot Product of Vector3 staticfloat Dot ( Vector3 left, Vector3 right ) If Vector3 v1 = new Vector3(a1, b1, c1); And Vector3 v2 = new Vector3(a2, b2, c2); Then v1o v2 = a1×a2 + b1×b2 + c1×c2
Cross Product of Vector3 staticVector3 Cross( Vector3 left, Vector3 right ) q
Other methods of Vector3 float Length( ) Return the length of the vector Vector3 Normalize( ) Return the vector that has the same direction and length=1 Vector3 Multiply(floata ) We can directly use * to multiply a vector by a float number
3D Matrix struct Matrix{ float M11, M12, M13, M14; float M21, M22, M23, M24; float M31, M32, M33, M34; float M41, M42, M43, M44; }; An array of floats that represent a 4x4 matrix, where i is the row number and jis the column number. For example, M34 means the component in the third row and fourth column
Matrix Properties float Determinant { get; } Call Matrix.Identity to get an identity matrix. static Matrix Identity { get; } static Matrix Zero{ get; } Call Matrix.Zero to get an empty matrix.
Matrix Rotation Methods static Matrix RotationX (floatq) Call Matrix.RotationX to get a matrix that represents the rotation about x-axis by angle q static Matrix RotationY (floatq) static Matrix RotationZ (floatq) static Matrix RotationAxis(Vector3 axis, floatq) Get a rotation matrix about any axis
Matrix Translation Methods static Matrix Translation(float a,float b,float c) This is the operation (x, y, z) (x+a, y+b, z+c) The matrix is static Matrix Translation(Vector3 v) Because that
Matrix Transpose Method static Matrix Transpose(Matrix m) If we have matrix Then its transpose matrix is
Matrix Inverse Method Matrix Invert() static Matrix Invert(Matrix m ) Let If A*B = I (identity matrix) , then B is call the inverse matrix of A. Also A is the inverse matrix of B.
Matrix operator Method Matrix has +, -, * operators static Matrix operator + ( Matrix m1, Matrix m2) static Matrix operator - ( Matrix m1, Matrix m2) static Matrix operator * ( Matrix m1, Matrix m2) Which means they can directly do operations Matrix m1, m2; Matrix m_sum = m1 + m2; Matrix m_minus = m1 - m2; Matrix m_product = m1* m2;
World Coordinate Every Matrix object is a transformation of the 3D space. The following is the code to set this transformation. Matrix m1, m2; m_device.Transform.World = Matrix.Identity(); Draw Figure 1; // under transformation m1 m_device.Transform.World = m1; Draw Figure 2; // under transformation m1 Draw Figure 3; // under transformation m1 m_device.Transform.World = m2; Draw Figure 4; // under transformation m2 only In the above, the world coordinate of Figure 1 could also under transformation by Matrix m1;
Matrix combination We have two transformations whose matrices are m1 and m2. If applying those two transformations consecutively to our 3D world, we need to do Matrix multiplication. In 3D program the left matrix takes action first. (In Mathematics, the right one takes action first.) Matrix matRotation, matTranslation; m_device.Transform.World = matTranslation* matRotation ; Draw Figure; // first translation then rotation