Normalizing Vectors...

Started by
5 comments, last by jwblair 22 years, 11 months ago
Does anyone know how to normailize vectors? Or at least point me to a good math sight? Thanks!
Thanks!John William Blair "The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of the darkness. For he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee."
http://members.home.com/chucklez/wtc/index.html
Advertisement
Divide each component (x,y,z) of the vector by the vector''s magnitude (length).
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]
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!
Thanks!John William Blair "The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of the darkness. For he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee."
http://members.home.com/chucklez/wtc/index.html
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!
Thanks!John William Blair "The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of the darkness. For he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee."
http://members.home.com/chucklez/wtc/index.html

This topic is closed to new replies.

Advertisement