Java Array Normalize normalize(long[] v, float[] target)

Here you can find the source of normalize(long[] v, float[] target)

Description

Returns a normalized vector (in R^2)

License

Open Source License

Parameter

Parameter Description
v the Long array to normalize (Long)

Return

a normalized vector

Declaration

public static void normalize(long[] v, float[] target) 

Method Source Code

//package com.java2s;

public class Main {
    /**// w  w  w.  ja  v  a  2 s .  co m
     * Returns a normalized vector
     *
     * @param v the Long array to normalize (Float)
     * @return a normalized vector
     */
    public static float[] normalize(float[] v) {
        float len = length(v);
        v[0] /= len;
        v[1] /= len;
        return v;
    }

    /**
     * Returns a normalized vector
     *
     * @param v the Long array to normalize (Long)
     * @return a normalized vector
     *
     * @deprecated for performance use normalize(long[], float[] instead)
     */
    public static float[] normalize(long[] v) {
        float[] target = new float[2];
        normalize(v, target);
        return target;
    }

    /**
     * Returns a normalized vector (in R^2)
     *
     * @param v the Long array to normalize (Long)
     * @return a normalized vector
     */
    public static void normalize(long[] v, float[] target) {
        // FIXME: is double precision really needed here? Faster if
        // len is a float instead of a double
        double len = length(v);
        target[0] = (float) (v[0] / len);
        target[1] = (float) (v[1] / len);
    }

    /**
     * Returns a normalized vector
     *
     * @param v the array to normalize (Integer)
     * @return a normalized vector
     */
    public static float[] normalize(int[] v) {
        float len = length(v);
        return new float[] { v[0] / len, v[1] / len };
    }

    /**
     * Returns the length of a vector
     *
     * @param v the vector (Long)
     * @return the length of the vector (Double)
     */
    public static double length(long[] v) {
        return Math.sqrt(v[0] * v[0] + v[1] * v[1]);
    }

    /**
     * Returns the length of a vector
     *
     * @param v the vector (Float)
     * @return the length of the vector (Float)
     */
    public static float length(float[] v) {
        return (float) Math.sqrt(v[0] * v[0] + v[1] * v[1]);
    }

    /**
     * Returns the length of a vector
     *
     * @param v the vector (Integer)
     * @return the length of the vector (Float)
     */
    public static float length(int[] v) {
        return (float) Math.sqrt(v[0] * v[0] + v[1] * v[1]);
    }
}

Related

  1. normalize(float[] vec)
  2. normalize(float[][] vals, float min, float max)
  3. normalize(int[] a)
  4. normalize(int[] values)
  5. normalize(int[][][] data, int startX, int startY, int stopX, int stopY, double scale)
  6. normalize(Number[] array)
  7. normalize(String[] slist)
  8. normalize01(float[] array)
  9. normalize2(double[] x)