comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/L1Comparator.java @ 875:5e9efdda6894

merged gnv-artifacts/1.0
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:56 +0200 (2012-09-28)
parents a645bd23c1c8
children f953c9a559d8
comparison
equal deleted inserted replaced
722:bb3ffe7d719e 875:5e9efdda6894
1 package de.intevation.gnv.math;
2
3 import com.vividsolutions.jts.geom.Coordinate;
4
5 import java.util.Comparator;
6
7 /**
8 * Compares two coordinates a and b by their L1(Manhattan) distance
9 * relative to a reference point r.
10 * da = L1(a, r)<br>
11 * db = L1(b, r)<br>
12 * -1 if da &lt; db, +1 if da &gt; db, 0 else.
13 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
14 */
15 public class L1Comparator
16 implements Comparator
17 {
18 private Coordinate ref;
19
20 /**
21 * Default constructor.
22 */
23 public L1Comparator() {
24 }
25
26 /**
27 * Constructor to create a L1Comparator with a given reference point.
28 * @param ref The reference point.
29 */
30 public L1Comparator(Coordinate ref) {
31 this.ref = ref;
32 }
33
34 /**
35 * Explicitly sets the reference point.
36 * @param ref The reference point.
37 */
38 public void setReference(Coordinate ref) {
39 this.ref = ref;
40 }
41
42 /**
43 * Compares to coordinate by their L1 distance to the reference point.
44 * @param a The first coordinate.
45 * @param b The second coordinate.
46 * @return -1 if L1(a, ref) &lt; L1(b, ref),
47 * +1 if L1(a, ref) &gt; L1(b, ref), 0 else.
48 */
49 public int compare(Object a, Object b) {
50 Coordinate pa = (Coordinate)a;
51 Coordinate pb = (Coordinate)b;
52 double da = L1(ref, pa);
53 double db = L1(ref, pb);
54 if (da < db) return -1;
55 if (da > db) return +1;
56 return 0;
57 }
58
59 /**
60 * Computes the L1 distance between two points a and b:<br>
61 * L1(a, b) = abs(a.x - b.x) + abs(a.y - b.y)
62 * @param a The first point.
63 * @param b The second point.
64 * @return The L1 distance.
65 */
66 public static double L1(Coordinate a, Coordinate b) {
67 return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
68 }
69 }
70 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org