Java Angle Between angle2DBetween(double[] coord1, double[] coord2)

Here you can find the source of angle2DBetween(double[] coord1, double[] coord2)

Description

Calculates the angle between two coordinates in degrees.

License

Open Source License

Parameter

Parameter Description
coord1 First Coordinate
coord2 Second Coordinate

Return

Angle between coord1 and coord2

Declaration

public static double angle2DBetween(double[] coord1, double[] coord2) 

Method Source Code

//package com.java2s;
/**/*from   w ww . j a  v  a2s  .c o  m*/
 * Copyright (C) 2014 Aniruddh Fichadia
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If you use or enhance the code, please let me know using the provided author information or via
 * email Ani.Fichadia@gmail.com.
 */

public class Main {
    /**
     * Calculates the angle between two coordinates in degrees.
     * 
     * Coordinates are represented as an array where the value for each dimension is in each index
     * 
     * @param coord1 First Coordinate
     * @param coord2 Second Coordinate
     * 
     * @return Angle between coord1 and coord2
     */
    public static double angle2DBetween(double[] coord1, double[] coord2) {
        if (coord1.length != coord2.length && coord1.length != 2)
            throw new IllegalArgumentException("Number of dimensions is not valid for the provided coordinates");

        double xDiff = coord2[0] - coord1[0];
        double yDiff = coord2[1] - coord1[1];

        double angle = Math.toDegrees(Math.atan2(yDiff, xDiff)) - 180;

        return angle;
    }
}

Related

  1. angle_between(double x1, double y1, double z1, double x2, double y2, double z2)
  2. angleBetween(double[] v1, double[] v2)
  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)