comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/Point2d.java @ 657:af3f56758f59

merged gnv-artifacts/0.5
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:53 +0200
parents b248531fa20b
children c4156275c1e1
comparison
equal deleted inserted replaced
590:5f5f273c8566 657:af3f56758f59
1 package de.intevation.gnv.math;
2
3 import com.vividsolutions.jts.geom.Coordinate;
4 import com.vividsolutions.jts.geom.Envelope;
5
6 import java.util.Comparator;
7
8 import org.apache.log4j.Logger;
9
10 /**
11 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
12 */
13 public class Point2d
14 extends Coordinate
15 {
16 private static Logger log = Logger.getLogger(Point2d.class);
17
18 public static final double EPSILON = 1e-3d;
19
20 public static final Comparator X_COMPARATOR = new Comparator() {
21 public int compare(Object a, Object b) {
22 double xa = ((Coordinate)a).x;
23 double xb = ((Coordinate)b).x;
24 if (xa < xb) return -1;
25 if (xa > xb) return +1;
26 return 0;
27 }
28 };
29
30 public static final Comparator Y_COMPARATOR = new Comparator() {
31 public int compare(Object a, Object b) {
32 double ya = ((Coordinate)a).y;
33 double yb = ((Coordinate)b).y;
34 if (ya < yb) return -1;
35 if (ya > yb) return +1;
36 return 0;
37 }
38 };
39
40 public static class InverseL1Comparator
41 implements Comparator
42 {
43 private Point2d ref;
44
45 public InverseL1Comparator(Point2d ref) {
46 this.ref = ref;
47 }
48
49 public int compare(Object a, Object b) {
50 Point2d pa = (Point2d)a;
51 Point2d pb = (Point2d)b;
52 double da = ref.L1(pa);
53 double db = ref.L1(pb);
54 if (da < db) return -1;
55 if (da > db) return +1;
56 return 0;
57 }
58 } // class InverseL1Comparator
59
60 public int i;
61 public int j;
62
63 public Point2d() {
64 }
65
66 public Point2d(double x, double y) {
67 super(x, y);
68 }
69
70 public Point2d(double x, double y, int i, int j) {
71 super(x, y);
72 this.i = i;
73 this.j = j;
74 }
75
76 public Point2d(double x, double y, double z, int i, int j) {
77 super(x, y, z);
78 this.i = i;
79 this.j = j;
80 }
81
82
83 public double L1(Point2d other) {
84 return L1(this, other);
85 }
86
87 public static double L1(Coordinate a, Coordinate b) {
88 return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
89 }
90
91 public Envelope envelope() {
92 return envelope(EPSILON);
93 }
94
95 public Envelope envelope(double epsilon) {
96 return new Envelope(
97 x-epsilon, x+epsilon,
98 y-epsilon, y+epsilon);
99 }
100
101 public boolean hasIGap(Point2d other) {
102 return Math.abs(i - other.i) > 1;
103 }
104
105 public boolean hasJGap(Point2d other) {
106 return Math.abs(j - other.j) > 1;
107 }
108
109 public Point2d extrapolate(double t, Point2d other) {
110 if (other == null) {
111 return null;
112 }
113 Point2d p = newPoint();
114 p.x = t*(other.x - x) + x;
115 p.y = t*(other.y - y) + y;
116 return p;
117 }
118
119 public Point2d newPoint() {
120 return new Point2d(0d, 0d);
121 }
122
123 public Point2d newPoint(double x, double y) {
124 return new Point2d(x, y);
125 }
126
127 public void inverseDistanceWeighting(Point2d [] ps) {
128
129 double sum = 0d;
130
131 double [] d = new double[ps.length];
132
133 for (int i = 0; i < ps.length; ++i) {
134 Point2d p = ps[i];
135 if (p != null) {
136 double di = distance(p);
137 if (di < 1e-5d) { z = p.z; return; }
138 di = 1d/di;
139 d[i] = di;
140 sum += di;
141 }
142 }
143
144 if (sum == 0d) {
145 return;
146 }
147
148 double v = 0d;
149
150 for (int i = 0; i < ps.length; ++i) {
151 Point2d p = ps[i];
152 if (p != null) {
153 v += p.z*d[i];
154 }
155 }
156 z = v/sum;
157 }
158
159 public static Point2d average(Point2d [] ps) {
160
161 Point2d p = null;
162 int count = 0;
163
164 for (int i = 0; i < ps.length; ++i) {
165 Point2d t = ps[i];
166 if (t != null) {
167 ++count;
168 if (p == null) {
169 p = t.newPoint(t.x, t.y);
170 }
171 else {
172 p.x += t.x;
173 p.y += t.y;
174 }
175 }
176 }
177
178 if (p != null) {
179 double s = 1d/count;
180 p.x *= s;
181 p.y *= s;
182 }
183
184 return p;
185 }
186
187 public boolean near(Point2d [] ps) {
188
189 for (int i = 0; i < ps.length; ++i) {
190 Point2d p = ps[i];
191 if (p != null && distance(p) > EPSILON) {
192 return false;
193 }
194 }
195
196 return true;
197 }
198 }
199 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org