) Managed DirectX + C # Development (Getting Started) (2)
Source: Internet
Author: User
Chapter 2 vector operations (1)1: equal VectorsIn geometry, there are two vectors of the same direction and length equal. In mathematics, we say that vectors with the same dimension and component are equal. For example, if UX = VX, Uy = Vy, and uz = VZ. (UX, Uy, uz) = (VX, Vy, VZ ). In the code, we can use "=" to determine that two vectors are equal.
2: vector ModulusThe vector size is the length of a directed line segment. Knowing the vector component, we can use the formula below to calculate the vector size. ‖ U‖ indicates the length of the vector u. For example, calculate the values of the vectors u = (1, 2, 3) and V = (1, 1. According to formula (1), we can obtain the length of the Vector Model in DirectX. For example, vector3 VEC = new vector3 (, 3); MessageBox. the length of show ("vector V (, 3):/N" + Vec. length (). tostring (); execution result: Another useful method is vector. lengthsq () is used to obtain the square of the vector modulus length. In practice, its purpose may exceed length (). If the modulus length is required before a program, next we need to perform the Modulo-long square operation, which saves a lot of computing. We know that square operation is actually a pain for computers;
3: vector unitizationVector unitization means to make the vector size equal to 1, which is called the unit vector. The vector size and each component can be used to form a vector unit: for example, for vectors u = (1, 2, 3) and V = (1, 1 ). The following is a unitization method. First, use the formula for calculating the length of the vector modulus to obtain the formula for "U" = √ 14 and "V" = √ 2. Therefore, the following code indicates that after the vector is unitized, let's look at the Vector Value and module length: vector3 VEC = new vector3 (, 3); Vec. normalize (); string disstring = "vector V (1, 2, 3) After unitization:/N"; disstring + = "V (" + Vec. x. tostring () + "," + Vec. y. tostring () + "," + Vec. z. tostring () + ")/n"; disstring + = "the module length after unitization:" + Vec. length (). tostring (); MessageBox. show (disstring, "vector unitization"); the program execution result is as follows:
4: Adding VectorsThe sum of the vectors can be obtained by adding each component of the two vectors respectively. Note that they must have the same dimension before addition. The following figure shows the geometric representation of vector addition: the following code shows the addition of two vectors and the result of the addition is displayed: vector3 vec1 = new vector3 (, 3 ); vector3 vec2 = new vector3 (3, 4, 5); vector3 vec3 = vector3.add (vec1, vec2); string disstring = "V (1, 2, 3) + V (3, 4, 5 ): /n "; disstring + =" V ("+ vec3.x. tostring () + "," + vec3.y. tostring () + "," + vec3.z. tostring () + ")/n"; MessageBox. show (disstring, "adding vectors"); the execution result is as follows:
5: vector SubtractionThe addition method is similar. The difference between the two vectors is obtained by subtracting each component of the two vectors. The figure shows the subtraction of vectors in the geometry. The following code shows the subtraction of two vectors and shows the result after subtraction: Private void vectorsubtract () {vector3 vec1 = new vector3 (, 3 ); vector3 vec2 = new vector3 (3, 4, 5); vector3 vec3 = vector3.subtract (vec1, vec2); string disstring = "V (1, 2, 3)-V (3, 4, 5 ): /n "; disstring + =" V ("+ vec3.x. tostring () + "," + vec3.y. tostring () + "," + vec3.z. tostring () + ")/n"; MessageBox. show (disstring, "subtraction of vectors");} The execution result is as follows:
6: Multiplication of VectorsMultiply by a number and a vector, and the vector changes proportionally. Use a negative number to multiply and change the direction. Run the following code to calculate the number of vectors: Private void vectorscale () {vector3 vec1 = new vector3 (1, 2, 3); vector3 vec3 = vector3.scale (vec1, 10 ); string disstring = "V (1, 2, 3) multiplied by 10:/N"; disstring + = "V (" + vec3.x. tostring () + "," + vec3.y. tostring () + "," + vec3.z. tostring () + ")/n"; MessageBox. show (disstring, "Multiplication of vectors");} The execution result is: transferred from: dandancool http://blog.csdn.net/dandancool/archive/2007/06/26/1666603.aspx
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service