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 17package android.util; 18 19/** 20 * A class that contains utility methods related to numbers. 21 * 22 * @hide Pending API council approval 23 */ 24public final class MathUtils { 25 private static final float DEG_TO_RAD = 3.1415926f / 180.0f; 26 private static final float RAD_TO_DEG = 180.0f / 3.1415926f; 27 28 private MathUtils() { 29 } 30 31 public static float abs(float v) { 32 return v > 0 ? v : -v; 33 } 34 35 public static int constrain(int amount, int low, int high) { 36 return amount < low ? low : (amount > high ? high : amount); 37 } 38 39 public static long constrain(long amount, long low, long high) { 40 return amount < low ? low : (amount > high ? high : amount); 41 } 42 43 public static float constrain(float amount, float low, float high) { 44 return amount < low ? low : (amount > high ? high : amount); 45 } 46 47 public static float log(float a) { 48 return (float) Math.log(a); 49 } 50 51 public static float exp(float a) { 52 return (float) Math.exp(a); 53 } 54 55 public static float pow(float a, float b) { 56 return (float) Math.pow(a, b); 57 } 58 59 public static float max(float a, float b) { 60 return a > b ? a : b; 61 } 62 63 public static float max(int a, int b) { 64 return a > b ? a : b; 65 } 66 67 public static float max(float a, float b, float c) { 68 return a > b ? (a > c ? a : c) : (b > c ? b : c); 69 } 70 71 public static float max(int a, int b, int c) { 72 return a > b ? (a > c ? a : c) : (b > c ? b : c); 73 } 74 75 public static float min(float a, float b) { 76 return a < b ? a : b; 77 } 78 79 public static float min(int a, int b) { 80 return a < b ? a : b; 81 } 82 83 public static float min(float a, float b, float c) { 84 return a < b ? (a < c ? a : c) : (b < c ? b : c); 85 } 86 87 public static float min(int a, int b, int c) { 88 return a < b ? (a < c ? a : c) : (b < c ? b : c); 89 } 90 91 public static float dist(float x1, float y1, float x2, float y2) { 92 final float x = (x2 - x1); 93 final float y = (y2 - y1); 94 return (float) Math.hypot(x, y); 95 } 96 97 public static float dist(float x1, float y1, float z1, float x2, float y2, float z2) { 98 final float x = (x2 - x1); 99 final float y = (y2 - y1); 100 final float z = (z2 - z1); 101 return (float) Math.sqrt(x * x + y * y + z * z); 102 } 103 104 public static float mag(float a, float b) { 105 return (float) Math.hypot(a, b); 106 } 107 108 public static float mag(float a, float b, float c) { 109 return (float) Math.sqrt(a * a + b * b + c * c); 110 } 111 112 public static float sq(float v) { 113 return v * v; 114 } 115 116 public static float dot(float v1x, float v1y, float v2x, float v2y) { 117 return v1x * v2x + v1y * v2y; 118 } 119 120 public static float cross(float v1x, float v1y, float v2x, float v2y) { 121 return v1x * v2y - v1y * v2x; 122 } 123 124 public static float radians(float degrees) { 125 return degrees * DEG_TO_RAD; 126 } 127 128 public static float degrees(float radians) { 129 return radians * RAD_TO_DEG; 130 } 131 132 public static float acos(float value) { 133 return (float) Math.acos(value); 134 } 135 136 public static float asin(float value) { 137 return (float) Math.asin(value); 138 } 139 140 public static float atan(float value) { 141 return (float) Math.atan(value); 142 } 143 144 public static float atan2(float a, float b) { 145 return (float) Math.atan2(a, b); 146 } 147 148 public static float tan(float angle) { 149 return (float) Math.tan(angle); 150 } 151 152 public static float lerp(float start, float stop, float amount) { 153 return start + (stop - start) * amount; 154 } 155 156 /** 157 * Returns an interpolated angle in degrees between a set of start and end 158 * angles. 159 * <p> 160 * Unlike {@link #lerp(float, float, float)}, the direction and distance of 161 * travel is determined by the shortest angle between the start and end 162 * angles. For example, if the starting angle is 0 and the ending angle is 163 * 350, then the interpolated angle will be in the range [0,-10] rather 164 * than [0,350]. 165 * 166 * @param start the starting angle in degrees 167 * @param end the ending angle in degrees 168 * @param amount the position between start and end in the range [0,1] 169 * where 0 is the starting angle and 1 is the ending angle 170 * @return the interpolated angle in degrees 171 */ 172 public static float lerpDeg(float start, float end, float amount) { 173 final float minAngle = (((end - start) + 180) % 360) - 180; 174 return minAngle * amount + start; 175 } 176 177 public static float norm(float start, float stop, float value) { 178 return (value - start) / (stop - start); 179 } 180 181 public static float map(float minStart, float minStop, float maxStart, float maxStop, float value) { 182 return maxStart + (maxStop - maxStart) * ((value - minStart) / (minStop - minStart)); 183 } 184 185 /** 186 * Returns the sum of the two parameters, or throws an exception if the resulting sum would 187 * cause an overflow or underflow. 188 * @throws IllegalArgumentException when overflow or underflow would occur. 189 */ 190 public static int addOrThrow(int a, int b) throws IllegalArgumentException { 191 if (b == 0) { 192 return a; 193 } 194 195 if (b > 0 && a <= (Integer.MAX_VALUE - b)) { 196 return a + b; 197 } 198 199 if (b < 0 && a >= (Integer.MIN_VALUE - b)) { 200 return a + b; 201 } 202 throw new IllegalArgumentException("Addition overflow: " + a + " + " + b); 203 } 204} 205