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 android.compat.annotation.UnsupportedAppUsage;
20 import android.graphics.Rect;
21 
22 /**
23  * A class that contains utility methods related to numbers.
24  *
25  * @hide Pending API council approval
26  */
27 @android.ravenwood.annotation.RavenwoodKeepWholeClass
28 public final class MathUtils {
29     private static final float DEG_TO_RAD = 3.1415926f / 180.0f;
30     private static final float RAD_TO_DEG = 180.0f / 3.1415926f;
31 
MathUtils()32     private MathUtils() {
33     }
34 
35     @UnsupportedAppUsage
abs(float v)36     public static float abs(float v) {
37         return v > 0 ? v : -v;
38     }
39 
40     @UnsupportedAppUsage
constrain(int amount, int low, int high)41     public static int constrain(int amount, int low, int high) {
42         return amount < low ? low : (amount > high ? high : amount);
43     }
44 
constrain(long amount, long low, long high)45     public static long constrain(long amount, long low, long high) {
46         return amount < low ? low : (amount > high ? high : amount);
47     }
48 
49     @UnsupportedAppUsage
constrain(float amount, float low, float high)50     public static float constrain(float amount, float low, float high) {
51         return amount < low ? low : (amount > high ? high : amount);
52     }
53 
log(float a)54     public static float log(float a) {
55         return (float) Math.log(a);
56     }
57 
exp(float a)58     public static float exp(float a) {
59         return (float) Math.exp(a);
60     }
61 
pow(float a, float b)62     public static float pow(float a, float b) {
63         return (float) Math.pow(a, b);
64     }
65 
sqrt(float a)66     public static float sqrt(float a) {
67         return (float) Math.sqrt(a);
68     }
69 
max(float a, float b)70     public static float max(float a, float b) {
71         return a > b ? a : b;
72     }
73 
74     @UnsupportedAppUsage
max(int a, int b)75     public static float max(int a, int b) {
76         return a > b ? a : b;
77     }
78 
max(float a, float b, float c)79     public static float max(float a, float b, float c) {
80         return a > b ? (a > c ? a : c) : (b > c ? b : c);
81     }
82 
max(int a, int b, int c)83     public static float max(int a, int b, int c) {
84         return a > b ? (a > c ? a : c) : (b > c ? b : c);
85     }
86 
min(float a, float b)87     public static float min(float a, float b) {
88         return a < b ? a : b;
89     }
90 
min(int a, int b)91     public static float min(int a, int b) {
92         return a < b ? a : b;
93     }
94 
min(float a, float b, float c)95     public static float min(float a, float b, float c) {
96         return a < b ? (a < c ? a : c) : (b < c ? b : c);
97     }
98 
min(int a, int b, int c)99     public static float min(int a, int b, int c) {
100         return a < b ? (a < c ? a : c) : (b < c ? b : c);
101     }
102 
dist(float x1, float y1, float x2, float y2)103     public static float dist(float x1, float y1, float x2, float y2) {
104         final float x = (x2 - x1);
105         final float y = (y2 - y1);
106         return (float) Math.hypot(x, y);
107     }
108 
dist(float x1, float y1, float z1, float x2, float y2, float z2)109     public static float dist(float x1, float y1, float z1, float x2, float y2, float z2) {
110         final float x = (x2 - x1);
111         final float y = (y2 - y1);
112         final float z = (z2 - z1);
113         return (float) Math.sqrt(x * x + y * y + z * z);
114     }
115 
mag(float a, float b)116     public static float mag(float a, float b) {
117         return (float) Math.hypot(a, b);
118     }
119 
mag(float a, float b, float c)120     public static float mag(float a, float b, float c) {
121         return (float) Math.sqrt(a * a + b * b + c * c);
122     }
123 
sq(float v)124     public static float sq(float v) {
125         return v * v;
126     }
127 
dot(float v1x, float v1y, float v2x, float v2y)128     public static float dot(float v1x, float v1y, float v2x, float v2y) {
129         return v1x * v2x + v1y * v2y;
130     }
131 
cross(float v1x, float v1y, float v2x, float v2y)132     public static float cross(float v1x, float v1y, float v2x, float v2y) {
133         return v1x * v2y - v1y * v2x;
134     }
135 
radians(float degrees)136     public static float radians(float degrees) {
137         return degrees * DEG_TO_RAD;
138     }
139 
degrees(float radians)140     public static float degrees(float radians) {
141         return radians * RAD_TO_DEG;
142     }
143 
acos(float value)144     public static float acos(float value) {
145         return (float) Math.acos(value);
146     }
147 
asin(float value)148     public static float asin(float value) {
149         return (float) Math.asin(value);
150     }
151 
atan(float value)152     public static float atan(float value) {
153         return (float) Math.atan(value);
154     }
155 
atan2(float a, float b)156     public static float atan2(float a, float b) {
157         return (float) Math.atan2(a, b);
158     }
159 
tan(float angle)160     public static float tan(float angle) {
161         return (float) Math.tan(angle);
162     }
163 
164     @UnsupportedAppUsage
lerp(float start, float stop, float amount)165     public static float lerp(float start, float stop, float amount) {
166         return start + (stop - start) * amount;
167     }
168 
lerp(int start, int stop, float amount)169     public static float lerp(int start, int stop, float amount) {
170         return lerp((float) start, (float) stop, amount);
171     }
172 
173     /**
174      * Returns the interpolation scalar (s) that satisfies the equation: {@code value = }{@link
175      * #lerp}{@code (a, b, s)}
176      *
177      * <p>If {@code a == b}, then this function will return 0.
178      */
lerpInv(float a, float b, float value)179     public static float lerpInv(float a, float b, float value) {
180         return a != b ? ((value - a) / (b - a)) : 0.0f;
181     }
182 
183     /** Returns the single argument constrained between [0.0, 1.0]. */
saturate(float value)184     public static float saturate(float value) {
185         return constrain(value, 0.0f, 1.0f);
186     }
187 
188     /** Returns the saturated (constrained between [0, 1]) result of {@link #lerpInv}. */
lerpInvSat(float a, float b, float value)189     public static float lerpInvSat(float a, float b, float value) {
190         return saturate(lerpInv(a, b, value));
191     }
192 
193     /**
194      * Returns an interpolated angle in degrees between a set of start and end
195      * angles.
196      * <p>
197      * Unlike {@link #lerp(float, float, float)}, the direction and distance of
198      * travel is determined by the shortest angle between the start and end
199      * angles. For example, if the starting angle is 0 and the ending angle is
200      * 350, then the interpolated angle will be in the range [0,-10] rather
201      * than [0,350].
202      *
203      * @param start the starting angle in degrees
204      * @param end the ending angle in degrees
205      * @param amount the position between start and end in the range [0,1]
206      *               where 0 is the starting angle and 1 is the ending angle
207      * @return the interpolated angle in degrees
208      */
lerpDeg(float start, float end, float amount)209     public static float lerpDeg(float start, float end, float amount) {
210         final float minAngle = (((end - start) + 180) % 360) - 180;
211         return minAngle * amount + start;
212     }
213 
norm(float start, float stop, float value)214     public static float norm(float start, float stop, float value) {
215         return (value - start) / (stop - start);
216     }
217 
map(float minStart, float minStop, float maxStart, float maxStop, float value)218     public static float map(float minStart, float minStop, float maxStart, float maxStop, float value) {
219         return maxStart + (maxStop - maxStart) * ((value - minStart) / (minStop - minStart));
220     }
221 
222     /**
223      * Calculates a value in [rangeMin, rangeMax] that maps value in [valueMin, valueMax] to
224      * returnVal in [rangeMin, rangeMax].
225      * <p>
226      * Always returns a constrained value in the range [rangeMin, rangeMax], even if value is
227      * outside [valueMin, valueMax].
228      * <p>
229      * Eg:
230      *    constrainedMap(0f, 100f, 0f, 1f, 0.5f) = 50f
231      *    constrainedMap(20f, 200f, 10f, 20f, 20f) = 200f
232      *    constrainedMap(20f, 200f, 10f, 20f, 50f) = 200f
233      *    constrainedMap(10f, 50f, 10f, 20f, 5f) = 10f
234      *
235      * @param rangeMin minimum of the range that should be returned.
236      * @param rangeMax maximum of the range that should be returned.
237      * @param valueMin minimum of range to map {@code value} to.
238      * @param valueMax maximum of range to map {@code value} to.
239      * @param value to map to the range [{@code valueMin}, {@code valueMax}]. Note, can be outside
240      *              this range, resulting in a clamped value.
241      * @return the mapped value, constrained to [{@code rangeMin}, {@code rangeMax}.
242      */
constrainedMap( float rangeMin, float rangeMax, float valueMin, float valueMax, float value)243     public static float constrainedMap(
244             float rangeMin, float rangeMax, float valueMin, float valueMax, float value) {
245         return lerp(rangeMin, rangeMax, lerpInvSat(valueMin, valueMax, value));
246     }
247 
248     /**
249      * Perform Hermite interpolation between two values.
250      * Eg:
251      *   smoothStep(0, 0.5f, 0.5f) = 1f
252      *   smoothStep(0, 0.5f, 0.25f) = 0.5f
253      *
254      * @param start Left edge.
255      * @param end Right edge.
256      * @param x A value between {@code start} and {@code end}.
257      * @return A number between 0 and 1 representing where {@code x} is in the interpolation.
258      */
smoothStep(float start, float end, float x)259     public static float smoothStep(float start, float end, float x) {
260         return constrain((x - start) / (end - start), 0f, 1f);
261     }
262 
263     /**
264      * Returns the sum of the two parameters, or throws an exception if the resulting sum would
265      * cause an overflow or underflow.
266      * @throws IllegalArgumentException when overflow or underflow would occur.
267      */
addOrThrow(int a, int b)268     public static int addOrThrow(int a, int b) throws IllegalArgumentException {
269         if (b == 0) {
270             return a;
271         }
272 
273         if (b > 0 && a <= (Integer.MAX_VALUE - b)) {
274             return a + b;
275         }
276 
277         if (b < 0 && a >= (Integer.MIN_VALUE - b)) {
278             return a + b;
279         }
280         throw new IllegalArgumentException("Addition overflow: " + a + " + " + b);
281     }
282 
283     /**
284      * Resize a {@link Rect} so one size would be {@param largestSide}.
285      *
286      * @param outToResize Rectangle that will be resized.
287      * @param largestSide Size of the largest side.
288      */
fitRect(Rect outToResize, int largestSide)289     public static void fitRect(Rect outToResize, int largestSide) {
290         if (outToResize.isEmpty()) {
291             return;
292         }
293         float maxSize = Math.max(outToResize.width(), outToResize.height());
294         outToResize.scale(largestSide / maxSize);
295     }
296 }
297