1 package org.opentrafficsim.base.geometry;
2
3 import org.djutils.draw.line.Polygon2d;
4 import org.djutils.draw.point.Point2d;
5
6
7
8
9
10
11
12
13
14 public class BoundingRectangle implements OtsBounds2d
15 {
16
17
18 private final double minX;
19
20
21 private final double maxX;
22
23
24 private final double minY;
25
26
27 private final double maxY;
28
29
30 private final double dx;
31
32
33 private final double dy;
34
35
36 private final Point2d midPoint;
37
38
39 private Polygon2d polygon;
40
41
42
43
44
45
46
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
60 @Override
61 public Point2d midPoint()
62 {
63 return this.midPoint;
64 }
65
66
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
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
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 }