Normalizing Vectors

Zatnikitelman

Addon Developer
Addon Developer
Joined
Jan 13, 2008
Messages
2,303
Reaction score
6
Points
38
Location
Atlanta, GA, USA, North America
I'm working on a smal calculator for normalizing vectors, however, the method that I'm using looks a bit sloppy to me.
What I do is given x and y coordinates, I take the arctan of the y over x to get theta which I then take the sin and cosine of to get the normalized y and x values respectively.
Code:
(2,3)
arctaan(2/3)=Θ
cos(Θ)=Xn
sin(Θ)=Yn
When the z value is added, my method breaks apart.
I've tried googling, but I can't seem to find anything that would truly work unless I'm just missing something.
Thanks for any help,
Zat
 
The real simple way to normalize a vector is

Vnorm = V*(1/|V|)

Or simply: Dividing each component of the vector by the length of the vector.

V = (X, Y, Z)

L = sqrt(X^2 + Y^2 + Z^2) = sqrt(V*V) = |V|
Xn = X/L
Yn = Y/L
Zn = Z/L

Vn = (Xn, Yn, Zn)
 
LoL yea :P

All you have to do is multiply each component by an inverted value of it's length - and you have to make sure it's not 0. But a 0 long vector... well... you get the idea :P

5 * 1/5 = 1 :)
 
All you have to do is multiply each component by an inverted value of it's length
Just to clarify, the "length" referred to here is the total length of the vector (|V| per Urwumpe's post) not the length of each respective component.

Not sure if your looking for code or not but here goes anyway - from my code:
Code:
VECTOR3 normalise(const VECTOR3 &a) { return (a/length(a)); }
and from OrbiterAPI:
Code:
inline VECTOR3 operator/ (const VECTOR3 &a, const double f)
{
    VECTOR3 c;
    c.x = a.x/f;
    c.y = a.y/f;
    c.z = a.z/f;
    return c;
}

inline double length (const VECTOR3 &a)
{
    return sqrt (a.x*a.x + a.y*a.y + a.z*a.z);
}
 
Code:
inline VECTOR3 norm (const VECTOR3 &a)
{
    double vlen;

    vlen =  sqrt(((a.x * a.x) + (a.y * a.y) + (a.z * a.z)));

    return _V(a.x/vlen,a.y/vlen,a.z/vlen);
}

SetCameraDefaultDirection(norm(_V(-1.5,-1.5,-1.5)));
 
Don't know if you have seen this:-

[ame="http://www.orbithangar.com/searchid.php?ID=3429"]Vector normalizer tool[/ame]

N.
 
Back
Top