|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.util; |
|
7 |
|
8 import android.graphics.PointF; |
|
9 |
|
10 import java.lang.IllegalArgumentException; |
|
11 |
|
12 public final class FloatUtils { |
|
13 private FloatUtils() {} |
|
14 |
|
15 public static boolean fuzzyEquals(float a, float b) { |
|
16 return (Math.abs(a - b) < 1e-6); |
|
17 } |
|
18 |
|
19 public static boolean fuzzyEquals(PointF a, PointF b) { |
|
20 return fuzzyEquals(a.x, b.x) && fuzzyEquals(a.y, b.y); |
|
21 } |
|
22 |
|
23 /* |
|
24 * Returns the value that represents a linear transition between `from` and `to` at time `t`, |
|
25 * which is on the scale [0, 1). Thus with t = 0.0f, this returns `from`; with t = 1.0f, this |
|
26 * returns `to`; with t = 0.5f, this returns the value halfway from `from` to `to`. |
|
27 */ |
|
28 public static float interpolate(float from, float to, float t) { |
|
29 return from + (to - from) * t; |
|
30 } |
|
31 |
|
32 /** |
|
33 * Returns 'value', clamped so that it isn't any lower than 'low', and it |
|
34 * isn't any higher than 'high'. |
|
35 */ |
|
36 public static float clamp(float value, float low, float high) { |
|
37 if (high < low) { |
|
38 throw new IllegalArgumentException( |
|
39 "clamp called with invalid parameters (" + high + " < " + low + ")" ); |
|
40 } |
|
41 return Math.max(low, Math.min(high, value)); |
|
42 } |
|
43 } |