mobile/android/base/util/FloatUtils.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/util/FloatUtils.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,43 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko.util;
    1.10 +
    1.11 +import android.graphics.PointF;
    1.12 +
    1.13 +import java.lang.IllegalArgumentException;
    1.14 +
    1.15 +public final class FloatUtils {
    1.16 +    private FloatUtils() {}
    1.17 +
    1.18 +    public static boolean fuzzyEquals(float a, float b) {
    1.19 +        return (Math.abs(a - b) < 1e-6);
    1.20 +    }
    1.21 +
    1.22 +    public static boolean fuzzyEquals(PointF a, PointF b) {
    1.23 +        return fuzzyEquals(a.x, b.x) && fuzzyEquals(a.y, b.y);
    1.24 +    }
    1.25 +
    1.26 +    /*
    1.27 +     * Returns the value that represents a linear transition between `from` and `to` at time `t`,
    1.28 +     * which is on the scale [0, 1). Thus with t = 0.0f, this returns `from`; with t = 1.0f, this
    1.29 +     * returns `to`; with t = 0.5f, this returns the value halfway from `from` to `to`.
    1.30 +     */
    1.31 +    public static float interpolate(float from, float to, float t) {
    1.32 +        return from + (to - from) * t;
    1.33 +    }
    1.34 +
    1.35 +    /**
    1.36 +     * Returns 'value', clamped so that it isn't any lower than 'low', and it
    1.37 +     * isn't any higher than 'high'.
    1.38 +     */
    1.39 +    public static float clamp(float value, float low, float high) {
    1.40 +        if (high < low) {
    1.41 +          throw new IllegalArgumentException(
    1.42 +              "clamp called with invalid parameters (" + high + " < " + low + ")" );
    1.43 +        }
    1.44 +        return Math.max(low, Math.min(high, value));
    1.45 +    }
    1.46 +}

mercurial