Sha256: f8ce0c8559b91bbcddcc1aa3633a5c138611b80df4672efbf69cf4f67920728d

Contents?: true

Size: 1.89 KB

Versions: 11

Compression:

Stored size: 1.89 KB

Contents

package toxi.math;

import java.util.NavigableSet;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 *
 * @author tux
 */
public class NonLinearScaleMap {

    /**
     *
     */
    public class Sample implements Comparable<Sample> {

        public final double x,

        /**
         *
         */
        y;

        /**
         *
         * @param x
         * @param y
         */
        public Sample(double x, double y) {
            this.x = x;
            this.y = y;
        }

        /**
         *
         * @param b
         * @return
         */
        @Override
        public int compareTo(Sample b) {
            return (int) Math.signum(x - b.x);
        }
    }

    private final TreeSet<Sample> samples;

    private double rangeMin = Float.MAX_VALUE;
    private double rangeMax = Float.MIN_VALUE;

    /**
     *
     */
    public NonLinearScaleMap() {
        samples = new TreeSet<>();
    }

    /**
     *
     * @param x
     * @param y
     * @return
     */
    public NonLinearScaleMap addSample(double x, double y) {
        samples.add(new Sample(x, y));
        rangeMin = MathUtils.min(y, rangeMin);
        rangeMax = MathUtils.max(y, rangeMax);
        return this;
    }

    /**
     *
     * @return
     */
    public NavigableSet<Sample> getSamples() {
        return samples;
    }

    /**
     *
     * @param x
     * @return
     */
    public double map(double x) {
        Sample t = new Sample(x, 0);
        SortedSet<Sample> aset = samples.headSet(t);
        SortedSet<Sample> bset = samples.tailSet(t);
        if (aset.isEmpty()) {
            return bset.first().y;
        } else {
            if (bset.isEmpty()) {
                return aset.last().y;
            } else {
                Sample a = aset.last();
                Sample b = bset.first();
                return MathUtils.lerp(a.y, b.y, (x - a.x) / (b.x - a.x));
            }
        }
    }

}

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
toxiclibs-2.1.0 src/main/java/toxi/math/NonLinearScaleMap.java
toxiclibs-2.0.0 src/main/java/toxi/math/NonLinearScaleMap.java
toxiclibs-1.0.0 src/toxi/math/NonLinearScaleMap.java
toxiclibs-0.9.3 src/toxi/math/NonLinearScaleMap.java
toxiclibs-0.9.2 src/toxi/math/NonLinearScaleMap.java
toxiclibs-0.9.1 src/toxi/math/NonLinearScaleMap.java
toxiclibs-0.9.0 src/toxi/math/NonLinearScaleMap.java
toxiclibs-0.8.0 src/toxi/math/NonLinearScaleMap.java
toxiclibs-0.6.0-java src/toxi/math/NonLinearScaleMap.java
toxiclibs-0.5.1-java src/toxi/math/NonLinearScaleMap.java
toxiclibs-0.5.0-java src/toxi/math/NonLinearScaleMap.java