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