Return the value of sinf(degree) - Android java.lang

Android examples for java.lang:Math Trigonometric Function

Description

Return the value of sinf(degree)

Demo Code


import android.util.FloatMath;

public class Main{
    /**//from  w w  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 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 Tutorials