Subversion Repositories AndroidProjects

Rev

Rev 265 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
265 chris 1
package com.gebauz.Bauzoid.math;
2
 
3
public class MathUtil
4
{
5
        static public final float EPSILON = 0.0000001f;
6
 
7
        /** Clamp value to range. */
8
        public static float clamp(float value, float min, float max)
9
        {
10
                if (value < min)
11
                        return min;
12
                if (value > max)
13
                        return max;
14
                return value;
15
        }
16
 
17
        /** Clamp value to range. */
18
        public static int clamp(int value, int min, int max)
19
        {
20
                if (value < min)
21
                        return min;
22
                if (value > max)
23
                        return max;
24
                return value;
25
        }
26
 
27
        /** Float-based sine function in degrees. */
28
        public static float sin(float degrees)
29
        {
30
                return (float)Math.sin(degToRad(degrees));
31
        }
32
 
33
        /** Float-based cosine function in degrees. */
34
        public static float cos(float degrees)
35
        {
36
                return (float)Math.cos(degToRad(degrees));
37
        }
38
 
39
        /** Float-based tangent function in degrees. */
40
        public static float tan(float degrees)
41
        {
42
                return (float)Math.tan(degToRad(degrees));
43
        }
44
 
45
        /** Convert from degrees to radians. */
46
        public static float degToRad(float degrees)
47
        {
48
                return (float)(degrees * Math.PI / 180.0f);
49
        }
50
 
51
        /** Convert from radians to degrees. */
52
        public static float radToDeg(float rad)
53
        {
54
                return (float)(rad * 180.0f / Math.PI);
55
        }
269 chris 56
 
265 chris 57
        /** Return the angle inside a range of 0 to 360 degrees. */
58
        public static float stayInDegrees0to360(float degrees)
59
        {
60
                float result = degrees;
61
                while (result > 360.0f)
62
                        result -= 360.0f;
63
                while (result < 0.0f)
64
                        result += 360.0f;
65
                return result;
66
        }
67
 
68
        /** Return the (smaller) angle in degrees between the two angles. */
69
        public static float turnDegrees(float rot1, float rot2)
70
        {
71
                if (Math.abs(rot1 - rot2) > (180.0f))
72
                {
73
                        if (rot1 < rot2)
74
                        {
75
                                rot1 += 360.0f;
76
                        }
77
                        else
78
                        {
79
                                rot1 -= 360.0f;
80
                        }
81
                }
82
                return Math.abs(rot2-rot1);
83
        }
84
 
85
        /** Simple ease in/ease out function. */
86
        public static float ease(float t)
87
        {
88
                return (t * t * (3.0f - 2.0f * t));
89
        }
90
 
91
/*      public static float easeOut(float t, float startValue, float changeValue)
92
        {
93
 
94
        }
95
 
96
        Math.easeOutQuad = function (t, b, c, d) {
97
                t /= d;
98
                return -c * t*(t-2) + b;
99
        };*/
100
}
101