view flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/LinearRemap.java @ 447:5606ba4139e0

WSTs will now have a header that contains the names of the Ws or Qs that had been defined for the computation. flys-artifacts/trunk@1939 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 18 May 2011 08:04:54 +0000
parents 909196be11a0
children 343f248e4c8c
line wrap: on
line source
package de.intevation.flys.artifacts.math;

public class LinearRemap
{
    public static class Segment {

        protected Segment next;

        protected double from;
        protected double to;

        protected double m;
        protected double b;

        public Segment() {
        }

        public Segment(
            double from, double to, 
            double m,    double b,
            Segment next
        ) {
            this.from = from;
            this.to   = to;
            this.m    = m;
            this.b    = b;
        }

        public double eval(double x) {
            return m*x + b;
        }
    } // class Segment

    protected Segment head;

    public LinearRemap() {
    }

    public void add(
        double from, double to,
        double x1,   double y1,
        double x2,   double y2
    ) {
        // y1 = m*x1 + b <=> b = y1 - m*x1
        // y2 = m*x2 + b
        // y2 - y1 = m*(x2 - x1)
        // m = (y2 - y1)/(x2 - x1)

        double m, b;

        if (x2 == x1) {
            m = 0.0;
            b = 0.5*(y2 + y1);
        }
        else {
            m = (y2 - y1)/(x2 - x1);
            b = y1 - m*x1;
        }

        head = new Segment(from, to, m, b, head);
    }

    public double eval(double pos, double x) {
        Segment current = head;

        while (current != null) {

            if (pos >= current.from && pos <= current.to) {
                return current.eval(x);
            }

            current = current.next;
        }

        return Double.NaN;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org