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