comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/Point2d.java @ 798:6cff63d0c434

Fixed vim modeline. Added some Javadoc. gnv-artifacts/trunk@880 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 06 Apr 2010 11:05:00 +0000
parents c4156275c1e1
children f953c9a559d8
comparison
equal deleted inserted replaced
797:9d2891068ba5 798:6cff63d0c434
6 import java.util.Comparator; 6 import java.util.Comparator;
7 7
8 import org.apache.log4j.Logger; 8 import org.apache.log4j.Logger;
9 9
10 /** 10 /**
11 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> 11 * Point which besides the x, y, z coodinates has an index i, j pair
12 * to model neighborhood.
13 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
12 */ 14 */
13 public class Point2d 15 public class Point2d
14 extends Coordinate 16 extends Coordinate
15 { 17 {
16 private static Logger log = Logger.getLogger(Point2d.class); 18 private static Logger log = Logger.getLogger(Point2d.class);
17 19
20 /**
21 * Numerical tolerance epsilon: {@value}
22 */
18 public static final double EPSILON = 1e-3d; 23 public static final double EPSILON = 1e-3d;
19 24
25 /**
26 * Compares two Point2ds by their x coordinates
27 */
20 public static final Comparator X_COMPARATOR = new Comparator() { 28 public static final Comparator X_COMPARATOR = new Comparator() {
21 public int compare(Object a, Object b) { 29 public int compare(Object a, Object b) {
22 double xa = ((Coordinate)a).x; 30 double xa = ((Coordinate)a).x;
23 double xb = ((Coordinate)b).x; 31 double xb = ((Coordinate)b).x;
24 if (xa < xb) return -1; 32 if (xa < xb) return -1;
25 if (xa > xb) return +1; 33 if (xa > xb) return +1;
26 return 0; 34 return 0;
27 } 35 }
28 }; 36 };
29 37
38 /**
39 * Compares two Point2ds by their y coordinates.
40 */
30 public static final Comparator Y_COMPARATOR = new Comparator() { 41 public static final Comparator Y_COMPARATOR = new Comparator() {
31 public int compare(Object a, Object b) { 42 public int compare(Object a, Object b) {
32 double ya = ((Coordinate)a).y; 43 double ya = ((Coordinate)a).y;
33 double yb = ((Coordinate)b).y; 44 double yb = ((Coordinate)b).y;
34 if (ya < yb) return -1; 45 if (ya < yb) return -1;
35 if (ya > yb) return +1; 46 if (ya > yb) return +1;
36 return 0; 47 return 0;
37 } 48 }
38 }; 49 };
39 50
40 public static class InverseL1Comparator 51 /**
41 implements Comparator 52 * The i index of this Point2d.
42 { 53 */
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; 54 public int i;
55 /**
56 * The j index of this Point2d.
57 */
61 public int j; 58 public int j;
62 59
60 /**
61 * Default constructor.
62 */
63 public Point2d() { 63 public Point2d() {
64 } 64 }
65 65
66 /**
67 * Constructor setting x and y coordinate.
68 * @param x The x coordinate.
69 * @param y The y coordinate.
70 */
66 public Point2d(double x, double y) { 71 public Point2d(double x, double y) {
67 super(x, y); 72 super(x, y);
68 } 73 }
69 74
75 /**
76 * Constructor setting x, y and i, j.
77 * @param x The x coordinate.
78 * @param y The y coordinate.
79 * @param i The i index.
80 * @param j The j index.
81 */
70 public Point2d(double x, double y, int i, int j) { 82 public Point2d(double x, double y, int i, int j) {
71 super(x, y); 83 super(x, y);
72 this.i = i; 84 this.i = i;
73 this.j = j; 85 this.j = j;
74 } 86 }
75 87
88 /**
89 * Constructor setting x, y, z and i, j.
90 * @param x The x coordinate.
91 * @param y The y coordinate.
92 * @param z The z coordinate.
93 * @param i The i index.
94 * @param j The j index.
95 */
76 public Point2d(double x, double y, double z, int i, int j) { 96 public Point2d(double x, double y, double z, int i, int j) {
77 super(x, y, z); 97 super(x, y, z);
78 this.i = i; 98 this.i = i;
79 this.j = j; 99 this.j = j;
80 } 100 }
81 101
82 102 /**
103 * Calculates the L1 distance to another Point2d.
104 * @param other The other Point2d.
105 * @return The L1 distance.
106 */
83 public double L1(Point2d other) { 107 public double L1(Point2d other) {
84 return L1(this, other); 108 return L1Comparator.L1(this, other);
85 } 109 }
86 110
87 public static double L1(Coordinate a, Coordinate b) { 111 /**
88 return Math.abs(a.x - b.x) + Math.abs(a.y - b.y); 112 * Creates an envelope around this Point2d with
89 } 113 * the numerical tolerance of {@link #EPSILON}.
90 114 * @return The envelope.
115 */
91 public Envelope envelope() { 116 public Envelope envelope() {
92 return envelope(EPSILON); 117 return envelope(EPSILON);
93 } 118 }
94 119
120 /**
121 *Creates an envelope around this Point2d with
122 * a given tolerance.
123 * @param epsilon The tolerance in all directions.
124 * @return The envelope.
125 */
95 public Envelope envelope(double epsilon) { 126 public Envelope envelope(double epsilon) {
96 return new Envelope( 127 return new Envelope(
97 x-epsilon, x+epsilon, 128 x-epsilon, x+epsilon,
98 y-epsilon, y+epsilon); 129 y-epsilon, y+epsilon);
99 } 130 }
100 131
132 /**
133 * Given this and another Point2d it looks if there is
134 * a gap between the in points in i index direction.
135 * @param other The other Point2d.
136 * @return true if there is is a gap, else false.
137 */
101 public boolean hasIGap(Point2d other) { 138 public boolean hasIGap(Point2d other) {
102 return Math.abs(i - other.i) > 1; 139 return Math.abs(i - other.i) > 1;
103 } 140 }
104 141
142 /**
143 * Given this and another Point2d it looks if there is
144 * a gap between the in points in j index direction.
145 * @param other The other Point2d.
146 * @return true if there is is a gap, else false.
147 */
105 public boolean hasJGap(Point2d other) { 148 public boolean hasJGap(Point2d other) {
106 return Math.abs(j - other.j) > 1; 149 return Math.abs(j - other.j) > 1;
107 } 150 }
108 151
152 /**
153 * Given this and another Point2d a new Point2d is
154 * created via {@link #newPoint() }. The x, y coordinate
155 * of the new point is on the line of this and the other
156 * given point at a given scaling point.
157 * @param t The scaling factor.
158 * @param other The other point.
159 * @return The new Point2d.
160 */
109 public Point2d extrapolate(double t, Point2d other) { 161 public Point2d extrapolate(double t, Point2d other) {
110 if (other == null) { 162 if (other == null) {
111 return null; 163 return null;
112 } 164 }
113 Point2d p = newPoint(); 165 Point2d p = newPoint();
114 p.x = t*(other.x - x) + x; 166 p.x = t*(other.x - x) + x;
115 p.y = t*(other.y - y) + y; 167 p.y = t*(other.y - y) + y;
116 return p; 168 return p;
117 } 169 }
118 170
171 /**
172 * Creates a new Point2d or an instance of a subclass.
173 * Override this in subclasses.
174 * @return The new Point2d.
175 */
119 public Point2d newPoint() { 176 public Point2d newPoint() {
120 return new Point2d(0d, 0d); 177 return new Point2d(0d, 0d);
121 } 178 }
122 179
180 /**
181 * Creates a new Point2d or an instance of a subclass
182 * at a given coordinate.
183 * Override this in subclasses.
184 * @param x The x coordinate.
185 * @param y The y coordinate.
186 * @return The new point.
187 */
123 public Point2d newPoint(double x, double y) { 188 public Point2d newPoint(double x, double y) {
124 return new Point2d(x, y); 189 return new Point2d(x, y);
125 } 190 }
126 191
192 /**
193 * Sets the z value to the inverse distance weighting (IDW) value
194 * of the z values of a set of given points.
195 * @param ps The points from wich the z values are taken
196 * to calculate the IDW.
197 */
127 public void inverseDistanceWeighting(Point2d [] ps) { 198 public void inverseDistanceWeighting(Point2d [] ps) {
128 199
129 double sum = 0d; 200 double sum = 0d;
130 201
131 double [] d = new double[ps.length]; 202 double [] d = new double[ps.length];
154 } 225 }
155 } 226 }
156 z = v/sum; 227 z = v/sum;
157 } 228 }
158 229
230 /**
231 * Creates a new point via {@link #newPoint() } with the
232 * x,y coordinates of the center of a given set of
233 * coordinates.
234 * @param ps The points from which the x,y coordinates are
235 * taken to calculate the center.
236 * @return The new center point.
237 */
159 public static Point2d average(Point2d [] ps) { 238 public static Point2d average(Point2d [] ps) {
160 239
161 Point2d p = null; 240 Point2d p = null;
162 int count = 0; 241 int count = 0;
163 242
182 } 261 }
183 262
184 return p; 263 return p;
185 } 264 }
186 265
266 /**
267 * Checks if this Point2d is near to at least one point
268 * out of a given set of points. Near is defined by an
269 * euclidian distance small than {@link #EPSILON}.
270 * @param ps The set of points to be tested.
271 * @return true if this Point2d is near to one of the given
272 * points, else false.
273 */
187 public boolean near(Point2d [] ps) { 274 public boolean near(Point2d [] ps) {
188 275
189 for (int i = 0; i < ps.length; ++i) { 276 for (int i = 0; i < ps.length; ++i) {
190 Point2d p = ps[i]; 277 Point2d p = ps[i];
191 if (p != null && distance(p) > EPSILON) { 278 if (p != null && distance(p) > EPSILON) {
194 } 281 }
195 282
196 return true; 283 return true;
197 } 284 }
198 } 285 }
199 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: 286 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org