view gnv-artifacts/src/main/java/de/intevation/gnv/math/L1Comparator.java @ 873:0e458a4271c4

Created NEWS and Changes for the upcoming release. gnv-artifacts/trunk@1027 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 28 Apr 2010 15:09:22 +0000
parents a645bd23c1c8
children f953c9a559d8
line wrap: on
line source
package de.intevation.gnv.math;

import com.vividsolutions.jts.geom.Coordinate;

import java.util.Comparator;

/**
 * Compares two coordinates a and b by their L1(Manhattan) distance
 * relative to a reference point r.
 * da = L1(a, r)<br>
 * db = L1(b, r)<br>
 * -1 if da &lt; db, +1 if da &gt; db, 0 else.
 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
 */
public  class L1Comparator
implements    Comparator
{
    private Coordinate ref;

    /**
     * Default constructor.
     */
    public L1Comparator() {
    }

    /**
     * Constructor to create a L1Comparator with a given reference point.
     * @param ref The reference point.
     */
    public L1Comparator(Coordinate ref) {
        this.ref = ref;
    }

    /**
     * Explicitly sets the reference point.
     * @param ref The reference point.
     */
    public void setReference(Coordinate ref) {
        this.ref = ref;
    }

    /**
     * Compares to coordinate by their L1 distance to the reference point.
     * @param a The first coordinate.
     * @param b The second coordinate.
     * @return -1 if L1(a, ref) &lt; L1(b, ref),
     * +1 if L1(a, ref) &gt; L1(b, ref), 0 else.
     */
    public int compare(Object a, Object b) {
        Coordinate pa = (Coordinate)a;
        Coordinate pb = (Coordinate)b;
        double da = L1(ref, pa);
        double db = L1(ref, pb);
        if (da < db) return -1;
        if (da > db) return +1;
        return 0;
    }

    /**
     * Computes the L1 distance between two points a and b:<br>
     * L1(a, b) = abs(a.x - b.x) + abs(a.y - b.y)
     * @param a The first point.
     * @param b The second point.
     * @return The L1 distance.
     */
    public static double L1(Coordinate a, Coordinate b) {
        return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org