/* * __ .__ .__ ._____. * _/ |_ _______ __|__| ____ | | |__\_ |__ ______ * \ __\/ _ \ \/ / |/ ___\| | | || __ \ / ___/ * | | ( <_> > <| \ \___| |_| || \_\ \\___ \ * |__| \____/__/\_ \__|\___ >____/__||___ /____ > * \/ \/ \/ \/ * * Copyright (c) 2006-2011 Karsten Schmidt * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * http://creativecommons.org/licenses/LGPL/2.1/ * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ package toxi.util.datatypes; import java.util.LinkedList; import java.util.List; import java.util.Random; import toxi.math.MathUtils; /** * * @author tux */ public class FloatRange { /** * * @param samples * @return */ public static FloatRange fromSamples(float... samples) { float min = Float.MAX_VALUE; float max = Float.MIN_VALUE; for (float s : samples) { min = MathUtils.min(min, s); max = MathUtils.max(max, s); } return new FloatRange(min, max); } /** * * @param samples * @return */ public static FloatRange fromSamples(List samples) { float min = Float.MAX_VALUE; float max = Float.MIN_VALUE; for (float s : samples) { min = MathUtils.min(min, s); max = MathUtils.max(max, s); } return new FloatRange(min, max); } public float min, /** * */ /** * */ max; /** * */ public float currValue; /** * */ protected Random random = new Random(); /** * */ public FloatRange() { this(0f, 1f); } /** * * @param min * @param max */ public FloatRange(float min, float max) { // swap if necessary... if (min > max) { float t = max; max = min; min = t; } this.min = min; this.max = max; this.currValue = min; } /** * * @param val * @return */ public float adjustCurrentBy(float val) { return setCurrent(currValue + val); } /** * * @return */ public FloatRange copy() { FloatRange range = new FloatRange(min, max); range.currValue = currValue; range.random = random; return range; } /** * Returns the value at the normalized position (0.0 = min ... 1.0 = * max-EPS) within the range. Since the max value is exclusive, the * value returned for position 1.0 is the range max value minus * {@link MathUtils#EPS}. Also note the given position is not being clipped * to the 0.0-1.0 interval, so when passing in values outside that interval * will produce out-of-range values too. * * @param perc * @return value within the range */ public final float getAt(float perc) { return min + (max - min - MathUtils.EPS) * perc; } /** * * @return */ public float getCurrent() { return currValue; } /** * * @return */ public float getMedian() { return (min + max) * 0.5f; } /** * * @return */ public float getRange() { return max - min; } /** * * @param val * @return */ public boolean isValueInRange(float val) { return val >= min && val <= max; } /** * * @return */ public float pickRandom() { currValue = MathUtils.random(random, min, max); return currValue; } /** * * @param seed * @return */ public FloatRange seed(long seed) { random.setSeed(seed); return this; } /** * * @param val * @return */ public float setCurrent(float val) { currValue = MathUtils.clip(val, min, max); return currValue; } /** * * @param rnd * @return */ public FloatRange setRandom(Random rnd) { random = rnd; return this; } /** * * @param step * @return */ public Float[] toArray(float step) { List range = new LinkedList<>(); double v = min; while (v < max) { range.add((float) v); v += step; } return range.toArray(new Float[0]); } /** * * @return */ @Override public String toString() { return "FloatRange: " + min + " -> " + max; } }