Compute the magnitude (length) of a vector - Android java.lang

Android examples for java.lang:Math Vector

Description

Compute the magnitude (length) of a vector

Demo Code


//package com.java2s;

public class Main {
    /**// w  w  w .  j  av a 2  s.c o  m
     * 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