Java atan2 atan2(double y, double x)

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

Description

atan

License

Open Source License

Declaration

static public double atan2(double y, double x) 

Method Source Code

//package com.java2s;
/**//from  w w  w .j av  a  2s  . c o  m
 * <p>Title: Class for float-point calculations in J2ME applications CLDC 1.1</p>
 * <p>Description: Useful methods for float-point calculations which absent in native Math class</p>
 * <p>Copyright: Copyright (c) 2004 Nick Henson</p>
 * <p>Company: UNTEH</p>
 * <p>License: Free use only for non-commercial purpose</p>
 * <p>If you want to use all or part of this class for commercial applications then take into account these conditions:</p>
 * <p>1. I need a one copy of your product which includes my class with license key and so on</p>
 * <p>2. Please append my copyright information henson.midp.Float (C) by Nikolay Klimchuk on ?About? screen of your product</p>
 * <p>3. If you have web site please append link <a href=?http://henson.newmail.ru?>Nikolay Klimchuk</a> on the page with description of your product</p>
 * <p>That's all, thank you!</p>
 * @author Nikolay Klimchuk http://henson.newmail.ru
 * @version 0.5
 */

public class Main {
    /** Square root from 3 */
    final static public double SQRT3 = 1.732050807568877294;

    static public double atan2(double y, double x) {
        // if x=y=0
        if (y == 0. && x == 0.)
            return 0.;
        // if x>0 atan(y/x)
        if (x > 0.)
            return atan(y / x);
        // if x<0 sign(y)*(pi - atan(|y/x|))
        if (x < 0.) {
            if (y < 0.)
                return -(Math.PI - atan(y / x));
            else
                return Math.PI - atan(-y / x);
        }
        // if x=0 y!=0 sign(y)*pi/2
        if (y < 0.)
            return -Math.PI / 2.;
        else
            return Math.PI / 2.;
    }

    static public double atan(double x) {
        boolean signChange = false;
        boolean Invert = false;
        int sp = 0;
        double x2, a;
        // check up the sign change
        if (x < 0.) {
            x = -x;
            signChange = true;
        }
        // check up the invertation
        if (x > 1.) {
            x = 1 / x;
            Invert = true;
        }
        // process shrinking the domain until x<PI/12
        while (x > Math.PI / 12) {
            sp++;
            a = x + SQRT3;
            a = 1 / a;
            x = x * SQRT3;
            x = x - 1;
            x = x * a;
        }
        // calculation core
        x2 = x * x;
        a = x2 + 1.4087812;
        a = 0.55913709 / a;
        a = a + 0.60310579;
        a = a - (x2 * 0.05160454);
        a = a * x;
        // process until sp=0
        while (sp > 0) {
            a = a + Math.PI / 6;
            sp--;
        }
        // invertation took place
        if (Invert)
            a = Math.PI / 2 - a;
        // sign change took place
        if (signChange)
            a = -a;
        //
        return a;
    }
}

Related

  1. atan2(double arg1, double arg2)
  2. atan2(double x, double y)
  3. atan2(double y, double x)
  4. atan2(double y, double x)
  5. atan2(double y, double x)
  6. atan2(double[] arr1, double[] arr2)
  7. atan2(final double y, final double x)
  8. atan2(float y, float x)