1 package org.djutils.draw.line;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Locale;
8
9 import org.djutils.draw.DrawRuntimeException;
10 import org.djutils.draw.point.Point2d;
11 import org.djutils.exceptions.Throw;
12
13
14
15
16
17
18
19
20
21
22
23 public class Polygon2d extends PolyLine2d
24 {
25
26 private static final long serialVersionUID = 20209999L;
27
28
29
30
31
32
33
34 public Polygon2d(final double[] x, final double[] y) throws DrawRuntimeException
35 {
36 super(fixClosingPointX(Throw.whenNull(x, "x may not be null"), Throw.whenNull(y, "y may not be null")),
37 fixClosingPointY(x, y));
38 }
39
40
41
42
43
44
45
46 private static double[] fixClosingPointX(final double[] x, final double[] y)
47 {
48 if (x.length > 1 && y.length == x.length && x[0] == x[x.length - 1] && y[0] == y[x.length - 1])
49 {
50 return Arrays.copyOf(x, x.length - 1);
51 }
52 return x;
53 }
54
55
56
57
58
59
60
61 private static double[] fixClosingPointY(final double[] x, final double[] y)
62 {
63 if (x.length > 1 && y.length == x.length && x[0] == x[x.length - 1] && y[0] == y[x.length - 1])
64 {
65 return Arrays.copyOf(y, x.length - 1);
66 }
67 return y;
68 }
69
70
71
72
73
74
75
76 public Polygon2d(final Point2d[] points) throws NullPointerException, DrawRuntimeException
77 {
78 this(PolyLine2d.makeArray(points, p -> p.x), PolyLine2d.makeArray(points, p -> p.y));
79 }
80
81
82
83
84
85
86
87
88
89
90 public Polygon2d(final Point2d.html#Point2d">Point2d point1, final Point2d point2, final Point2d... otherPoints)
91 throws NullPointerException, DrawRuntimeException
92 {
93 super(Throw.whenNull(point1, "point1 may not be null"), Throw.whenNull(point2, "point2 may not be null"),
94 fixClosingPoint(point1, otherPoints));
95 }
96
97
98
99
100
101
102
103 private static Point2d.html#Point2d">Point2dt2d">Point2d[] fixClosingPoint(final Point2d.html#Point2d">Point2d point1, final Point2d[] otherPoints)
104 {
105 if (otherPoints == null || otherPoints.length == 0)
106 {
107 return otherPoints;
108 }
109 Point2d[] result = otherPoints;
110 Point2d lastPoint = result[result.length - 1];
111 if (point1.x == lastPoint.x && point1.y == lastPoint.y)
112 {
113 result = Arrays.copyOf(otherPoints, result.length - 1);
114 lastPoint = result[result.length - 1];
115 }
116 Throw.when(point1.x == lastPoint.x && point1.y == lastPoint.y, DrawRuntimeException.class,
117 "Before last point and last point are at same location");
118 return result;
119 }
120
121
122
123
124
125
126
127 public Polygon2d(final List<Point2d> points) throws NullPointerException, DrawRuntimeException
128 {
129 super(fixClosingPoint(true, Throw.whenNull(points, "points may not be null")));
130
131 }
132
133
134
135
136
137
138
139
140
141
142 private static List<Point2d> fixClosingPoint(final boolean doNotModifyList, final List<Point2d> points)
143 throws DrawRuntimeException
144 {
145 Throw.when(points.size() < 2, DrawRuntimeException.class, "Need at least two points");
146 Point2d firstPoint = points.get(0);
147 Point2d lastPoint = points.get(points.size() - 1);
148 List<Point2d> result = points;
149 if (firstPoint.x == lastPoint.x && firstPoint.y == lastPoint.y)
150 {
151 if (doNotModifyList)
152 {
153 result = new ArrayList<>(points.size() - 1);
154 for (int i = 0; i < points.size() - 1; i++)
155 {
156 result.add(points.get(i));
157 }
158 }
159 else
160 {
161 result.remove(points.size() - 1);
162 }
163 lastPoint = result.get(result.size() - 1);
164 }
165 Throw.when(firstPoint.x == lastPoint.x && firstPoint.y == lastPoint.y, DrawRuntimeException.class,
166 "Before last point and last point are at same location");
167 return result;
168 }
169
170
171
172
173
174 public Polygon2d(final Iterator<Point2d> iterator)
175 {
176 this(fixClosingPoint(false, iteratorToList(Throw.whenNull(iterator, "iterator cannot be null"))));
177 }
178
179
180
181
182
183
184
185 public Polygon2d(final boolean filterDuplicates, final Point2d... points) throws DrawRuntimeException
186 {
187 this(PolyLine2d.cleanPoints(filterDuplicates, Arrays.stream(points).iterator()));
188 }
189
190
191
192
193
194
195
196
197 public Polygon2d(final boolean filterDuplicates, final List<Point2d> pointList) throws DrawRuntimeException
198 {
199 this(PolyLine2d.cleanPoints(filterDuplicates, pointList.iterator()));
200 }
201
202
203
204
205
206
207 public final boolean isConvex()
208 {
209 int flag = 0;
210 for (int i = 0; i < size(); i++)
211 {
212 int j = (i + 1) % size();
213 int k = (j + 1) % size();
214 double z = (getX(j) - getX(i)) * (getY(k) - getY(j)) - (getY(j) - getY(i)) * (getX(k) - getX(j));
215 if (z < 0)
216 {
217 flag |= 1;
218 }
219 else if (z > 0)
220 {
221 flag |= 2;
222 }
223 if (flag == 3)
224 {
225 return false;
226 }
227 }
228 return flag != 0;
229 }
230
231
232
233
234
235
236
237 public boolean contains(final Point2d point)
238 {
239 return contains(point.x, point.y);
240 }
241
242
243
244
245
246
247
248
249
250 public boolean contains(final double x, final double y)
251 {
252 if (!getBounds().contains(x, y))
253 {
254 return false;
255 }
256 int counter = 0;
257
258 double prevPointX = getX(size() - 1);
259 double prevPointY = getY(size() - 1);
260 for (int i = 0; i < size(); i++)
261 {
262 double curPointX = getX(i);
263 double curPointY = getY(i);
264
265 if (y > Math.min(prevPointY, curPointY) && y <= Math.max(prevPointY, curPointY)
266 && x <= Math.max(prevPointX, curPointX) && prevPointY != curPointY)
267 {
268 double xIntersection = (y - prevPointY) * (curPointX - prevPointX) / (curPointY - prevPointY) + prevPointX;
269 if (prevPointX == curPointX || x <= xIntersection)
270 {
271 counter++;
272 }
273 }
274 prevPointX = curPointX;
275 prevPointY = curPointY;
276 }
277 return counter % 2 != 0;
278 }
279
280
281
282
283
284
285 public double surface()
286 {
287
288 double result = 0;
289
290 double prevX = getX(size() - 1);
291 double prevY = getY(size() - 1);
292 for (int i = 0; i < size(); i++)
293 {
294 double thisX = getX(i);
295 double thisY = getY(i);
296 result += prevX * thisY - thisX * prevY;
297 prevX = thisX;
298 prevY = thisY;
299 }
300 return result / 2;
301 }
302
303
304 @Override
305 public double getLength()
306 {
307
308 return super.getLength() + Math.hypot(getX(size() - 1) - getX(0), getY(size() - 1) - getY(0));
309 }
310
311
312 @Override
313 public LineSegment2d getSegment(final int index)
314 {
315 if (index < size() - 1)
316 {
317 return super.getSegment(index);
318 }
319 Throw.when(index != size() - 1, DrawRuntimeException.class, "index must be in range 0..size() - 1");
320 return new LineSegment2d(getX(index), getY(index), getX(0), getY(0));
321 }
322
323
324 @Override
325 public Polygon2d reverse()
326 {
327 return new Polygon2d(super.reverse().getPoints());
328 }
329
330
331 @Override
332 public final String toExcel()
333 {
334 StringBuffer s = new StringBuffer();
335 for (int i = 0; i < size(); i++)
336 {
337 s.append(getX(i) + "\t" + getY(i) + "\n");
338 }
339 s.append(getX(0) + "\t" + getY(0) + "\n");
340 return s.toString();
341 }
342
343
344 @Override
345 public final String toPlot()
346 {
347 StringBuffer result = new StringBuffer();
348 for (int i = 0; i < size(); i++)
349 {
350 result.append(String.format(Locale.US, "%s%.3f,%.3f", 0 == result.length() ? "M" : " L", getX(i), getY(i)));
351 }
352 result.append(String.format(Locale.US, " L%.3f,%.3f", getX(0), getY(0)));
353 result.append("\n");
354 return result.toString();
355 }
356
357
358 @Override
359 public final String toString()
360 {
361 return toString("%f", false);
362 }
363
364
365 @Override
366 public String toString(final String doubleFormat, final boolean doNotIncludeClassName)
367 {
368 StringBuilder result = new StringBuilder();
369 if (!doNotIncludeClassName)
370 {
371 result.append("Polygon2d ");
372 }
373 result.append("[super=");
374 result.append(super.toString(doubleFormat, false));
375 result.append("]");
376 return result.toString();
377 }
378
379 }