Normalizing Vectors...
Does anyone know how to normailize vectors? Or at least point me to a good math sight?
Thanks!
V1 is a vector, ie. V1 = [5,2,3]
length/magnitude of V1, len = Sqrt((5*5)+(2*2)+(3*3))
normalized vector, V2 = [5/len , 2/len , 3/len]
length/magnitude of V1, len = Sqrt((5*5)+(2*2)+(3*3))
normalized vector, V2 = [5/len , 2/len , 3/len]
here is a simple c function:
void normalize(VECTOR * v)
{
float c = sqrt(v.x * v.x +v.y * v.y +v.z * v.z);
v.x/=c;
v.y/=c;
v.z/=c;
}
void normalize(VECTOR * v)
{
float c = sqrt(v.x * v.x +v.y * v.y +v.z * v.z);
v.x/=c;
v.y/=c;
v.z/=c;
}
Here''s a small optimization you could use:
void normalize(VECTOR * v)
{
float c = 1.0f / sqrt(v.x * v.x +v.y * v.y +v.z * v.z);
v.x *= c;
v.y *= c;
v.z *= c;
}
A multiplication is around 12 times faster than a division, so you''ve only to divide one time.
It''s really a VERY SMALL amount of time you''ll save, but why not cause it''s only a minimal change.
void normalize(VECTOR * v)
{
float c = 1.0f / sqrt(v.x * v.x +v.y * v.y +v.z * v.z);
v.x *= c;
v.y *= c;
v.z *= c;
}
A multiplication is around 12 times faster than a division, so you''ve only to divide one time.
It''s really a VERY SMALL amount of time you''ll save, but why not cause it''s only a minimal change.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement