view gnv-artifacts/src/main/java/de/intevation/gnv/math/L1Comparator.java @ 1115:f953c9a559d8

Added license file and license headers. gnv-artifacts/trunk@1260 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:46:55 +0000
parents a645bd23c1c8
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

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