1 package com.cooliris.media;
4 * A static class for some useful operations on Floats and Vectors
7 public class FloatUtils {
8 private static final float ANIMATION_SPEED = 4.0f;
11 * This function animates a float value to another float value
14 * : The previous value (or the animated value)
18 * Time elapsed since the last time this function was called
19 * @return The new animated value that is closer to the target value and
20 * clamped to the target value
22 public static final float animate(float prevVal, float targetVal, float timeElapsed) {
23 timeElapsed = timeElapsed * ANIMATION_SPEED;
24 return animateAfterFactoringSpeed(prevVal, targetVal, timeElapsed);
28 * This function animates a Tuple3f value to another Tuple3f value
31 * : The animating Tuple
33 * : The target value for the Tuple
35 * : Time elapsed since the last time this function was called
37 public static final void animate(Vector3f animVal, Vector3f targetVal, float timeElapsed) {
38 timeElapsed = timeElapsed * ANIMATION_SPEED;
39 animVal.x = animateAfterFactoringSpeed(animVal.x, targetVal.x, timeElapsed);
40 animVal.y = animateAfterFactoringSpeed(animVal.y, targetVal.y, timeElapsed);
41 animVal.z = animateAfterFactoringSpeed(animVal.z, targetVal.z, timeElapsed);
45 * Clamp a float to a lower bound
48 * : the input float value
50 * : the minimum value to use to clamp
51 * @return the clamped value
53 public static final float clampMin(float val, float minVal) {
55 return minVal; // CR: braces
61 * Clamp a float to an upper bound
64 * : the input float value
66 * : the maximum value to use to clamp
67 * @return the clamped value
69 public static final float clampMax(float val, float maxVal) {
76 // CR: these comments are barely useful. they mostly just fill space. If
77 // anything, a one-liner would be sufficient.
79 * Clamp a float to a lower and upper bound
82 * : the input float value
84 * : the minimum value to use to clamp
86 * : the maximum value to use to clamp
87 * @return the clamped value
89 public static final float clamp(float val, float minVal, float maxVal) {
92 else if (val > maxVal)
99 * Clamp an integer to a lower and upper bound
102 * : the input float value
104 * : the minimum value to use to clamp
106 * : the maximum value to use to clamp
107 * @return the clamped value
109 public static final int clamp(int val, int minVal, int maxVal) {
112 else if (val > maxVal)
119 * Function to check whether a point lies inside a rectangle
122 * : the x coordinate of the left most point
124 * : the x coordinate of the right most point
126 * : the y coordinate of the top most point
128 * : the y coordinate of the bottom most point
130 * : the input point's x coordinate
132 * : the input point's y coordinate
133 * @return true if point is inside the rectangle else return false
135 public static final boolean boundsContainsPoint(float left, float right, float top, float bottom, float posX, float posY) {
136 // CR: return ... (one statement).
137 if (posX < left || posX > right || posY < top || posY > bottom)
143 private static final float animateAfterFactoringSpeed(float prevVal, float targetVal, float timeElapsed) {
144 if (prevVal == targetVal)
146 float newVal = prevVal + ((targetVal - prevVal) * timeElapsed);
147 if (Math.abs(newVal - prevVal) < 0.0001f)
149 if (newVal == prevVal) {
151 } else { // } else if (...) { ... }; no need for a new level of
153 if (prevVal > targetVal && newVal < targetVal) {
155 } else if (prevVal < targetVal && newVal > targetVal) {
163 public static final float max(float scaleX, float scaleY) {