Java Angle angleOfLine(int x1, int y1, int x2, int y2)

Here you can find the source of angleOfLine(int x1, int y1, int x2, int y2)

Description

Given the coordinates of the start and end of a line, returns the angle in radians of the line

License

Open Source License

Declaration

public static double angleOfLine(int x1, int y1, int x2, int y2) 

Method Source Code

//package com.java2s;
/* Mesquite source code.  Copyright 1997 and onward, W. Maddison and D. Maddison. 
    //from w  w w  .ja  va2 s.com
    
Disclaimer:  The Mesquite source code is lengthy and we are few.  There are no doubt inefficiencies and goofs in this code. 
The commenting leaves much to be desired. Please approach this source code with the spirit of helping out.
Perhaps with your help we can be more than a few, and make Mesquite better.
    
Mesquite is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY.
Mesquite's web site is http://mesquiteproject.org
    
This source code and its compiled class files are free and modifiable under the terms of 
GNU Lesser General Public License.  (http://www.gnu.org/copyleft/lesser.html)
 */

public class Main {
    /** Given the coordinates of the start and end of a line, returns the angle in radians of the line */
    public static double angleOfLine(int x1, int y1, int x2, int y2) {
        if (y1 == y2) //it's a horizontal line or a single point
            if (x2 >= x1)
                return 0.0;
            else
                return Math.PI;
        if (x1 == x2) //it's a vertical line or a single point
            if (y2 >= y1)
                return Math.PI / 2.0;
            else
                return -Math.PI / 2.0;
        double a = Math.PI - Math.atan(-(y2 - y1) * 1.0 / (x2 - x1));
        if (x2 > x1)
            a = Math.PI + a;
        return a;
    }
}

Related

  1. angleInDegrees(float ownerRotation, float x1, float y1, float x2, float y2)
  2. angleInRadians(float originX, float originY, float targetX, float targetY)
  3. angleInRange(double angle, double min, double max)
  4. angleInRange(float theta1, float theta2, float tolerance)
  5. angleLinear(float a, float b, int spin, float f)
  6. angleOfLineDeg(double x1, double y1, double x2, double y2)
  7. angleRadToDegClipped(final double angleRad)
  8. anglesInvalid(double sza, double vza, double saa, double vaa)
  9. angleSum(float a1, float a2)