Romain Guy | a32d100 | 2009-07-31 15:33:59 -0700 | [diff] [blame] | 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 | |
| 31 | private MathUtils() { |
| 32 | } |
| 33 | |
| 34 | public static float abs(float v) { |
| 35 | return v > 0 ? v : -v; |
| 36 | } |
| 37 | |
| 38 | public static int constrain(int amount, int low, int high) { |
| 39 | return amount < low ? low : (amount > high ? high : amount); |
| 40 | } |
| 41 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 42 | public static long constrain(long amount, long low, long high) { |
| 43 | return amount < low ? low : (amount > high ? high : amount); |
| 44 | } |
| 45 | |
Romain Guy | a32d100 | 2009-07-31 15:33:59 -0700 | [diff] [blame] | 46 | public static float constrain(float amount, float low, float high) { |
| 47 | return amount < low ? low : (amount > high ? high : amount); |
| 48 | } |
| 49 | |
| 50 | public static float log(float a) { |
| 51 | return (float) Math.log(a); |
| 52 | } |
| 53 | |
| 54 | public static float exp(float a) { |
| 55 | return (float) Math.exp(a); |
| 56 | } |
| 57 | |
| 58 | public static float pow(float a, float b) { |
| 59 | return (float) Math.pow(a, b); |
| 60 | } |
| 61 | |
| 62 | public static float max(float a, float b) { |
| 63 | return a > b ? a : b; |
| 64 | } |
| 65 | |
| 66 | public static float max(int a, int b) { |
| 67 | return a > b ? a : b; |
| 68 | } |
| 69 | |
| 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 | |
| 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 | |
| 78 | public static float min(float a, float b) { |
| 79 | return a < b ? a : b; |
| 80 | } |
| 81 | |
| 82 | public static float min(int a, int b) { |
| 83 | return a < b ? a : b; |
| 84 | } |
| 85 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 107 | public static float mag(float a, float b) { |
| 108 | return (float) Math.sqrt(a * a + b * b); |
| 109 | } |
| 110 | |
| 111 | public static float mag(float a, float b, float c) { |
| 112 | return (float) Math.sqrt(a * a + b * b + c * c); |
| 113 | } |
| 114 | |
| 115 | public static float sq(float v) { |
| 116 | return v * v; |
| 117 | } |
| 118 | |
| 119 | public static float radians(float degrees) { |
| 120 | return degrees * DEG_TO_RAD; |
| 121 | } |
| 122 | |
Romain Guy | a9d2d5e | 2009-08-09 17:04:54 -0700 | [diff] [blame] | 123 | public static float degrees(float radians) { |
Romain Guy | a32d100 | 2009-07-31 15:33:59 -0700 | [diff] [blame] | 124 | return radians * RAD_TO_DEG; |
| 125 | } |
| 126 | |
| 127 | public static float acos(float value) { |
| 128 | return (float) Math.acos(value); |
| 129 | } |
| 130 | |
| 131 | public static float asin(float value) { |
| 132 | return (float) Math.asin(value); |
| 133 | } |
| 134 | |
| 135 | public static float atan(float value) { |
| 136 | return (float) Math.atan(value); |
| 137 | } |
| 138 | |
| 139 | public static float atan2(float a, float b) { |
| 140 | return (float) Math.atan2(a, b); |
| 141 | } |
| 142 | |
| 143 | public static float tan(float angle) { |
| 144 | return (float) Math.tan(angle); |
| 145 | } |
| 146 | |
| 147 | public static float lerp(float start, float stop, float amount) { |
| 148 | return start + (stop - start) * amount; |
| 149 | } |
| 150 | |
Romain Guy | e85c5aa | 2009-08-12 15:23:56 -0700 | [diff] [blame] | 151 | public static float norm(float start, float stop, float value) { |
| 152 | return (value - start) / (stop - start); |
| 153 | } |
| 154 | |
| 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 | |
Romain Guy | a32d100 | 2009-07-31 15:33:59 -0700 | [diff] [blame] | 159 | public static int random(int howbig) { |
| 160 | return (int) (sRandom.nextFloat() * howbig); |
| 161 | } |
| 162 | |
| 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 | |
| 168 | public static float random(float howbig) { |
| 169 | return sRandom.nextFloat() * howbig; |
| 170 | } |
| 171 | |
| 172 | public static float random(float howsmall, float howbig) { |
| 173 | if (howsmall >= howbig) return howsmall; |
| 174 | return sRandom.nextFloat() * (howbig - howsmall) + howsmall; |
| 175 | } |
| 176 | |
| 177 | public static void randomSeed(long seed) { |
| 178 | sRandom.setSeed(seed); |
| 179 | } |
| 180 | } |