Java Angle Between angleBetween(double[] v1, double[] v2)

Here you can find the source of angleBetween(double[] v1, double[] v2)

Description

Determine the angle of rotation between two vectors.

License

Academic Free License

Parameter

Parameter Description
v1 a parameter
v2 a parameter

Declaration

public static double angleBetween(double[] v1, double[] v2) 

Method Source Code

//package com.java2s;
//License from project: Academic Free License 

public class Main {
    /**/*from   w ww.  j a  va  2  s . c  o m*/
     * Determine the angle of rotation between two vectors.
     * 
     * @param v1
     * @param v2
     * @return
     */
    public static double angleBetween(double[] v1, double[] v2) {
        double m1 = mag(v1);
        double m2 = mag(v2);
        double ang = dot(v1, v2) / (m1 * m2);
        ang = Math.acos(ang);
        return ang;
    }

    /**
     * Calculates the magnitude of the vector.
     * 
     * @param v
     * @return
     */
    public static double mag(double v[]) {
        return Math.sqrt(dot(v, v));
    }

    /**
     * Calculates the dot product of two vectors.
     * 
     * @param v1
     * @param v2
     * @return
     */
    public static double dot(double v1[], double v2[]) {
        int size = v1.length;
        double cp = 0.0;

        for (int i = 0; i < size; i++) {
            cp += v1[i] * v2[i];
        }

        return cp;
    }
}

Related

  1. angle2DBetween(double[] coord1, double[] coord2)
  2. angle_between(double x1, double y1, double z1, double x2, double y2, double z2)
  3. angleBetween(final double ax, final double ay, final double bx, final double by)
  4. angleBetween2Lines(double l1x1, double l1y1, double l1x2, double l1y2, double l2x1, double l2y1, double l2x2, double l2y2)
  5. angleBetween2LinesVectorAlgebra(double x11, double y11, double x12, double y12, double x21, double y21, double x22, double y22)
  6. AngleBetweenDegrees(double longitudeFirstBody, double latitudeFirstBody, double longitudeSecondBody, double latitudeSecondBody)