Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/L1Comparator.java @ 805:bb7afd783321
Removed trailing whitespace. Added more javadoc.
gnv-artifacts/trunk@887 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 08 Apr 2010 11:31:44 +0000 |
parents | 6cff63d0c434 |
children | a645bd23c1c8 |
comparison
equal
deleted
inserted
replaced
804:9058c08eac3a | 805:bb7afd783321 |
---|---|
3 import com.vividsolutions.jts.geom.Coordinate; | 3 import com.vividsolutions.jts.geom.Coordinate; |
4 | 4 |
5 import java.util.Comparator; | 5 import java.util.Comparator; |
6 | 6 |
7 /** | 7 /** |
8 * Compares two coordinates a and b by their L1(Matnhattan) distance | |
9 * relative to a reference point r. | |
10 * da = L1(a, r)<br> | |
11 * db = L1(b, r)<br> | |
12 * -1 if da < db, +1 if da > db, 0 else. | |
8 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> | 13 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> |
9 */ | 14 */ |
10 public class L1Comparator | 15 public class L1Comparator |
11 implements Comparator | 16 implements Comparator |
12 { | 17 { |
13 private Coordinate ref; | 18 private Coordinate ref; |
14 | 19 |
20 /** | |
21 * Default constructor. | |
22 */ | |
15 public L1Comparator() { | 23 public L1Comparator() { |
16 } | 24 } |
17 | 25 |
26 /** | |
27 * Constructor to create a L1Comparator with a given reference point. | |
28 * @param ref The reference point. | |
29 */ | |
18 public L1Comparator(Coordinate ref) { | 30 public L1Comparator(Coordinate ref) { |
19 this.ref = ref; | 31 this.ref = ref; |
20 } | 32 } |
21 | 33 |
34 /** | |
35 * Explicitly sets the reference point. | |
36 * @param ref The reference point. | |
37 */ | |
22 public void setReference(Coordinate ref) { | 38 public void setReference(Coordinate ref) { |
23 this.ref = ref; | 39 this.ref = ref; |
24 } | 40 } |
25 | 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) < L1(b, ref), | |
47 * +1 if L1(a, ref) > L1(b, ref), 0 else. | |
48 */ | |
26 public int compare(Object a, Object b) { | 49 public int compare(Object a, Object b) { |
27 Coordinate pa = (Coordinate)a; | 50 Coordinate pa = (Coordinate)a; |
28 Coordinate pb = (Coordinate)b; | 51 Coordinate pb = (Coordinate)b; |
29 double da = L1(ref, pa); | 52 double da = L1(ref, pa); |
30 double db = L1(ref, pb); | 53 double db = L1(ref, pb); |
31 if (da < db) return -1; | 54 if (da < db) return -1; |
32 if (da > db) return +1; | 55 if (da > db) return +1; |
33 return 0; | 56 return 0; |
34 } | 57 } |
35 | 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 */ | |
36 public static double L1(Coordinate a, Coordinate b) { | 66 public static double L1(Coordinate a, Coordinate b) { |
37 return Math.abs(a.x - b.x) + Math.abs(a.y - b.y); | 67 return Math.abs(a.x - b.x) + Math.abs(a.y - b.y); |
38 } | 68 } |
39 | |
40 } | 69 } |
41 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 70 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |