Archived

This topic is now archived and is closed to further replies.

jwblair

Normalizing Vectors...

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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;

}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites