Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

package com.gebauz.bauzoid.math;

public class MathUtil
{
        static public final float EPSILON = 0.0000001f;
       
        /** Check if value is in range (inclusive). */
        public static boolean isInRange(float value, float min, float max)
        {
                return ((value >= min) && (value <= max));             
        }
       
        /** Check if value is in range (inclusive min, exclusive max). */
        public static boolean isInRangeInEx(float value, float min, float max)
        {
                return ((value >= min) && (value < max));              
        }
       
        /** Check if value is in range (exclusive min, inclusive max). */
        public static boolean isInRangeExIn(float value, float min, float max)
        {
                return ((value > min) && (value <= max));              
        }
       
        /** Check if value is in range (exclusive). */
        public static boolean isInRangeEx(float value, float min, float max)
        {
                return ((value > min) && (value < max));               
        }
       
        /** Check if value is in range (inclusive). */
        public static boolean isInRange(int value, int min, int max)
        {
                return ((value >= min) && (value <= max));
        }

        /** Clamp value to range. */
        public static float clamp(float value, float min, float max)
        {
                if (value < min)
                        return min;
                if (value > max)
                        return max;
                return value;
        }
       
        /** Clamp value to range. */
        public static int clamp(int value, int min, int max)
        {
                if (value < min)
                        return min;
                if (value > max)
                        return max;
                return value;
        }
       
        /** Float-based sine function in degrees. */
        public static float sin(float degrees)
        {
                return (float)Math.sin(degToRad(degrees));
        }
       
        /** Float-based cosine function in degrees. */
        public static float cos(float degrees)
        {
                return (float)Math.cos(degToRad(degrees));
        }
       
        /** Float-based tangent function in degrees. */
        public static float tan(float degrees)
        {
                return (float)Math.tan(degToRad(degrees));
        }
       
        /** Convert from degrees to radians. */
        public static float degToRad(float degrees)
        {
                return (float)(degrees * Math.PI / 180.0f);
        }
       
        /** Convert from radians to degrees. */
        public static float radToDeg(float rad)
        {
                return (float)(rad * 180.0f / Math.PI);
        }
       
        /** Return the angle inside a range of 0 to 360 degrees. */
        public static float stayInDegrees0to360(float degrees)
        {
                float result = degrees;
                while (result > 360.0f)
                        result -= 360.0f;
                while (result < 0.0f)
                        result += 360.0f;
                return result;
        }
       
        /** Return the (smaller) angle in degrees between the two angles. */
        public static float turnDegrees(float rot1, float rot2)
        {
                if (Math.abs(rot1 - rot2) > (180.0f))
                {
                        if (rot1 < rot2)
                        {
                                rot1 += 360.0f;
                        }
                        else
                        {
                                rot1 -= 360.0f;
                        }
                }
                return Math.abs(rot2-rot1);
        }
       
        /** Simple ease in/ease out function. */
/*      public static float ease(float t)
        {
                return (t * t * (3.0f - 2.0f * t));
        }*/

       
/*      public static float easeOut(float t, float startValue, float changeValue)
        {
               
        }
       
        Math.easeOutQuad = function (t, b, c, d) {
                t /= d;
                return -c * t*(t-2) + b;
        };*/

}