Classe vecteur

#include <cmath> /** check for any other libraries that are required (and feel free to comment to tell me about it)**/ /** if you encounter any non-logical results, feel free to lend me a bit of your code so I can analyze the mistakes I made and fix the snippet **/ #define PI 3.141592653589793 #define t TRUE #define f false template <class variable> void out(variable x, bool y=false){if(!y){cout<<x;}else{cout<<x<<endl;}} float radian2degree(float x){ return x*(float)(180/PI); } int radian2degree(int x){ return x *(int)(180/PI); } float degree2radian(float x){ return x * (float)(PI/180); } int degree2radian(int x){ return x * (int)(PI/180); } class vecteur{ public: //constructor vecteur(); vecteur(float,float,float); vecteur(vecteur*); //object member float x; float y; float z; void add(vecteur* v){ this->x+=v->x; this->y+=v->y; this->z+=v->z; } vecteur* opposite(){ float a,b,c; a=this->x; b=this->y; c=this->z; if(a!=0)a=-a; if(b!=0)b=-b; if(c!=0)c=-c; vecteur* v = new vecteur(a,b,c); return v; } void substract(vecteur* v){ this->x-=v->x; this->y-=v->y; this->z-=v->z; } float prod_scal(vecteur* v){ return this->x*v->x + this->y*v->y + this->z*v->z; } bool colinear(vecteur* v){ bool test = f; if( (this->is2D() && v->is2D()) || (this->is3D() && v->is3D())){ float b1b1=this->prod_scal(this); float b1b2=this->prod_scal(v); float b2b2=v->prod_scal(v); if ( b1b1!=0 && b2b2!=0 ){ if (b1b1*b2b2==b1b2*b1b2) { test=t; } } } return test; } bool orthogonal(vecteur* v){ return (prod_scal(v)==0.0); } void scale(float s){ this->x=this->x * s; this->y=this->y * s; this->z=this->z * s; } float norme(){ float as,bs,cs,sum; as=pow(this->x,2); bs=pow(this->y,2); cs=pow(this->z,2); sum=(as+bs+cs); sum=sqrt(sum); return abs(sum); } float angleFrom(vecteur* v){ float res = -404.0; if(this->is2D() && v->is2D()){ float a = this->norme(); float b = v->norme(); float pv = this->prod_scal(v); float ab = a * b; res = radian2degree( acos( (float)(pv/ab) ) ); } return res; } bool is2D(){ return (z==0); } bool is3D(){ return !this->is2D(); } vecteur* prod_vect(vecteur* v){ float a,b,c; /** a stands for the x component b stands for the y component c stands for the z component **/ c=(this->x * v->y) - (this->y * v->x); a=(this->y * v->z) - (this->z * v->y); b=(this->z * v->x) - (this->x * v->z); vecteur* tmp = new vecteur(a,b,c); return tmp; } void show(){ out("x = ");out(this->x,t); out("y = ");out(this->y,t); if(z!=0){ out("z = ");out(this->z,t); } } //destructor virtual ~vecteur(){ out("~delete"); } }; vecteur::vecteur(){ this->x=0.0; this->y=0.0; this->z=0.0; } vecteur::vecteur(float a, float b, float c=0){ this->x=a; this->y=b; this->z=c; } vecteur::vecteur(vecteur* u){ this->x = u->x; this->y = u->y; this->z = u->z; } vecteur* addition(vecteur* a, vecteur* b){ vecteur* tmp = new vecteur(); tmp->x = a->x + b->x; tmp->y = a->y + b->y; tmp->z = a->z + b->z; return tmp; } vecteur* soustraction(vecteur* a, vecteur* b){ vecteur* tmp = new vecteur(); tmp->x = a->x - b->x; tmp->y = a->y - b->y; tmp->z = a->z - b->z; return tmp; } float det(vecteur* a, vecteur* b){ return ( (a->x * b->y) - (b->x * a->y) ); }
une classe vecteur qui permet aussi bien la gestion de coordonnées pour un plan ou dans l'espace

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.