Les fonctions sur les vecteurs

Ici on crée les fonctions de base pour effectuer les opérations sur les vecteurs.

#include <d3d9.h>
#include <math.h>

// calcule le vecteur unitaire
D3DVECTOR normalisation(D3DVECTOR);
// calcule la soustraction de deux vecteurs
D3DVECTOR soustraction(D3DVECTOR, D3DVECTOR);
// calcule le produit vectoriel de deux vecteurs
D3DVECTOR produit_vectoriel(D3DVECTOR, D3DVECTOR);
// calcule le produit scalaire de deux vecteurs
float produit_scalaire(D3DVECTOR, D3DVECTOR);
// scalaire * vecteur
D3DVECTOR scalaire(float, D3DVECTOR);
// calcule la matrice identité
D3DMATRIX identite(void);

Dans le fichier source vect_matr.cpp on aura

D3DVECTOR normalisation(D3DVECTOR vecteur) {

float normale = (float)sqrt(vecteur.x * vecteur.x + vecteur.y * vecteur.y + vecteur.z * vecteur.z);

if (!normale) {

D3DVECTOR vect_nul = {0.0f, 0.0f, 0.0f};
return vect_nul;
}

D3DVECTOR norm = {vecteur.x / normale, vecteur.y / normale, vecteur.z / normale};

return norm;
}

Cette fonction permet de retourner le vecteur unitaire où :

        ________________________
v = \/v.x * v.x + v.y * v.y + v.z * v.z
e = (v.x/v, v.y/v, v.z/v)

D3DVECTOR soustraction(D3DVECTOR a, D3DVECTOR b) {
D3DVECTOR s = {a.x - b.x, a.y - b.y, a.z - b.z};

return s;
}


Cette fonction calcule la différence de deux vecteurs :

a - b = (a.x - b.x, a.y - b.y, a.z - b.z)

D3DVECTOR produit_vectoriel(D3DVECTOR a, D3DVECTOR b) {
D3DVECTOR p = {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};

return p;
}


Cette fonction calcule le produit vectoriel de deux vecteurs :

a ^ b = (a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x)

float produit_scalaire(D3DVECTOR a, D3DVECTOR b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}


Cette fonction calcule le produit scalaire de deux vecteurs :

a . b = a.x * b.x + a.y * b.y + a.z * b.z

D3DVECTOR scalaire(float s, D3DVECTOR vecteur) {
D3DVECTOR v = {s*vecteur.x, s*vecteur.y, s*vecteur.z};

return v;
}


Cette fonction multiplie un vecteur par un scalaire.

s . v = (s * v.x, s * v.y, s * v.z)

Téléchargez la source, cliquez ci-dessous :