Java Angle Between angleBetween(final double ax, final double ay, final double bx, final double by)

Here you can find the source of angleBetween(final double ax, final double ay, final double bx, final double by)

Description

Return the angle in radians between the 2d vectors defined by the parameters.

License

Open Source License

Parameter

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

Return

angle between the vectors

Declaration

public static double angleBetween(final double ax, final double ay, final double bx, final double by) 

Method Source Code

//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;
    }
}

Related

  1. angle2DBetween(double[] coord1, double[] coord2)
  2. angle_between(double x1, double y1, double z1, double x2, double y2, double z2)
  3. angleBetween(double[] v1, double[] v2)
  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)
  7. angleBetweenPoints(double ax, double ay, double bx, double by)