Accueil > > > CLASS VECTEUR 3D
CLASS VECTEUR 3D
Information sur la source
Description
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
- /**************************************************************************************
- *
- * 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;
- };
/**************************************************************************************
*
* 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);
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
[TECHDAYS2012] OUI J'Y SERAI![TECHDAYS2012] OUI J'Y SERAI! par JeremyJeanson
Bonsoir, Certes, je l'annonce avec un peu de retard, mais je serai effectivement au Techdays demain. Comme l'an dernier, je participerai au programme ATE (Ask The Expert). Si vous avez des questions Workflow, WCF, AppFabric ou plus généralement .net, n'hé...
Cliquez pour lire la suite de l'article par JeremyJeanson TFS INTEGRATION TOOLS - SUIVI DES SYNCHRONISATIONS AVEC REPORTING SERVICESTFS INTEGRATION TOOLS - SUIVI DES SYNCHRONISATIONS AVEC REPORTING SERVICES par vfabing
Afin de s'assurer du bon fonctionnement des différentes synchronisations effectuées par les TFS Integration Tools, 2 rapports sont présents dès l'installation. Il suffit alors d'effectuer les manipulations suivantes pour pouvoir les visualiser : Loca...
Cliquez pour lire la suite de l'article par vfabing CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT)CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT) par FREMYCOMPANY
Bonjour à tous, Je viens de publier une proposition comprenant 5 pseudo-classes pour le CSS Working Group ayant trait à l'état de chargement d'un élément (ex: IMG,VIDEO,AUDIO,OBJECT pour l'HTML.). Si le c½ur vous en dit, vous pouvez retrouver cette p...
Cliquez pour lire la suite de l'article par FREMYCOMPANY MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ?MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ? par ROMELARD Fabrice
Formation initiale Durant la formation, le découpage classique est le suivant (je donnerai les équivalences Suisse lorsque je les connaîtrais) : Ecole primaire jusqu'au Collège : Formation générale permettant d'obtenir les méthodes...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice Y'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENTY'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENT par Aleks
Quand on a ce genre d'erreur sans log :
Et bas on a juste envie de choper le gas de Microsoft qu'a développé ça et lui foutre des baffes de Coboye ! ...
Cliquez pour lire la suite de l'article par Aleks
Forum
RE : ARBRE BINAIRERE : ARBRE BINAIRE par pacotheking
Cliquez pour lire la suite par pacotheking
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|