Android Trigonometric Functions tanf(float degree)

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

Description

Return the value of tan(degree)

Declaration

public static float tanf(float degree) 

Method Source Code

import android.util.FloatMath;

public class Main{
    /**/*w ww .  j a v a  2 s.c  o 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 tan(degree)
     */
    public static float tanf(float degree) {
        return (float) Math.tan(constrainAngle(degree));
    }
    /**
     * 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)];
    }
    /**
     * 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;

    }
    /**
     * 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);
        }
    }
    /**
     * 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 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];
    }
}

Related

  1. cosr(float radians)
  2. sin(float degree)
  3. sinf(float degree)
  4. sinr(float radians)
  5. tan(float degree)
  6. tanr(float radians)