# Thread: My 3D Vector class

1. so we're back to my point that float should be used for 3D programming, not double.

2. and only float, not templated

3. So you mean something as simple as this:

Code:
```void Vector3D::normalize() {
double temp = length();
x /= temp;
y /= temp;
z /= temp;
}```
Yep. You got it. I just think that three calls (especially to the sqrt function) may be a bit expensive.

4. Not too bad for your first try but it could certainly be much faster First of all let's start with your normalize function. Don't call length 3 times, that's a lot of multiplications, and I see you have already corrected this. You don't check division by 0 in your normalize function. What if the length of the vector is 0? Make sure you check for these things!

Overloaded operators: Definitely implement +=, *=, /=, etc.. these will return by reference and be much faster than +, *, / etc, will be...so..

Vector3D& operator+=( const Vector3D &v );

would be the declaration. Which brings me to another point.

Whenever you are passing a Vector3D class as a parameter pass it as const reference if you can! You are going to get murdered in efficiency if you continue to pass by value. You are getting crazy overhead with that. You will get hidden stuff like copy constructors and the compiler won't be able to optimize as good when you just pass by value.

Also don't worry about writing your own assignment operator if you don't need one. You will be given a default one that will do exactly what your code is doing now. Not necessary.

On constructors, prefer to use initializer lists instead of direct assignment.

Code:
`Vector3D( float x, float y, float z ) : m_x(x), m_y(y), m_z(z) { }`
Hope this gets you going a little faster

5. OK, I'm making some changes based on all input. BTW Mr. Wizard: I already have +=, ect, just havent updated it yet .

6. About the initializer lists - that is an optimization to. If you use the initializer lists, then the constructor for those member variables will be called with the argument(s) you provide it. Otherwise, the default constructor is called, and then you assign to the object, which is not as efficient (especially for user defined types).

7. I've upgraded the class, but I'm only including the files this time: first is vector.h, next post is vector.cpp. Thanks !

8. Vector.cpp :

9. Looking better...

Again, check for divide by 0 in normalize, operator / and /=.

10. yeah I had a problem with that divide by zero crap in my code...it took me about half an hour trying to figure out why when I would hit the D and A keys at the same time (both strafe directions) i would disappear completely out of the world.

11. Originally posted by MrWizard
Looking better...

Again, check for divide by 0 in normalize, operator / and /=.
What should I do if it is 0? Return -1 ?

12. if length == 0, then do nothing, something like this:

Code:
```float len = length();
if (len == 0.0f) {
return;
}
x /= len;
y /= len;
z /= len;```
in other words, if the vector is (0, 0, 0), do nothing.

13. OK, thanks, I'll update the class in a minute...

14. I added divide by 0 checking and the cross product . Should I add 2 functions: void dotProduct and void crossProduct to have it automatically set the vector to it? Such as operator+= would be to operator+? Thanks for opinions, I really appreciate it !

15. == and != aren't going to work the way you think. You need to do a |x-v.x|<&sigma;,|y-v.y|<&sigma;,|z-v.z|<&sigma; for some small &sigma;. You can rotate a vector around 360 degrees and because of very small round off erros, == will say false;

When you want to divide by a number, w. Compute temp=1/w one time and do x*=temp; y*=temp; z*= temp;. Division is much slower than multiplication.

Why not make a function distance2 or something that does just like distance but not the sqrt at the end. You can still use this value for comparing the lengths of vectors, which is often needed.