Mercurial > dive4elements > gnv-client
view gnv-artifacts/src/main/java/de/intevation/gnv/math/L1Comparator.java @ 949:11d8cc2deb92 1.0
merged doc/1.0
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:58 +0200 |
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 < db, +1 if da > 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) < L1(b, ref), * +1 if L1(a, ref) > 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 :