Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/geom/Polygon2D.java @ 1798:552888e9c64a
Polygon2D: Handle start points when building polygons. Work in progress.
flys-artifacts/trunk@3121 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 31 Oct 2011 11:32:11 +0000 |
parents | 5eec623db50a |
children | 281b9430c720 |
comparison
equal
deleted
inserted
replaced
1797:5eec623db50a | 1798:552888e9c64a |
---|---|
10 import java.util.ArrayList; | 10 import java.util.ArrayList; |
11 import java.util.List; | 11 import java.util.List; |
12 import java.util.Arrays; | 12 import java.util.Arrays; |
13 import java.util.Comparator; | 13 import java.util.Comparator; |
14 import java.util.Collections; | 14 import java.util.Collections; |
15 | |
16 import de.intevation.flys.artifacts.math.Linear; | |
15 | 17 |
16 public class Polygon2D | 18 public class Polygon2D |
17 implements Serializable | 19 implements Serializable |
18 { | 20 { |
19 private static final double X(Point2D p) { | 21 private static final double X(Point2D p) { |
52 | 54 |
53 public void add(double x, double y) { | 55 public void add(double x, double y) { |
54 points.add(new Point2D.Double(x, y)); | 56 points.add(new Point2D.Double(x, y)); |
55 } | 57 } |
56 | 58 |
57 public boolean addCheck(Point2D p) { | 59 protected static boolean addCheck(Point2D p, List<Point2D> points) { |
58 switch (points.size()) { | 60 switch (points.size()) { |
59 case 0: | 61 case 0: |
60 points.add(p); | 62 points.add(p); |
61 return true; | 63 return true; |
62 case 1: | 64 case 1: |
80 } | 82 } |
81 return true; | 83 return true; |
82 } | 84 } |
83 } | 85 } |
84 | 86 |
87 public boolean addCheck(Point2D p) { | |
88 return addCheck(p, points); | |
89 } | |
90 | |
85 public void addReversed(List<Point2D> other) { | 91 public void addReversed(List<Point2D> other) { |
86 for (int i = other.size()-1; i >= 0; --i) { | 92 for (int i = other.size()-1; i >= 0; --i) { |
87 addCheck(other.get(i)); | 93 addCheck(other.get(i)); |
88 } | 94 } |
89 } | 95 } |
154 | 160 |
155 protected static boolean removeNoneIntersecting( | 161 protected static boolean removeNoneIntersecting( |
156 List<Point2D []> As, | 162 List<Point2D []> As, |
157 List<Point2D []> Bs | 163 List<Point2D []> Bs |
158 ) { | 164 ) { |
165 int B = Bs.size()-1; | |
159 OUTER: for (int i = 0; i < As.size();) { | 166 OUTER: for (int i = 0; i < As.size();) { |
160 Point2D [] a = As.get(i); | 167 Point2D [] a = As.get(i); |
161 int lo = 0, hi = Bs.size()-1; | 168 int lo = 0, hi = B; |
162 while (lo <= hi) { | 169 while (lo <= hi) { |
163 int mid = (lo + hi) >> 1; | 170 int mid = (lo + hi) >> 1; |
164 Point2D [] b = Bs.get(mid); | 171 Point2D [] b = Bs.get(mid); |
165 if (X(a[0]) > X(b[b.length-1])) lo = mid+1; | 172 if (X(a[0]) > X(b[b.length-1])) lo = mid+1; |
166 else if (X(a[a.length-1]) < X(b[0])) hi = mid-1; | 173 else if (X(a[a.length-1]) < X(b[0])) hi = mid-1; |
182 Point2D [] bs, | 189 Point2D [] bs, |
183 Point2D [][] rest, | 190 Point2D [][] rest, |
184 List<Polygon2D> positives, | 191 List<Polygon2D> positives, |
185 List<Polygon2D> negatives | 192 List<Polygon2D> negatives |
186 ) { | 193 ) { |
194 List<Point2D> apoints = new ArrayList<Point2D>(); | |
195 List<Point2D> bpoints = new ArrayList<Point2D>(); | |
196 | |
197 double ax = X(as[0]); | |
198 double bx = X(bs[0]); | |
199 | |
200 int ai = 0; | |
201 int bi = 0; | |
202 | |
203 if (ax == bx) { | |
204 apoints.add(as[0]); | |
205 bpoints.add(bs[0]); | |
206 } | |
207 else if (ax > bx) { | |
208 apoints.add(as[0]); | |
209 bi = 1; // exists because of overlap | |
210 double bx1; | |
211 while ((bx1 = X(bs[bi])) < ax) ++bi; | |
212 if (bx1 == ax) { | |
213 bpoints.add(bs[bi]); | |
214 } | |
215 else { // need to calculate start b point. | |
216 double by1 = Linear.linear( | |
217 ax, | |
218 X(bs[bi-1]), bx1, | |
219 Y(bs[bi-1]), Y(bs[bi])); | |
220 | |
221 bpoints.add(new Point2D.Double(ax, by1)); | |
222 } | |
223 } | |
224 else { // bx > ax: Symmetric case | |
225 bpoints.add(bs[0]); | |
226 ai = 1; // exists because of overlap | |
227 double ax1; | |
228 while ((ax1 = X(as[ai])) < bx) ++ai; | |
229 if (ax1 == bx) { | |
230 apoints.add(as[ai]); | |
231 } | |
232 else { // need to calculate start b point. | |
233 double ay1 = Linear.linear( | |
234 bx, | |
235 X(as[ai-1]), ax1, | |
236 Y(as[ai-1]), Y(as[ai])); | |
237 | |
238 apoints.add(new Point2D.Double(bx, ay1)); | |
239 } | |
240 } | |
241 | |
242 // now we have a point in each list, decide if neg/pos. | |
243 boolean neg = Y(bpoints.get(0)) > Y(apoints.get(0)); | |
244 | |
245 // TODO: Continue with inner points | |
187 } | 246 } |
188 | 247 |
189 public static void createPolygons( | 248 public static void createPolygons( |
190 double [] xAs, double [] yAs, | 249 double [] xAs, double [] yAs, |
191 double [] xBs, double [] yBs, | 250 double [] xBs, double [] yBs, |