Blame view

src/jsky/coords/Trigod.java 3.52 KB
fe0fb87e   Elodie Bourrec   First push to cre...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * ESO Archive
 *
 * $Id: Trigod.java,v 1.2 2009/02/20 23:10:11 abrighto Exp $
 *
 * who             when        what
 * --------------  ----------  ----------------------------------------
 * Allan Brighton  1999/05/10  port of trigo.c, Francois Ochsenbein [ESO-IPG]
 */

package jsky.coords;

/**
 * Trigonometric Function in degrees.
 * <p>
 * This class is based on C routintes by Francois Ochsenbein [ESO-IPG].
 */
public class Trigod {

    /** max floating value */
    static final double DOUBLE_MAX = 1.7e38;

    /** radians to degrees */
    static final double DEG = (180.e0 / Math.PI);

    /**
     * Return the fractional part of d.
     */
    private static double fractionalPart(double d) {
        return Math.abs(d - (int) d);
    }

    /**
     * Computation of cosine (argument in degrees).
     * @param x argument in degrees
     * @return cosine (double).
     */
    public static double cosd(double x) {
        int sign = 0;
        double argument = fractionalPart(x / 360.e0);
        if (argument > .5e0)
            argument = 1.e0 - argument;
        if (argument > .25e0) {
            argument = .5e0 - argument;
            sign = 1;
        }
        if (argument > .125e0)
            argument = Math.sin((Math.PI * 2) * (.25e0 - argument));
        else
            argument = Math.cos((Math.PI * 2) * argument);
        if (sign != 0)
            argument = -argument;
        return argument;
    }

    /**
     * Computes the sine (argument in degrees)
     * @return sine of argument (double)
     */
    public static double sind(double x) {
        double argument = fractionalPart(x / 360.e0);
        int sign = (x >= 0.e0 ? 0 : 1);
        if (argument > .5e0) {
            argument = 1.e0 - argument;
            sign ^= 1;
        }
        if (argument > .25e0)
            argument = .5e0 - argument;
        if (argument > .125e0)
            argument = Math.cos((Math.PI * 2) * (.25e0 - argument));
        else
            argument = Math.sin((Math.PI * 2) * argument);
        if (sign != 0)
            argument = -argument;
        return argument;
    }


    /**
     * Computes the tangent (argument in degrees).
     * For +90 degrees, DOUBLE_MAX is returned;
     * For -90 degrees, -DOUBLE_MAX is returned
     * @return tangent of argument (double)
     */
    public static double tand(double x) {
        double argument = fractionalPart(x / 180.e0);
        if (argument == .5e0)
            argument = DOUBLE_MAX;
        else
            argument = Math.tan(Math.PI * argument);
        return (x > 0.e0 ? argument : -argument);
    }

    /*
     * Computes the Arc tan in degrees.
     * @return Arc tangent of argument (double), in range [-90, 90].
     */
    public static double atand(double x) {
        return (DEG * Math.atan(x));
    }

    /**
     * Cartesian to polar.
     * @param x X argument in degrees.
     * @param y Y argument in degrees.
     * @return Angle in range [-180, 180].
     */
    public static double atan2d(double x, double y) {
        //noinspection SuspiciousNameCombination
        return (DEG * Math.atan2(x, y));
    }

    /**
     * Computes the Arc cos in degrees (Range of argument [-1, +1]).
     * @return Arc cosine of argument (double), in range [0, 180].
     */
    public static double acosd(double x) {
        return (DEG * Math.acos(x));
    }

    /**
     * Computes the Arc sine in degrees (Range of argument [-1, +1]).
     * @return Arc tangent of argument (double) in range [-90, 90].
     */
    double asind(double x) {
        return (DEG * Math.asin(x));
    }
}