Ano-Tech Computers
Enter keyword:

Math: How to normalize a vector
Problem:
When dealing with 3D lighting or rotation quaternions, it is sometimes useful to normalize a vector; that is to recalculate the vector so that its magnitude becomes 1
 
Solution:
Divide each axis by the vector magnitude:

magnitude = sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] );
v[0] = v[0] / magnitude;
v[1] = v[1] / magnitude;
v[2] = v[2] / magnitude;

 
Discuss this solution
Did this article solve your problem? Yes No Did not apply

We welcome anyone who is willing to contribute to this public knowledge base, contact siteadmin@atc.no if you have information you would like to share. The idea is not to replace the commercial support sites, but to publish those hard-to-find solutions you've found yourself looking for over and over again.

Show all articles