Java atan2 atan2(double x, double y)

Here you can find the source of atan2(double x, double y)

Description

Approximates the atan2 function.

License

Open Source License

Parameter

Parameter Description
x a parameter
y a parameter

Return

the calculated value

Declaration

public static double atan2(double x, double y) 

Method Source Code

//package com.java2s;
/*//w ww  . j  a v  a 2  s.  c o m
 * Created on 02-May-2006 at 17:31:01.
 * 
 * Copyright (c) 2010 Robert Virkus / Enough Software
 *
 * This file is part of J2ME Polish.
 *
 * J2ME Polish 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 2 of the License, or
 * (at your option) any later version.
 * 
 * J2ME Polish 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 J2ME Polish; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Commercial licenses are also available, please
 * refer to the accompanying LICENSE.txt or visit
 * http://www.j2mepolish.org for details.
 */

public class Main {
    /**
     * Approximates the atan2 function. Results are in the [0,2*PI) range.
     * 
     * @param x
     * @param y
     * @return the calculated value
     */
    public static double atan2(double x, double y) {
        // Origin - return zero
        if (y == 0.0 && x == 0.0) {
            return 0.0;
        } else if (x > 0.0) {
            if (y > 0.0) { // Point is in first quadrant
                return atan(y / x);
            } else { // Point is in fourth quadrant
                return 2 * Math.PI - atan(-y / x);
            }
        } else if (x < 0.0) {
            if (y < 0.0) { // Point is in third quadrant
                return Math.PI + atan(y / x);
            } else { // Point is in second quadrant
                return Math.PI - atan(-y / x);
            }
        } else if (y < 0.0) { // Special cases for when the point is directly on the Y axis.
            return 2 * Math.PI - Math.PI / 2.;
        } else {
            return Math.PI / 2.;
        }
    }

    /**
     * Approximates the atan function. Uses a polynomial approximation that should
     * be accurate enough for most practical purposes.
     * 
     * @param x
     * @return the calculated value
     */
    public static double atan(double x) {
        double res;

        if (Math.abs(x) < 1) {
            res = x / (1 + 0.28 * x * x);
        } else {
            // NOTE : Due to some weird JVM behavior,
            // if x is negative it's more accurate to calculate
            // atan for -x and then negate it, rather than calculate atan
            // for x directly.
            if (x < 0.0) {
                x = -x;
                res = (Math.PI / 2 - x / (x * x + 0.28));
                res = -res;
            } else {
                res = (Math.PI / 2 - x / (x * x + 0.28));
            }
        }

        return res;
    }
}

Related

  1. atan2(double arg1, double arg2)
  2. atan2(double y, double x)
  3. atan2(double y, double x)
  4. atan2(double y, double x)
  5. atan2(double y, double x)