view flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/LinearRemap.java @ 637:f0c1250d1e7b

Make "Berechnungsart 4" work independent of flow direction. Commented out back jump correction temporarily. flys-artifacts/trunk@2013 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 26 May 2011 16:54:16 +0000
parents 343f248e4c8c
children
line wrap: on
line source
package de.intevation.flys.artifacts.math;

import org.apache.log4j.Logger;

public class LinearRemap
{
    private static Logger logger = Logger.getLogger(LinearRemap.class);

    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;
        }

        if (from > to) { double t = from; from = to; to = t; }

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

        if (logger.isDebugEnabled()) {
            logger.debug("LinearRemap.add --------- enter");
            logger.debug("  range: [" + from + ", " + to + "]");
            logger.debug("  " + x1 + " -> " + y1 + " (" + head.eval(x1) + ")");
            logger.debug("  " + x2 + " -> " + y2 + " (" + head.eval(x2) + ")");
            logger.debug("LinearRemap.add --------- leave");
        }
    }

    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