OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.base; | |
6 | |
7 /** | |
8 * Contains various math utilities used throughout Android. | |
9 */ | |
10 public class AndroidMathUtils { | |
Yaron
2015/03/06 22:57:08
There's already a MathUtils class in org.chromium.
David Trainor- moved to gerrit
2015/03/09 20:02:42
Done.
| |
11 | |
12 private AndroidMathUtils() {} | |
13 | |
14 /** | |
15 * Moves {@code value} forward to {@code target} based on {@code speed}. | |
16 * @param value The current value. | |
17 * @param target The target value. | |
18 * @param speed How far to move {@code value} to {@code target}. 0 doesn't move it at all. 1 | |
19 * moves it to {@code target}. | |
20 * @return The new interpolated value. | |
21 */ | |
22 public static float interpolate(float value, float target, float speed) { | |
23 return (value + (target - value) * speed); | |
24 } | |
25 | |
26 /** | |
27 * Smooth a value between 0 and 1. | |
28 * @param t The value to smooth. | |
29 * @return The smoothed value between 0 and 1. | |
30 */ | |
31 public static float smoothstep(float t) { | |
32 return t * t * (3.0f - 2.0f * t); | |
33 } | |
34 | |
35 /** | |
36 * Scales the provided dimension such that it is just large enough to fit | |
37 * the target width and height. | |
38 * | |
39 * @param dimensions The dimensions to scale | |
40 * @param targetWidth The target width | |
41 * @param targetHeight The target height | |
42 * @return The scale factor applied to dimensions | |
43 */ | |
44 public static float scaleToFitTargetSize( | |
45 int [] dimensions, int targetWidth, int targetHeight) { | |
46 if (dimensions.length < 2 || dimensions[0] <= 0 || dimensions[1] <= 0) { | |
47 throw new IllegalArgumentException( | |
48 "Expected dimensions to have length >= 2 && dimensions[0] > 0 && " | |
49 + "dimensions[1] > 0"); | |
50 } | |
51 float scale = Math.max( | |
52 (float) targetWidth / dimensions[0], | |
53 (float) targetHeight / dimensions[1]); | |
54 dimensions[0] *= scale; | |
55 dimensions[1] *= scale; | |
56 return scale; | |
57 } | |
58 | |
59 /** | |
60 * Flips {@code value} iff {@code flipSign} is {@code true}. | |
61 * @param value The value to flip. | |
62 * @param flipSign Whether or not to flip the value. | |
63 * @return {@code value} iff {@code flipSign} is {@code false}, othe rwise negative | |
64 * {@code value}. | |
65 */ | |
66 public static int flipSignIf(int value, boolean flipSign) { | |
67 return flipSign ? -value : value; | |
68 } | |
69 | |
70 /** | |
71 * Flips {@code value} iff {@code flipSign} is {@code true}. | |
72 * @param value The value to flip. | |
73 * @param flipSign Whether or not to flip the value. | |
74 * @return {@code value} iff {@code flipSign} is {@code false}, othe rwise negative | |
75 * {@code value}. | |
76 */ | |
77 public static float flipSignIf(float value, boolean flipSign) { | |
78 return flipSign ? -value : value; | |
79 } | |
80 } | |
OLD | NEW |