michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.util; michael@0: michael@0: import android.graphics.PointF; michael@0: michael@0: import java.lang.IllegalArgumentException; michael@0: michael@0: public final class FloatUtils { michael@0: private FloatUtils() {} michael@0: michael@0: public static boolean fuzzyEquals(float a, float b) { michael@0: return (Math.abs(a - b) < 1e-6); michael@0: } michael@0: michael@0: public static boolean fuzzyEquals(PointF a, PointF b) { michael@0: return fuzzyEquals(a.x, b.x) && fuzzyEquals(a.y, b.y); michael@0: } michael@0: michael@0: /* michael@0: * Returns the value that represents a linear transition between `from` and `to` at time `t`, michael@0: * which is on the scale [0, 1). Thus with t = 0.0f, this returns `from`; with t = 1.0f, this michael@0: * returns `to`; with t = 0.5f, this returns the value halfway from `from` to `to`. michael@0: */ michael@0: public static float interpolate(float from, float to, float t) { michael@0: return from + (to - from) * t; michael@0: } michael@0: michael@0: /** michael@0: * Returns 'value', clamped so that it isn't any lower than 'low', and it michael@0: * isn't any higher than 'high'. michael@0: */ michael@0: public static float clamp(float value, float low, float high) { michael@0: if (high < low) { michael@0: throw new IllegalArgumentException( michael@0: "clamp called with invalid parameters (" + high + " < " + low + ")" ); michael@0: } michael@0: return Math.max(low, Math.min(high, value)); michael@0: } michael@0: }