• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.util;
18 
19 import java.util.Random;
20 
21 /**
22  * A class that contains utility methods related to numbers.
23  *
24  * @hide Pending API council approval
25  */
26 public final class MathUtils {
27     private static final Random sRandom = new Random();
28     private static final float DEG_TO_RAD = 3.1415926f / 180.0f;
29     private static final float RAD_TO_DEG = 180.0f / 3.1415926f;
30 
MathUtils()31     private MathUtils() {
32     }
33 
abs(float v)34     public static float abs(float v) {
35         return v > 0 ? v : -v;
36     }
37 
constrain(int amount, int low, int high)38     public static int constrain(int amount, int low, int high) {
39         return amount < low ? low : (amount > high ? high : amount);
40     }
41 
constrain(long amount, long low, long high)42     public static long constrain(long amount, long low, long high) {
43         return amount < low ? low : (amount > high ? high : amount);
44     }
45 
constrain(float amount, float low, float high)46     public static float constrain(float amount, float low, float high) {
47         return amount < low ? low : (amount > high ? high : amount);
48     }
49 
log(float a)50     public static float log(float a) {
51         return (float) Math.log(a);
52     }
53 
exp(float a)54     public static float exp(float a) {
55         return (float) Math.exp(a);
56     }
57 
pow(float a, float b)58     public static float pow(float a, float b) {
59         return (float) Math.pow(a, b);
60     }
61 
max(float a, float b)62     public static float max(float a, float b) {
63         return a > b ? a : b;
64     }
65 
max(int a, int b)66     public static float max(int a, int b) {
67         return a > b ? a : b;
68     }
69 
max(float a, float b, float c)70     public static float max(float a, float b, float c) {
71         return a > b ? (a > c ? a : c) : (b > c ? b : c);
72     }
73 
max(int a, int b, int c)74     public static float max(int a, int b, int c) {
75         return a > b ? (a > c ? a : c) : (b > c ? b : c);
76     }
77 
min(float a, float b)78     public static float min(float a, float b) {
79         return a < b ? a : b;
80     }
81 
min(int a, int b)82     public static float min(int a, int b) {
83         return a < b ? a : b;
84     }
85 
min(float a, float b, float c)86     public static float min(float a, float b, float c) {
87         return a < b ? (a < c ? a : c) : (b < c ? b : c);
88     }
89 
min(int a, int b, int c)90     public static float min(int a, int b, int c) {
91         return a < b ? (a < c ? a : c) : (b < c ? b : c);
92     }
93 
dist(float x1, float y1, float x2, float y2)94     public static float dist(float x1, float y1, float x2, float y2) {
95         final float x = (x2 - x1);
96         final float y = (y2 - y1);
97         return (float) Math.sqrt(x * x + y * y);
98     }
99 
dist(float x1, float y1, float z1, float x2, float y2, float z2)100     public static float dist(float x1, float y1, float z1, float x2, float y2, float z2) {
101         final float x = (x2 - x1);
102         final float y = (y2 - y1);
103         final float z = (z2 - z1);
104         return (float) Math.sqrt(x * x + y * y + z * z);
105     }
106 
mag(float a, float b)107     public static float mag(float a, float b) {
108         return (float) Math.sqrt(a * a + b * b);
109     }
110 
mag(float a, float b, float c)111     public static float mag(float a, float b, float c) {
112         return (float) Math.sqrt(a * a + b * b + c * c);
113     }
114 
sq(float v)115     public static float sq(float v) {
116         return v * v;
117     }
118 
radians(float degrees)119     public static float radians(float degrees) {
120         return degrees * DEG_TO_RAD;
121     }
122 
degrees(float radians)123     public static float degrees(float radians) {
124         return radians * RAD_TO_DEG;
125     }
126 
acos(float value)127     public static float acos(float value) {
128         return (float) Math.acos(value);
129     }
130 
asin(float value)131     public static float asin(float value) {
132         return (float) Math.asin(value);
133     }
134 
atan(float value)135     public static float atan(float value) {
136         return (float) Math.atan(value);
137     }
138 
atan2(float a, float b)139     public static float atan2(float a, float b) {
140         return (float) Math.atan2(a, b);
141     }
142 
tan(float angle)143     public static float tan(float angle) {
144         return (float) Math.tan(angle);
145     }
146 
lerp(float start, float stop, float amount)147     public static float lerp(float start, float stop, float amount) {
148         return start + (stop - start) * amount;
149     }
150 
norm(float start, float stop, float value)151     public static float norm(float start, float stop, float value) {
152         return (value - start) / (stop - start);
153     }
154 
map(float minStart, float minStop, float maxStart, float maxStop, float value)155     public static float map(float minStart, float minStop, float maxStart, float maxStop, float value) {
156         return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
157     }
158 
random(int howbig)159     public static int random(int howbig) {
160         return (int) (sRandom.nextFloat() * howbig);
161     }
162 
random(int howsmall, int howbig)163     public static int random(int howsmall, int howbig) {
164         if (howsmall >= howbig) return howsmall;
165         return (int) (sRandom.nextFloat() * (howbig - howsmall) + howsmall);
166     }
167 
random(float howbig)168     public static float random(float howbig) {
169         return sRandom.nextFloat() * howbig;
170     }
171 
random(float howsmall, float howbig)172     public static float random(float howsmall, float howbig) {
173         if (howsmall >= howbig) return howsmall;
174         return sRandom.nextFloat() * (howbig - howsmall) + howsmall;
175     }
176 
randomSeed(long seed)177     public static void randomSeed(long seed) {
178         sRandom.setSeed(seed);
179     }
180 }
181