1
2
3
4
5 package math.geom2d.polygon;
6
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.Iterator;
10
11 import math.geom2d.Angle2D;
12 import math.geom2d.Point2D;
13 import math.geom2d.conic.Circle2D;
14 import math.geom2d.conic.CircleArc2D;
15 import math.geom2d.domain.BoundaryPolyCurve2D;
16 import math.geom2d.domain.PolyOrientedCurve2D;
17 import math.geom2d.domain.SmoothOrientedCurve2D;
18 import math.geom2d.line.LineSegment2D;
19 import math.geom2d.line.StraightLine2D;
20
21
22
23
24
25
26
27 public abstract class Polyline2DUtils {
28
29
30
31
32
33
34
35
36
37
38 public final static PolyOrientedCurve2D<SmoothOrientedCurve2D>
39 createParallel(Polyline2D polyline, double d) {
40
41
42 PolyOrientedCurve2D<SmoothOrientedCurve2D> result =
43 new PolyOrientedCurve2D<SmoothOrientedCurve2D>();
44 result.setClosed(polyline instanceof LinearRing2D);
45
46
47 if (polyline.getVertices().size()<2)
48 return result;
49
50
51
52
53 Point2D v1, v2;
54
55
56
57 Point2D p1, p2, p0 = null;
58
59
60 StraightLine2D line0, line;
61
62
63 Circle2D circle;
64
65 Iterator<Point2D> iterator;
66
67
68
69 if (polyline instanceof LinearRing2D) {
70
71
72
73 LineSegment2D lastEdge = polyline.getLastEdge();
74 line0 = StraightLine2D.createParallel(lastEdge, d);
75
76 v2 = lastEdge.getLastPoint();
77 p0 = line0.getProjectedPoint(v2);
78
79
80 iterator = polyline.getVertices().iterator();
81 v1 = iterator.next();
82 v2 = iterator.next();
83 line = new StraightLine2D(v1, v2).getParallel(d);
84
85
86 p1 = line.getProjectedPoint(v1);
87 if (Angle2D.getAngle(line0, line)>Math.PI^d<0) {
88
89
90 p1 = line.getIntersection(line0);
91 p0 = p1;
92 } else {
93
94 circle = new Circle2D(v1, Math.abs(d));
95 result.addCurve(new CircleArc2D(v1, Math.abs(d), circle
96 .getPosition(p0), circle.getPosition(p1), d>0));
97 }
98
99 p2 = line.getProjectedPoint(v2);
100 line0 = line;
101 } else {
102
103 iterator = polyline.getVertices().iterator();
104 v1 = iterator.next();
105 v2 = iterator.next();
106
107
108 line0 = new StraightLine2D(v1, v2).getParallel(d);
109 p1 = line0.getProjectedPoint(v1);
110 p2 = line0.getProjectedPoint(v2);
111 }
112
113
114
115
116 while (iterator.hasNext()) {
117
118 v1 = v2;
119 v2 = iterator.next();
120 line = new StraightLine2D(v1, v2).getParallel(d);
121
122
123 if (Angle2D.getAngle(line0, line)>Math.PI^d<0) {
124
125
126 p2 = line.getIntersection(line0);
127
128 result.addCurve(new LineSegment2D(p1, p2));
129 p1 = p2;
130 } else {
131
132
133 result.addCurve(new LineSegment2D(p1, p2));
134 p1 = line.getProjectedPoint(v1);
135 circle = new Circle2D(v1, Math.abs(d));
136 result.addCurve(new CircleArc2D(v1, Math.abs(d), circle
137 .getPosition(p2), circle.getPosition(p1), d>0));
138 }
139
140
141 p2 = line.getProjectedPoint(v2);
142 line0 = line;
143 }
144
145
146
147 if (polyline instanceof LinearRing2D) {
148
149 iterator = polyline.getVertices().iterator();
150 v1 = v2;
151 v2 = iterator.next();
152 line = new StraightLine2D(v1, v2).getParallel(d);
153
154
155 if (Angle2D.getAngle(line0, line)>Math.PI^d<0) {
156
157
158 p2 = line.getIntersection(line0);
159
160 result.addCurve(new LineSegment2D(p1, p2));
161 p1 = p2;
162 } else {
163
164
165 result.addCurve(new LineSegment2D(p1, p2));
166 p1 = line.getProjectedPoint(v1);
167 circle = new Circle2D(v1, Math.abs(d));
168 result.addCurve(new CircleArc2D(v1, Math.abs(d), circle
169 .getPosition(p2), circle.getPosition(p1), d>0));
170 }
171
172
173 result.addCurve(new LineSegment2D(p1, p0));
174 } else {
175
176 result.addCurve(new LineSegment2D(p1, p2));
177 }
178
179
180 return result;
181 }
182
183
184
185
186
187
188
189
190
191
192 public final static BoundaryPolyCurve2D<SmoothOrientedCurve2D>
193 createClosedParallel(LinearRing2D polyline, double d) {
194
195
196 BoundaryPolyCurve2D<SmoothOrientedCurve2D> result =
197 new BoundaryPolyCurve2D<SmoothOrientedCurve2D>();
198 result.setClosed(true);
199
200
201 if (polyline.getVertices().size()<2)
202 return result;
203
204
205
206
207 Point2D v1, v2;
208
209
210
211 Point2D p1, p2, p0 = null;
212
213
214 StraightLine2D line0, line;
215
216
217 Circle2D circle;
218
219 Iterator<Point2D> iterator;
220
221
222
223
224
225
226 LineSegment2D lastEdge = polyline.getLastEdge();
227 line0 = StraightLine2D.createParallel(lastEdge, d);
228
229 v2 = lastEdge.getLastPoint();
230 p0 = line0.getProjectedPoint(v2);
231
232
233 iterator = polyline.getVertices().iterator();
234 v1 = iterator.next();
235 v2 = iterator.next();
236 line = new StraightLine2D(v1, v2).getParallel(d);
237
238
239 p1 = line.getProjectedPoint(v1);
240 if (Angle2D.getAngle(line0, line)>Math.PI^d<0) {
241
242
243 p1 = line.getIntersection(line0);
244 p0 = p1;
245 } else {
246
247 circle = new Circle2D(v1, Math.abs(d));
248 result.addCurve(new CircleArc2D(v1, Math.abs(d), circle
249 .getPosition(p0), circle.getPosition(p1), d>0));
250 }
251
252 p2 = line.getProjectedPoint(v2);
253 line0 = line;
254
255
256
257
258 while (iterator.hasNext()) {
259
260 v1 = v2;
261 v2 = iterator.next();
262 line = new StraightLine2D(v1, v2).getParallel(d);
263
264
265 if (Angle2D.getAngle(line0, line)>Math.PI^d<0) {
266
267
268 p2 = line.getIntersection(line0);
269
270 result.addCurve(new LineSegment2D(p1, p2));
271 p1 = p2;
272 } else {
273
274
275 result.addCurve(new LineSegment2D(p1, p2));
276 p1 = line.getProjectedPoint(v1);
277 circle = new Circle2D(v1, Math.abs(d));
278 result.addCurve(new CircleArc2D(v1, Math.abs(d), circle
279 .getPosition(p2), circle.getPosition(p1), d>0));
280 }
281
282
283 p2 = line.getProjectedPoint(v2);
284 line0 = line;
285 }
286
287
288
289
290 iterator = polyline.getVertices().iterator();
291 v1 = v2;
292 v2 = iterator.next();
293 line = new StraightLine2D(v1, v2).getParallel(d);
294
295
296 if (Angle2D.getAngle(line0, line)>Math.PI^d<0) {
297
298
299 p2 = line.getIntersection(line0);
300
301 result.addCurve(new LineSegment2D(p1, p2));
302 p1 = p2;
303 } else {
304
305
306 result.addCurve(new LineSegment2D(p1, p2));
307 p1 = line.getProjectedPoint(v1);
308 circle = new Circle2D(v1, Math.abs(d));
309 result.addCurve(new CircleArc2D(v1, Math.abs(d), circle
310 .getPosition(p2), circle.getPosition(p1), d>0));
311 }
312
313
314 result.addCurve(new LineSegment2D(p1, p0));
315
316
317 return result;
318 }
319
320
321
322
323
324
325
326 public final static Collection<Point2D> intersect(
327 Polyline2D poly1, Polyline2D poly2) {
328 ArrayList<Point2D> points = new ArrayList<Point2D>();
329
330 Point2D point;
331 for(LineSegment2D edge1 : poly1.getEdges()){
332 for(LineSegment2D edge2 : poly2.getEdges()){
333 point = edge1.getIntersection(edge2);
334 if(point!=null)
335 points.add(point);
336 }
337 }
338
339 return points;
340 }
341 }