Converts this vector into a normalized (unit length) vector Modifies the input parameter - Android java.lang

Android examples for java.lang:Math Vector

Description

Converts this vector into a normalized (unit length) vector Modifies the input parameter

Demo Code


//package com.java2s;

public class Main {
    /**//from ww w  .j  a v a 2s .co  m
     * Converts this vector into a normalized (unit length) vector
     * <b>Modifies the input parameter</b>
     * @param vector The vector to normalize
     **/
    public static void normalize(int size, float[] vector, int offset) {
        multiply(size, vector, offset, 1 / magnitude(size, vector, offset));
    }

    /**
     * Multiply a vector by a scalar.  <b>Modifies the input vector</b>
     * @param vector The vector 
     * @param scalar The scalar
     **/
    public static void multiply(int size, float[] vector, int offset,
            float scalar) {
        for (int i = 0; i < size; i++)
            vector[offset + i] *= scalar;
    }

    /**
     * Compute the magnitude (length) of a vector
     * @param vector The vector
     * @return The magnitude of the vector
     **/
    public static float magnitude(int size, float[] vector, int offset) {
        float tmp = 0.0f;
        for (int i = offset; i < (offset + size); i++) {
            tmp += vector[i] * vector[i];
        }
        return (float) Math.sqrt(tmp);
    }
}

Related Tutorials