| 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 |
public final class FloatUtils { |
| 11 |
private FloatUtils() {} |
| 12 |
|
| 13 |
public static boolean fuzzyEquals(float a, float b) { |
| 14 |
return (Math.abs(a - b) < 1e-6); |
| 15 |
} |
| 16 |
|
| 17 |
public static boolean fuzzyEquals(PointF a, PointF b) { |
| 18 |
return fuzzyEquals(a.x, b.x) && fuzzyEquals(a.y, b.y); |
| 19 |
} |
| 20 |
|
| 21 |
/* |
| 22 |
* Returns the value that represents a linear transition between `from` and `to` at time `t`, |
| 23 |
* which is on the scale [0, 1). Thus with t = 0.0f, this returns `from`; with t = 1.0f, this |
| 24 |
* returns `to`; with t = 0.5f, this returns the value halfway from `from` to `to`. |
| 25 |
*/ |
| 26 |
public static float interpolate(float from, float to, float t) { |
| 27 |
return from + (to - from) * t; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Returns 'value', clamped so that it isn't any lower than 'low', and it |
| 32 |
* isn't any higher than 'high'. |
| 33 |
*/ |
| 34 |
public static float clamp(float value, float low, float high) { |
| 35 |
if (high < low) { |
| 36 |
throw new IllegalArgumentException( |
| 37 |
"clamp called with invalid parameters (" + high + " < " + low + ")" ); |
| 38 |
} |
| 39 |
return Math.max(low, Math.min(high, value)); |
| 40 |
} |
| 41 |
} |