Find missing coordinate in normalized vector

114 views Asked by At

Say I have a normalized vector but I don't know what the z coordinate is. How can I retrieve the normalized z coordinate while preserving the existing x and y coordinates.

For example:

X: -0.361
Y:  0.361
Z:  ?

The length of just x and y is 0.510531

len = sqrt((ax * ax) + (ay * ay)) -- 0.510531

Thanks!

2

There are 2 answers

0
vsoftco On BEST ANSWER

What does it mean to have a normalized vector? It means that |Vx|^2 + |Vy|^2 +|Vz|^2 = 1. So, from standard algebra, it follows that |Vz| = sqrt(1 - |Vx|^2 - |Vy|^2), which implies that Vz can take 2 values, negative and positive of sqrt(1 - |Vx|^2 - |Vy|^2)

And btw, the answer is the same in all programming languages ;) Math has nothing to do with C++.

0
Paul Evans On

It's simply the positive or negative of the square root of:

1 - ax * ax - ay * ay