At the invitation of a friend, I specially used C language to implement the cross multiplication of 3D vectors, because he could not find the code about vector multiplication on the Internet. Now that I have written this article, I would like to share it with you. There are not many considerations for the data structure during the writing, but I will focus on the implementation of the computation process. If you have good suggestions, I would like to leave a message.
//// // Main. c /////////////////////////////////////// //
# Include <stdio. h>
# Include "vector_multiply.h"
Int main (void)
{
Float a [3] = {1.0, 2.0, 3.0}, B [3] = {7.0, 8.0, 9.0}, c [3] = {0 };
Vector_multiply (a, B, c );
Printf ("%. 1f: %. 1f: %. 1f \ n", c [0], c [1], c [2]);
Return (0 );
}
/// // Vector_multiply.c //// ///////////////////////////
Void vector_multiply (float a [], float B [], float c []) {
Int I, j, k, n = 0;
/* According the formula we can get these marks */
Int d [3] [4] = {0, 1,-1, 0}, {0,-1, 1, 0}, {0, 1,-1, 0 }};
For (k = 0; k <3; k ++ ){
For (I = 0; I <3; I ++ ){
For (j = 0; j <3; j ++)
If (k! = I & k! = J)
C [k] + = a [I] * B [j] * d [k] [n ++];
}
N = 0;
}
Return;
}
//// // Vector_multiply.h /////// ///////////////////////////
# Ifndef VECTOR_MULTIPLY_H _
# Define VECTOR_MULTIPLY_H _
Void vector_multiply (float a [], float B [], float c []);
# Endif/* VECTOR_MULTIPLY_H _*/
//////////////////////////////////////// //////////////////////////////////////// ///
Gcc main. c vector_multiply.c-o VectorOperations
Finally, the formula of the vector cross multiplication is left:
Vector: u = (u1, u2, u3) v = (v1, v2, v3)
Cross Product formula: u x v = | I j k |
| A1 b1 c1 |
| A2 b2 c2 |
= {U2v3-v2u3, u3v1-v3u1, u1v2-u2v1}