view flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/LinearRemap.java @ 648:d4c4fefb095b

The matrix that is used to transform chart image coordinates into chart coordinates supports inverted x-axis now. flys-artifacts/trunk@2037 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 01 Jun 2011 09:18:00 +0000
parents f0c1250d1e7b
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