View Javadoc
1   /*
2    * Copyright (C) 2017 Alberto Irurueta Carro (alberto@irurueta.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.irurueta.geometry;
18  
19  /**
20   * This class defines a 2D rectangular area aligned with the horizontal and vertical axes.
21   * A box is defined by two corners, low, containing minimum coordinate values, and high,
22   * containing maximum coordinate values.
23   * This class is used internally by KD trees and quad trees.
24   */
25  public class Box2D extends Box<Point2D> {
26  
27      /**
28       * Empty constructor.
29       * Creates a box centered at the origin and having unitary area.
30       */
31      public Box2D() {
32          super();
33          lo = new InhomogeneousPoint2D(-0.5, -0.5);
34          hi = new InhomogeneousPoint2D(0.5, 0.5);
35      }
36  
37      /**
38       * Constructor.
39       *
40       * @param lo low coordinate values.
41       * @param hi high coordinate values.
42       */
43      public Box2D(final Point2D lo, final Point2D hi) {
44          super(lo, hi);
45      }
46  
47      /**
48       * Constructor from rectangle.
49       *
50       * @param rectangle a rectangle.
51       */
52      public Box2D(final Rectangle rectangle) {
53          fromRectangle(rectangle);
54      }
55  
56      /**
57       * Sets boundaries.
58       *
59       * @param loX horizontal low coordinate.
60       * @param loY vertical low coordinate.
61       * @param hiX horizontal high coordinate.
62       * @param hiY vertical high coordinate.
63       */
64      public final void setBounds(final double loX, final double loY, final double hiX, final double hiY) {
65          setBounds(new InhomogeneousPoint2D(loX, loY), new InhomogeneousPoint2D(hiX, hiY));
66      }
67  
68      /**
69       * Sets values of this box from provided rectangle.
70       *
71       * @param rectangle a rectangle containing boundaries.
72       */
73      public final void fromRectangle(final Rectangle rectangle) {
74          final var topLeft = rectangle.getTopLeft();
75          final var bottomRight = rectangle.getBottomRight();
76  
77          final var loX = topLeft.getInhomX();
78          final var loY = bottomRight.getInhomY();
79  
80          final var hiX = bottomRight.getInhomX();
81          final var hiY = topLeft.getInhomY();
82  
83          lo = new InhomogeneousPoint2D(loX, loY);
84          hi = new InhomogeneousPoint2D(hiX, hiY);
85      }
86  
87      /**
88       * Creates a rectangle equivalent to this box.
89       *
90       * @return a rectangle equivalent to this box.
91       */
92      public Rectangle toRectangle() {
93          final var result = new Rectangle();
94          toRectangle(result);
95          return result;
96      }
97  
98      /**
99       * Sets values into provided rectangle to make it equivalent to this box.
100      *
101      * @param result instance where values will be stored.
102      */
103     public void toRectangle(final Rectangle result) {
104         final var left = lo.getInhomX();
105         final var right = hi.getInhomX();
106 
107         final var bottom = lo.getInhomY();
108         final var top = hi.getInhomY();
109 
110         result.setBounds(left, top, right, bottom);
111     }
112 }