Mercurial > dive4elements > gnv-client
diff gnv-artifacts/src/main/java/de/intevation/gnv/math/L1Comparator.java @ 1119:7c4f81f74c47
merged gnv-artifacts
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:00 +0200 |
parents | f953c9a559d8 |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/math/L1Comparator.java Fri Sep 28 12:14:00 2012 +0200 @@ -0,0 +1,78 @@ +/* + * 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 < 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 :