Liron Harel
Liron Harel

Reputation: 11247

Normalized vector function returns unexpected result

I am having a problem understanding the vector normalization function.

I have this vector normalization function:

function vectorNormalization(vector) {
    var magnitude = Math.sqrt((vector[0] * vector[0]) + (vector[1] * vector[1]) +
                    (vector[2] * vector[2]));

    var x = vector[0] / magnitude;
    var y = vector[1] / magnitude;
    var z = vector[2] / magnitude;

    return [x, y, z];
}

These are the result of the reduction between the two vectors (using in a 3D software).

[X: -0.004805074799706634, Y: 0.19191680472036263, Z: -0.002017289400100708]

The vector normalization function returns this:

[-0.025028054575754678,0.99963152765883,-0.010507397138519977]

The Y value is too high, it supposed to be normalized and represent a unit but it results in a much higher number. The unit should be less than the value before the normalization.

As you can see, I am dealing with a 3D coordinate system with decimal accuracy. The division is yielding this. I think I need to do something that should take the decimal precision into account, but I am not sure.

Upvotes: 1

Views: 200

Answers (1)

andand
andand

Reputation: 17487

The answer is correct. Normalization projects the vector onto the unit sphere. The original vector has magnitude of 0.191987547 which is less than 1 and so normalization will cause the magnitude to increase. You can verify this to be the case by computing the magnitude of the resulting normalized vector and see that its magnitude is essentially 1.0.

Further, you can see that the Y component of the original is substantially longer than the X or Z components, so it's not surprising it will be very close to 1 in the normalized vector.

Upvotes: 1

Related Questions