so we're back to my point that float should be used for 3D programming, not double.
so we're back to my point that float should be used for 3D programming, not double.
and only float, not templated
Yep. You got it. I just think that three calls (especially to the sqrt function) may be a bit expensive.So you mean something as simple as this:
Code:void Vector3D::normalize() { double temp = length(); x /= temp; y /= temp; z /= temp; }
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
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.
Hope this gets you going a little fasterCode:Vector3D( float x, float y, float z ) : m_x(x), m_y(y), m_z(z) { }
Any questions just ask.
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
OK, I'm making some changes based on all input. BTW Mr. Wizard: I already have +=, ect, just havent updated it yet .
Do not make direct eye contact with me.
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).
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
I've upgraded the class, but I'm only including the files this time: first is vector.h, next post is vector.cpp. Thanks !
Last edited by Lurker; 11-14-2003 at 05:15 PM.
Do not make direct eye contact with me.
Vector.cpp :
Last edited by Lurker; 11-14-2003 at 05:15 PM.
Do not make direct eye contact with me.
Looking better...
Again, check for divide by 0 in normalize, operator / and /=.
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
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.
What should I do if it is 0? Return -1 ?Originally posted by MrWizard
Looking better...
Again, check for divide by 0 in normalize, operator / and /=.
Do not make direct eye contact with me.
if length == 0, then do nothing, something like this:
in other words, if the vector is (0, 0, 0), do nothing.Code:float len = length(); if (len == 0.0f) { return; } x /= len; y /= len; z /= len;
OK, thanks, I'll update the class in a minute...
Do not make direct eye contact with me.
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 !
Do not make direct eye contact with me.
== and != aren't going to work the way you think. You need to do a |x-v.x|<σ,|y-v.y|<σ,|z-v.z|<σ for some small σ. 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.