Class vecteur 3d

Contenu du snippet

Voila la definition d'une classe de vecteur 3D, c'est assez utile pour les programmes OpenGL par exemple.
La classe ne contient pas toutes les opérations mais les plus importantes :
==, =, +, -, *, /, ., ^, norm, normalize.

Source / Exemple :


/**************************************************************************************
*

  • Name : Vector3D.h
*
                                                                                                                                                                            • /
#include <windows.h> #include <stdio.h> #include <stdlib.h> #include <math.h> class Vector3D { public: /*---------------------------------- Constructor --------------------------------*/ Vector3D () {} Vector3D (float X, float Y, float Z) { x = X; y = Y; z = Z; } /*---------------------------------- Operators ----------------------------------*/ // Return himself operator Vector3D () const { return Vector3D(x, y, z); } // Add vector Vector3D operator + (Vector3D Vector) { return Vector3D (Vector.x + x, Vector.y + y, Vector.z + z); } // Subtract vectors Vector3D operator - (Vector3D Vector) { return Vector3D (x - Vector.x, y - Vector.y, z - Vector.z); } // "produit vectoriel" Vector3D operator ^(Vector3D Vector) { Vector3D vNormal; vNormal.x = ((y * Vector.z) - (z * Vector.y)); vNormal.y = ((z * Vector.x) - (x * Vector.z)); vNormal.z = ((x * Vector.y) - (y * Vector.x)); return vNormal; } // Divide by a scalar Vector3D operator * (float num) { // Return the scaled vector return Vector3D(x * num, y * num, z * num); } // "produit scalaire" float operator * (Vector3D Vector) { return (x * Vector.x + y * Vector.y + z * Vector.z); } // Divide by a scalar Vector3D operator / (float num) { // Return the scale vector return Vector3D(x / num, y / num, z / num); } // = operator void operator = (Vector3D Vector) { x = Vector.x; y = Vector.y; z = Vector.z; } // == operator bool operator == (Vector3D Vector) { return (x == Vector.x && y == Vector.y && z == Vector.z); } // != operator bool operator != (Vector3D Vector) { return (x != Vector.x || y != Vector.y || z != Vector.z); } /*---------------------------------- Functions ----------------------------------*/ // Work out the norm of our vector float Norm() { // Norme = sqrt(x^2 + y^2 + z^2) return (float)sqrt( Vector3D(x,y,z) * Vector3D(x,y,z) ); } // Work out the vector normalized : Vector.norm() == 1 Vector3D Normalize() { return (Vector3D(x,y,z) / Norm()); } float x, y, z; };

Conclusion :


exemple :

Vector3D Vector1(0.0,4.0,0.0);
Vector3D Vector2(9.0,6.0,0.0);
Vector3D Vector3(0.0,0.0,0.0);
float result;
char buffer[255];

Vector3 = Vector1 ^ Vector2;
result = Vector1 * Vector2;

sprintf(buffer, "(0.0,4.0,0.0)) ^ (9.0,6.0,0.0) = (%f,%f,%f)", Vector3.x, Vector3.y, Vector3.z);
MessageBox(NULL,buffer,"RESULT",MB_OK);

sprintf(buffer, "(0.0,4.0,0.0) * (9.0,6.0,0.0) = %f", result);
MessageBox(NULL,buffer,"RESULT",MB_OK);

sprintf(buffer, "Norm of Vector3 = %f", Vector3.Norm());
MessageBox(NULL,buffer,"RESULT",MB_OK);

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.