| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.graphics.Rect; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public final class MathUtils { |
| 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 |
@UnsupportedAppUsage |
| 35 |
public static float abs(float v) { |
| 36 |
return v > 0 ? v : -v; |
| 37 |
} |
| 38 |
|
| 39 |
@UnsupportedAppUsage |
| 40 |
public static int constrain(int amount, int low, int high) { |
| 41 |
return amount < low ? low : (amount > high ? high : amount); |
| 42 |
} |
| 43 |
|
| 44 |
public static long constrain(long amount, long low, long high) { |
| 45 |
return amount < low ? low : (amount > high ? high : amount); |
| 46 |
} |
| 47 |
|
| 48 |
@UnsupportedAppUsage |
| 49 |
public static float constrain(float amount, float low, float high) { |
| 50 |
return amount < low ? low : (amount > high ? high : amount); |
| 51 |
} |
| 52 |
|
| 53 |
public static float log(float a) { |
| 54 |
return (float) Math.log(a); |
| 55 |
} |
| 56 |
|
| 57 |
public static float exp(float a) { |
| 58 |
return (float) Math.exp(a); |
| 59 |
} |
| 60 |
|
| 61 |
public static float pow(float a, float b) { |
| 62 |
return (float) Math.pow(a, b); |
| 63 |
} |
| 64 |
|
| 65 |
public static float sqrt(float a) { |
| 66 |
return (float) Math.sqrt(a); |
| 67 |
} |
| 68 |
|
| 69 |
public static float max(float a, float b) { |
| 70 |
return a > b ? a : b; |
| 71 |
} |
| 72 |
|
| 73 |
@UnsupportedAppUsage |
| 74 |
public static float max(int a, int b) { |
| 75 |
return a > b ? a : b; |
| 76 |
} |
| 77 |
|
| 78 |
public static float max(float a, float b, float c) { |
| 79 |
return a > b ? (a > c ? a : c) : (b > c ? b : c); |
| 80 |
} |
| 81 |
|
| 82 |
public static float max(int a, int b, int c) { |
| 83 |
return a > b ? (a > c ? a : c) : (b > c ? b : c); |
| 84 |
} |
| 85 |
|
| 86 |
public static float min(float a, float b) { |
| 87 |
return a < b ? a : b; |
| 88 |
} |
| 89 |
|
| 90 |
public static float min(int a, int b) { |
| 91 |
return a < b ? a : b; |
| 92 |
} |
| 93 |
|
| 94 |
public static float min(float a, float b, float c) { |
| 95 |
return a < b ? (a < c ? a : c) : (b < c ? b : c); |
| 96 |
} |
| 97 |
|
| 98 |
public static float min(int a, int b, int c) { |
| 99 |
return a < b ? (a < c ? a : c) : (b < c ? b : c); |
| 100 |
} |
| 101 |
|
| 102 |
public static float dist(float x1, float y1, float x2, float y2) { |
| 103 |
final float x = (x2 - x1); |
| 104 |
final float y = (y2 - y1); |
| 105 |
return (float) Math.hypot(x, y); |
| 106 |
} |
| 107 |
|
| 108 |
public static float dist(float x1, float y1, float z1, float x2, float y2, float z2) { |
| 109 |
final float x = (x2 - x1); |
| 110 |
final float y = (y2 - y1); |
| 111 |
final float z = (z2 - z1); |
| 112 |
return (float) Math.sqrt(x * x + y * y + z * z); |
| 113 |
} |
| 114 |
|
| 115 |
public static float mag(float a, float b) { |
| 116 |
return (float) Math.hypot(a, b); |
| 117 |
} |
| 118 |
|
| 119 |
public static float mag(float a, float b, float c) { |
| 120 |
return (float) Math.sqrt(a * a + b * b + c * c); |
| 121 |
} |
| 122 |
|
| 123 |
public static float sq(float v) { |
| 124 |
return v * v; |
| 125 |
} |
| 126 |
|
| 127 |
public static float dot(float v1x, float v1y, float v2x, float v2y) { |
| 128 |
return v1x * v2x + v1y * v2y; |
| 129 |
} |
| 130 |
|
| 131 |
public static float cross(float v1x, float v1y, float v2x, float v2y) { |
| 132 |
return v1x * v2y - v1y * v2x; |
| 133 |
} |
| 134 |
|
| 135 |
public static float radians(float degrees) { |
| 136 |
return degrees * DEG_TO_RAD; |
| 137 |
} |
| 138 |
|
| 139 |
public static float degrees(float radians) { |
| 140 |
return radians * RAD_TO_DEG; |
| 141 |
} |
| 142 |
|
| 143 |
public static float acos(float value) { |
| 144 |
return (float) Math.acos(value); |
| 145 |
} |
| 146 |
|
| 147 |
public static float asin(float value) { |
| 148 |
return (float) Math.asin(value); |
| 149 |
} |
| 150 |
|
| 151 |
public static float atan(float value) { |
| 152 |
return (float) Math.atan(value); |
| 153 |
} |
| 154 |
|
| 155 |
public static float atan2(float a, float b) { |
| 156 |
return (float) Math.atan2(a, b); |
| 157 |
} |
| 158 |
|
| 159 |
public static float tan(float angle) { |
| 160 |
return (float) Math.tan(angle); |
| 161 |
} |
| 162 |
|
| 163 |
@UnsupportedAppUsage |
| 164 |
public static float lerp(float start, float stop, float amount) { |
| 165 |
return start + (stop - start) * amount; |
| 166 |
} |
| 167 |
|
| 168 |
public static float lerp(int start, int stop, float amount) { |
| 169 |
return lerp((float) start, (float) stop, amount); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public static float lerpInv(float a, float b, float value) { |
| 179 |
return a != b ? ((value - a) / (b - a)) : 0.0f; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
public static float saturate(float value) { |
| 184 |
return constrain(value, 0.0f, 1.0f); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
public static float lerpInvSat(float a, float b, float value) { |
| 189 |
return saturate(lerpInv(a, b, value)); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public static float lerpDeg(float start, float end, float amount) { |
| 209 |
final float minAngle = (((end - start) + 180) % 360) - 180; |
| 210 |
return minAngle * amount + start; |
| 211 |
} |
| 212 |
|
| 213 |
public static float norm(float start, float stop, float value) { |
| 214 |
return (value - start) / (stop - start); |
| 215 |
} |
| 216 |
|
| 217 |
public static float map(float minStart, float minStop, float maxStart, float maxStop, float value) { |
| 218 |
return maxStart + (maxStop - maxStart) * ((value - minStart) / (minStop - minStart)); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public static float constrainedMap( |
| 243 |
float rangeMin, float rangeMax, float valueMin, float valueMax, float value) { |
| 244 |
return lerp(rangeMin, rangeMax, lerpInvSat(valueMin, valueMax, value)); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public static float smoothStep(float start, float end, float x) { |
| 259 |
return constrain((x - start) / (end - start), 0f, 1f); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public static int addOrThrow(int a, int b) throws IllegalArgumentException { |
| 268 |
if (b == 0) { |
| 269 |
return a; |
| 270 |
} |
| 271 |
|
| 272 |
if (b > 0 && a <= (Integer.MAX_VALUE - b)) { |
| 273 |
return a + b; |
| 274 |
} |
| 275 |
|
| 276 |
if (b < 0 && a >= (Integer.MIN_VALUE - b)) { |
| 277 |
return a + b; |
| 278 |
} |
| 279 |
throw new IllegalArgumentException("Addition overflow: " + a + " + " + b); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public static void fitRect(Rect outToResize, int largestSide) { |
| 289 |
if (outToResize.isEmpty()) { |
| 290 |
return; |
| 291 |
} |
| 292 |
float maxSize = Math.max(outToResize.width(), outToResize.height()); |
| 293 |
outToResize.scale(largestSide / maxSize); |
| 294 |
} |
| 295 |
} |