1 package org.opentrafficsim.base.geometry;
2
3 import java.util.List;
4
5 import org.djutils.draw.bounds.Bounds;
6 import org.djutils.draw.line.PolyLine2d;
7 import org.djutils.draw.line.Polygon2d;
8 import org.djutils.draw.point.Point2d;
9
10
11
12
13
14
15
16
17
18
19
20 public class ClickableBounds
21 {
22
23
24 private static double R_MIN = 1.0;
25
26
27
28
29 private ClickableBounds()
30 {
31
32 }
33
34
35
36
37
38
39 public static OtsBounds2d get(final Bounds<?, ?, ?> bounds)
40 {
41 if (bounds.getDeltaX() < 2 * R_MIN)
42 {
43 double x = (bounds.getMinX() + bounds.getMaxX()) / 2.0;
44 if (bounds.getDeltaY() < 2 * R_MIN)
45 {
46 double y = (bounds.getMinY() + bounds.getMaxY()) / 2.0;
47 return new BoundingRectangle(x - R_MIN, x + R_MIN, y - R_MIN, y + R_MIN);
48 }
49 return new BoundingRectangle(x - R_MIN, x + R_MIN, bounds.getMinY(), bounds.getMaxY());
50 }
51 if (bounds.getDeltaY() < 2 * R_MIN)
52 {
53 double y = (bounds.getMinY() + bounds.getMaxY()) / 2.0;
54 return new BoundingRectangle(bounds.getMinX(), bounds.getMaxX(), y - R_MIN, y + R_MIN);
55 }
56 return new BoundingRectangle(bounds.getMinX(), bounds.getMaxX(), bounds.getMinY(), bounds.getMaxY());
57 }
58
59
60
61
62
63
64 public static BoundingPolygon get(final PolyLine2d flattenedLine)
65 {
66 PolyLine2d left = flattenedLine.offsetLine(R_MIN);
67 PolyLine2d right = flattenedLine.offsetLine(-R_MIN);
68 List<Point2d> points = left.getPointList();
69 points.addAll(right.reverse().getPointList());
70 return new BoundingPolygon(new Polygon2d(points));
71 }
72
73 }