I'm a hobby programmer and have started 2 month ago with C++. I had some knowledge of Python, and I learned it all through Internet tutorials.
I'm looking for your help, since I don't have a tutor I would like to have a comment about my programming style. I used this small "library" to create the Snake4D game (see bottom of question for further information).
Here is a little program that handles mathematical vectors, 4D and 3D. Specifically, I ask you to find any improvements in the code's readability and in the algorithms.
Here is the "vec.h" file:
class V4{
private:
double coords[4];
public:
V4();
V4(double x, double y, double z, double w);
~V4();
void set_cooord(int i, double x); //set_coord (index of coord i, value x)
double module();
double operator[](unsigned int i);
void operator =(V4);
V4 operator +(const V4);
V4 operator -(const V4);
V4 operator *(double k); //moltiplication of vector by a scalar k
double dot4(V4); //the dot product
V4 cross(V4 u, V4 v,V4 w); //the cross product
};
Here is the "vec.cpp" file:
/****************
V4 class
*****************/
V4::V4(){
//set all coords to 0. if it's called without specific coord
coords[0] = 0.;
coords[1] = 0.;
coords[2] = 0.;
coords[3] = 0.;
}
V4::V4(double x, double y, double z, double w){
coords[0] = x;
coords[1] = y;
coords[2] = z;
coords[3] = w;
}
V4::~V4(){}//nothing to destroy?
void V4::set_cooord(int i, double x){ //i index, x value
coords[i] = x;
}
double V4::module(){
double sum = 0;
for(int i= 0; i<4; i++){
sum += coords[i]*coords[i];
}
return sqrt(sum);
}
double V4::operator[](unsigned int i){
//if(i > 4){return;} //list index out of range ? how to throw give a compile error?
return coords[i];
}
void V4::operator =(V4 v1){
for(int i= 0; i<4; i++){
coords[i] = v1[i];
}
}
V4 V4::operator+(V4 v1) {
double c[4]; // array of 4 double which will be transfered to the returned vector
for(int i= 0; i<4; i++){
c[i] = coords[i] + v1[i];
}
return V4(c[0],c[1],c[2],c[3]);
}
V4 V4::operator-( V4 v1) {
double c[4];
for(int i= 0; i<4; i++){
c[i] = coords[i] - v1[i];
}
return V4(c[0],c[1],c[2],c[3]);
}
V4 V4::operator*(double k){
double c[4];
for(int i= 0; i<4; i++){
c[i] = coords[i] * k;
}
return V4(c[0],c[1],c[2],c[3]);
}
double V4::dot4(V4 v1){
double sum = 0;
for(int i= 0; i<4; i++){
sum += coords[i]*v1[i];
}
return sum;
}
// requires three external vector and doesn't take any coords of the actual vector
//intended use:
// V4 v1(x,y,z,w), v2(x,y,z,w), v3(x,y,z,w), result, op;
// result = op.cross(v1,v2,v3)
V4 V4::cross(V4 u, V4 v, V4 w){
//source Steve Hollasch master thesis
//partial product
double A = (v[0]*w[1])-(v[1]*w[0]);
double B = (v[0]*w[2])-(v[2]*w[0]);
double C = (v[0]*w[3])-(v[3]*w[0]);
double D = (v[1]*w[1])-(v[2]*w[1]);
double E = (v[1]*w[3])-(v[3]*w[1]);
double F = (v[2]*w[3])-(v[3]*w[2]);
double x = (u[1]*F)-(u[2]*E)+(u[3]*D);
double y = -(u[0]*F)+(u[2]*C)-(u[3]*B);
double z = (u[0]*E)-(u[1]*C)+(u[3]*A);
double wc = -(u[0]*D)+(u[1]*B)-(u[2]*A); //defined as wc to not conflic with w vector
return V4(x,y,z,wc);
}
Here are the source (.h and .cpp) file.
I already see some improvements:
- I use the vectors as
public
members, because if they areprivate
, I cannot get access to all the really comfortable functions they offer. - Instead of giving vertex hand by hand for the generation of the hypercube, there should be an algorithm that does this.
- It could be used a general purpose class
V
containing the basic methods and having a variableint
dimension then specialize theV3
andV4
inheriting the classV
.
PS: I've made a little clip that shows the Snake4D game written in C++, Qt and OpenGL.