View Javadoc
1   package org.opentrafficsim.base.geometry;
2   
3   import org.djutils.draw.line.Polygon2d;
4   import org.djutils.draw.point.Point2d;
5   
6   /**
7    * Bounding rectangle; this is a non-centered box.
8    * <p>
9    * Copyright (c) 2024-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
10   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
11   * </p>
12   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
13   */
14  public class BoundingRectangle implements OtsBounds2d
15  {
16  
17      /** Minimum x coordinate. */
18      private final double minX;
19  
20      /** Maximum x coordinate. */
21      private final double maxX;
22  
23      /** Minimum y coordinate. */
24      private final double minY;
25  
26      /** Maximum y coordinate. */
27      private final double maxY;
28  
29      /** Half width. */
30      private final double dx;
31  
32      /** Half height. */
33      private final double dy;
34  
35      /** Middle point. */
36      private final Point2d midPoint;
37  
38      /** Resulting polygon. */
39      private Polygon2d polygon;
40  
41      /**
42       * Constructor.
43       * @param minX double; minimum x coordinate.
44       * @param maxX double; maximum x coordinate.
45       * @param minY double; minimum y coordinate.
46       * @param maxY double; maximum y coordinate.
47       */
48      public BoundingRectangle(final double minX, final double maxX, final double minY, final double maxY)
49      {
50          this.minX = minX;
51          this.maxX = maxX;
52          this.minY = minY;
53          this.maxY = maxY;
54          this.dx = (maxX - minX) / 2.0;
55          this.dy = (maxY - minY) / 2.0;
56          this.midPoint = new Point2d(minX + this.dx, minY + this.dy);
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public Point2d midPoint()
62      {
63          return this.midPoint;
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public double signedDistance(final Point2d point)
69      {
70          double qx = Math.abs(point.x - this.midPoint.x) - this.dx;
71          double qy = Math.abs(point.y - this.midPoint.y) - this.dy;
72          return Math.hypot(Math.max(qx, 0.0), Math.max(qy, 0.0)) + Math.min(Math.max(qx, qy), 0.0);
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public Polygon2d asPolygon()
78      {
79          if (this.polygon == null)
80          {
81              this.polygon = new Polygon2d(new double[] {this.maxX, this.minX, this.minX, this.maxX},
82                      new double[] {this.maxY, this.maxY, this.minY, this.minY});
83          }
84          return this.polygon;
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      public String toString()
90      {
91          return "BoundingRectangle [minX=" + this.minX + ", maxX=" + this.maxX + ", minY=" + this.minY + ", maxY=" + this.maxY
92                  + "]";
93      }
94  
95  }