Here you can find the source of angle(double[] vec1, double[] vec2)
public static double angle(double[] vec1, double[] vec2)
//package com.java2s; //License from project: Apache License public class Main { public static double angle(double[] vec1, double[] vec2) { double small, undefined, magv1, magv2, temp; small = 0.00000001;// w w w.j a v a 2s .c o m undefined = 999999.1; magv1 = mag(vec1); magv2 = mag(vec2); if (magv1 * magv2 > small * small) { temp = dot(vec1, vec2) / (magv1 * magv2); if (Math.abs(temp) > 1.0) { temp = Math.signum(temp) * 1.0; } return Math.acos(temp); } else { return undefined; } } public static double mag(double[] x) { return Math.sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]); } public static double dot(double[] x, double[] y) { return (x[0] * y[0] + x[1] * y[1] + x[2] * y[2]); } }