View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
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    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.util.geometry;
16  
17  /**
18   * Simple 2D rectangle shape.
19   *
20   * @author Ronald Brill
21   */
22  public class Rectangle2D implements Shape2D {
23      private double left_;
24      private double top_;
25      private double right_;
26      private double bottom_;
27  
28      /**
29       * Creates a new rectangle defined by two corner points.
30       *
31       * @param x1 the x coordinate of the first corner
32       * @param y1 the y coordinate of the first corner
33       * @param x2 the x coordinate of the second corner
34       * @param y2 the y coordinate of the second corner
35       */
36      public Rectangle2D(final double x1, final double y1, final double x2, final double y2) {
37          if (x1 < x2) {
38              left_ = x1;
39              right_ = x2;
40          }
41          else {
42              left_ = x2;
43              right_ = x1;
44          }
45  
46          if (y1 > y2) {
47              top_ = y1;
48              bottom_ = y2;
49          }
50          else {
51              top_ = y2;
52              bottom_ = y1;
53          }
54      }
55  
56      /**
57       * Returns the x coordinate of the left edge.
58       *
59       * @return the x coordinate of the left edge
60       */
61      public double getLeft() {
62          return left_;
63      }
64  
65      /**
66       * Returns the y coordinate of the bottom edge.
67       *
68       * @return the y coordinate of the bottom edge
69       */
70      public double getBottom() {
71          return bottom_;
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public boolean contains(final double x, final double y) {
79          return x >= left_
80                  && x <= right_
81                  && y <= top_
82                  && y >= bottom_;
83      }
84  
85      /**
86       * Expands this rectangle to ensure that the given point is included,
87       * moving the left, right, top, or bottom edge as necessary.
88       *
89       * @param x the x coordinate to include
90       * @param y the y coordinate to include
91       */
92      public void extend(final double x, final double y) {
93          if (x > right_) {
94              right_ = x;
95          }
96          else {
97              if (x < left_) {
98                  left_ = x;
99              }
100         }
101 
102         if (y > top_) {
103             top_ = y;
104         }
105         else {
106             if (y < bottom_) {
107                 bottom_ = y;
108             }
109         }
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public boolean isEmpty() {
117         return Math.abs(top_ - bottom_) < EPSILON || Math.abs(left_ - right_) < EPSILON;
118     }
119 
120     @Override
121     public String toString() {
122         return "Rectangle2D [left_=" + left_ + ", top_=" + top_ + ", right_=" + right_ + ", bottom_=" + bottom_ + "]";
123     }
124 }