View Javadoc
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   * Creates bounds that are at least 2m of size in the x and y direction, useful for animation objects a user may click on.
12   * <p>
13   * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
15   * </p>
16   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
18   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
19   */
20  public class ClickableBounds
21  {
22  
23      /** Minimum distance from center to edge. */
24      private static double R_MIN = 1.0;
25  
26      /**
27       * Constructor.
28       */
29      private ClickableBounds()
30      {
31          // utility class
32      }
33  
34      /**
35       * Creates bounds that are at least 2m of size in the x and y direction.
36       * @param bounds Bounds&lt;?, ?, ?&gt;; actual object bounds.
37       * @return Bounds&lt;?, ?, ?&gt;; bounds that are at least 2m of size in the x and y direction.
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       * Creates bounds that are clickable from a line, generating an area of 2m wide.
61       * @param flattenedLine PolyLine2d; line.
62       * @return BoundingPolygon; bounding polygon.
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  }