Java Angle angle(double x1, double y1, double x2, double y2)

Here you can find the source of angle(double x1, double y1, double x2, double y2)

Description

Returns the clockwise angle in radians to the y axis of the line from x1, y1 to x2, y2

License

Open Source License

Parameter

Parameter Description
x1 - the x coordinate of one point
y1 - the y coordinate of one point
x2 - the x coordinate of another point
y2 - the y coordinate of another point

Declaration

public static final double angle(double x1, double y1, double x2, double y2) 

Method Source Code

//package com.java2s;
/**//from   w  ww.j  a v a 2s .c  o m
 * Version 1.0 is to handle single variable 2DSquareCelled raster data.
 * Copyright (C) 2005 Andy Turner, CCG, University of Leeds, UK.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */

public class Main {
    /**
     * Returns the clockwise angle in radians to the y axis of the line from x1, y1 to x2, y2
     * @param x1 - the x coordinate of one point
     * @param y1 - the y coordinate of one point
     * @param x2 - the x coordinate of another point
     * @param y2 - the y coordinate of another point
     * @return 
     */
    public static final double angle(double x1, double y1, double x2, double y2) {
        double xdiff = x1 - x2;
        double ydiff = y1 - y2;
        double angle;
        if (xdiff == 0.0d && ydiff == 0.0d) {
            angle = -1.0d;
        } else {
            if (xdiff <= 0.0d) {
                if (xdiff == 0.0d) {
                    if (ydiff <= 0.0d) {
                        angle = 0.0d;
                    } else {
                        angle = Math.PI;
                    }
                } else {
                    if (ydiff <= 0.0d) {
                        if (ydiff == 0.0d) {
                            angle = Math.PI / 2.0d;
                        } else {
                            angle = Math.atan(Math.abs(xdiff / ydiff));
                        }
                    } else {
                        angle = Math.PI - Math.atan(Math.abs(xdiff / ydiff));
                    }
                }
            } else {
                if (ydiff <= 0.0d) {
                    if (ydiff == 0.0d) {
                        angle = 3.0d * Math.PI / 2.0d;
                    } else {
                        angle = (2.0d * Math.PI) - Math.atan(Math.abs(xdiff / ydiff));
                    }
                } else {
                    angle = Math.PI + Math.atan(Math.abs(xdiff / ydiff));
                }
            }
        }
        return angle;
    }
}

Related

  1. angle(double a, double b)
  2. angle(double aX, double aY, double bX, double bY)
  3. angle(double px, double py, double qx, double qy)
  4. angle(double x1, double y1, double x2, double y2)
  5. angle(double x1, double y1, double x2, double y2)
  6. angle(double[] vec1, double[] vec2)
  7. angle(float x, float y, float x1, float y1)