Android Trigonometric Functions cos(float degree)

Here you can find the source of cos(float degree)

Description

Return the value of cos(degree)

Declaration

public static float cos(float degree) 

Method Source Code

import android.util.FloatMath;

public class Main{
    /**//from   ww w . j  av a 2  s . co  m
     * Convert degrees to Radians.
     */
    public static final float TO_RADIANS = 0.0174532925f;
    /**
     * 360 degrees of SINE
     */
    private static float[] SINE = null;
    /**
     * 360 degrees of COSINE
     */
    private static float[] COSINE = null;
    /**
     * 360 degrees of TAN
     */
    private static float[] TANGENT = null;
    /**
     * Return the value of cos(degree)
     */
    public static float cos(float degree) {
        //do we need to initialise the arrays?
        if (COSINE == null)
            initialise();

        //get the cosine value wanted
        int deg = (int) constrainAngle(degree);
        return COSINE[deg];
    }
    /**
     * Initialise the SINE, COSINE and TANGENT arrays
     */
    private static void initialise() {
        //initialise arrays
        SINE = new float[360];
        COSINE = new float[360];
        TANGENT = new float[360];

        //fill them
        for (int i = 0; i < 360; i++) {
            SINE[i] = android.util.FloatMath.sin(i * TO_RADIANS);
            COSINE[i] = android.util.FloatMath.cos(i * TO_RADIANS);
            TANGENT[i] = (float) Math.tan(i * TO_RADIANS);
        }
    }
    /**
     * Constrain a given degree to between 0-359
     */
    public static float constrainAngle(float degree) {
        while (degree < 0.0f || degree >= 360.0f) {
            if (degree < 0.0f)
                degree = 360.0f + degree;
            else if (degree >= 360.0f)
                degree -= 360.0f;
        }
        return degree;

    }
    /**
     * Return the value of sin(degree)
     */
    public static float sin(float degree) {
        //do we need to initialise the arrays?
        if (SINE == null)
            initialise();

        //get the sine value wanted
        int deg = (int) constrainAngle(degree);
        return SINE[deg];
    }
    /**
     * Return the value of tan(degree)
     */
    public static float tan(float degree) {
        //do we need to initialise the arrays?
        if (TANGENT == null)
            initialise();

        //get the tangent value wanted
        return TANGENT[(int) constrainAngle(degree)];
    }
}

Related

  1. arctan(float opp, float adj)
  2. arctan(float value)
  3. cosf(float degree)
  4. cosr(float radians)
  5. sin(float degree)
  6. sinf(float degree)