Android Trigonometric Functions sinf(float degree)

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

Description

Return the value of sin(degree)

Declaration

public static float sinf(float degree) 

Method Source Code

import android.util.FloatMath;

public class Main{
    /**//w  ww . ja v a2  s.  com
     * 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 sin(degree)
     */
    public static float sinf(float degree) {
        return FloatMath.sin(constrainAngle(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];
    }
    /**
     * 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 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];
    }
    /**
     * 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 value)
  2. cos(float degree)
  3. cosf(float degree)
  4. cosr(float radians)
  5. sin(float degree)
  6. sinr(float radians)
  7. tan(float degree)
  8. tanf(float degree)
  9. tanr(float radians)