comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/Point2d.java @ 593:b248531fa20b

Added experimental support for extrapolation in "Horizontalschnitte" gnv-artifacts/trunk@648 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 31 Jan 2010 21:50:15 +0000
parents 422275fc9927
children c4156275c1e1
comparison
equal deleted inserted replaced
592:3c939c95c477 593:b248531fa20b
1 package de.intevation.gnv.math; 1 package de.intevation.gnv.math;
2
3 import com.vividsolutions.jts.geom.Coordinate;
4 import com.vividsolutions.jts.geom.Envelope;
2 5
3 import java.util.Comparator; 6 import java.util.Comparator;
4 7
5 import com.vividsolutions.jts.geom.Envelope; 8 import org.apache.log4j.Logger;
6 import com.vividsolutions.jts.geom.Coordinate;
7 9
8 /** 10 /**
9 * @author Sascha L. Teichmann 11 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
10 */ 12 */
11 public class Point2d 13 public class Point2d
12 extends Coordinate 14 extends Coordinate
13 { 15 {
16 private static Logger log = Logger.getLogger(Point2d.class);
17
14 public static final double EPSILON = 1e-3d; 18 public static final double EPSILON = 1e-3d;
15 19
16 public static final Comparator X_COMPARATOR = new Comparator() { 20 public static final Comparator X_COMPARATOR = new Comparator() {
17 public int compare(Object a, Object b) { 21 public int compare(Object a, Object b) {
18 double xa = ((Coordinate)a).x; 22 double xa = ((Coordinate)a).x;
57 public int j; 61 public int j;
58 62
59 public Point2d() { 63 public Point2d() {
60 } 64 }
61 65
66 public Point2d(double x, double y) {
67 super(x, y);
68 }
69
62 public Point2d(double x, double y, int i, int j) { 70 public Point2d(double x, double y, int i, int j) {
63 super(x, y); 71 super(x, y);
64 this.i = i; 72 this.i = i;
65 this.j = j; 73 this.j = j;
66 } 74 }
95 } 103 }
96 104
97 public boolean hasJGap(Point2d other) { 105 public boolean hasJGap(Point2d other) {
98 return Math.abs(j - other.j) > 1; 106 return Math.abs(j - other.j) > 1;
99 } 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 }
100 } 198 }
101 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: 199 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org