Here you can find the source of angleBetween(final double ax, final double ay, final double bx, final double by)
Parameter | Description |
---|---|
ax | x-axis component of the first vector |
ay | y-axis component of the first vector |
bx | x-axis component of the second vector |
by | y-axis component of the second vector |
public static double angleBetween(final double ax, final double ay, final double bx, final double by)
//package com.java2s; public class Main { public static final double PI = Math.PI; public static final double TWO_PI = 2d * PI; /**/* ww w . j a va2 s . com*/ * Return the angle in radians between the 2d vectors defined by the * parameters. * * @param ax x-axis component of the first vector * @param ay y-axis component of the first vector * @param bx x-axis component of the second vector * @param by y-axis component of the second vector * @return angle between the vectors */ public static double angleBetween(final double ax, final double ay, final double bx, final double by) { return normaliseAngle(Math.atan2(ay, ax) - Math.atan2(by, bx)); } /** * Normalise the angle theta: so it is in the range: -pi <= a < pi * * @param theta angle to normalise (in radians) * @return angle normalised */ public static double normaliseAngle(final double theta) { final double trimmedAngle = theta % TWO_PI; if (trimmedAngle >= PI) return (trimmedAngle % PI) - PI; else if (trimmedAngle < -PI) return PI + (trimmedAngle % PI); else return trimmedAngle; } }