OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.util; | 5 package org.chromium.chrome.browser.util; |
6 | 6 |
7 /** | 7 /** |
8 * Contains various math utilities used throughout Chrome Mobile. | 8 * Contains various math utilities used throughout Chrome Mobile. |
9 */ | 9 */ |
10 public class MathUtils { | 10 public class MathUtils { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 } | 66 } |
67 | 67 |
68 /** | 68 /** |
69 * Computes a%b that is positive. Note that result of % operation is not alw
ays positive. | 69 * Computes a%b that is positive. Note that result of % operation is not alw
ays positive. |
70 * @return a%b >= 0 ? a%b : a%b + b | 70 * @return a%b >= 0 ? a%b : a%b + b |
71 */ | 71 */ |
72 public static int positiveModulo(int a, int b) { | 72 public static int positiveModulo(int a, int b) { |
73 int mod = a % b; | 73 int mod = a % b; |
74 return mod >= 0 ? mod : mod + b; | 74 return mod >= 0 ? mod : mod + b; |
75 } | 75 } |
| 76 |
| 77 /** |
| 78 * Moves {@code value} forward to {@code target} based on {@code speed}. |
| 79 * @param value The current value. |
| 80 * @param target The target value. |
| 81 * @param speed How far to move {@code value} to {@code target}. 0 doesn't
move it at all. 1 |
| 82 * moves it to {@code target}. |
| 83 * @return The new interpolated value. |
| 84 */ |
| 85 public static float interpolate(float value, float target, float speed) { |
| 86 return (value + (target - value) * speed); |
| 87 } |
| 88 |
| 89 /** |
| 90 * Smooth a value between 0 and 1. |
| 91 * @param t The value to smooth. |
| 92 * @return The smoothed value between 0 and 1. |
| 93 */ |
| 94 public static float smoothstep(float t) { |
| 95 return t * t * (3.0f - 2.0f * t); |
| 96 } |
| 97 |
| 98 /** |
| 99 * Scales the provided dimension such that it is just large enough to fit |
| 100 * the target width and height. |
| 101 * |
| 102 * @param dimensions The dimensions to scale |
| 103 * @param targetWidth The target width |
| 104 * @param targetHeight The target height |
| 105 * @return The scale factor applied to dimensions |
| 106 */ |
| 107 public static float scaleToFitTargetSize( |
| 108 int [] dimensions, int targetWidth, int targetHeight) { |
| 109 if (dimensions.length < 2 || dimensions[0] <= 0 || dimensions[1] <= 0) { |
| 110 throw new IllegalArgumentException( |
| 111 "Expected dimensions to have length >= 2 && dimensions[0] >
0 && " |
| 112 + "dimensions[1] > 0"); |
| 113 } |
| 114 float scale = Math.max( |
| 115 (float) targetWidth / dimensions[0], |
| 116 (float) targetHeight / dimensions[1]); |
| 117 dimensions[0] *= scale; |
| 118 dimensions[1] *= scale; |
| 119 return scale; |
| 120 } |
| 121 |
| 122 /** |
| 123 * Flips {@code value} iff {@code flipSign} is {@code true}. |
| 124 * @param value The value to flip. |
| 125 * @param flipSign Whether or not to flip the value. |
| 126 * @return {@code value} iff {@code flipSign} is {@code false}, othe
rwise negative |
| 127 * {@code value}. |
| 128 */ |
| 129 public static int flipSignIf(int value, boolean flipSign) { |
| 130 return flipSign ? -value : value; |
| 131 } |
| 132 |
| 133 /** |
| 134 * Flips {@code value} iff {@code flipSign} is {@code true}. |
| 135 * @param value The value to flip. |
| 136 * @param flipSign Whether or not to flip the value. |
| 137 * @return {@code value} iff {@code flipSign} is {@code false}, othe
rwise negative |
| 138 * {@code value}. |
| 139 */ |
| 140 public static float flipSignIf(float value, boolean flipSign) { |
| 141 return flipSign ? -value : value; |
| 142 } |
76 } | 143 } |
OLD | NEW |