#### Archived

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

# Normalizing Vectors...

This topic is 6295 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Does anyone know how to normailize vectors? Or at least point me to a good math sight? Thanks!

##### Share on other sites
Divide each component (x,y,z) of the vector by the vector''s magnitude (length).

##### Share on other sites
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 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;

}

Thanks Everyone!

##### Share on other sites
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.

Thanks Again!

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631766
• Total Posts
3002235
×