View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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 shape rectangle.
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       * Ctor.
30       * @param x1 x value of the first corner
31       * @param y1 y value of the first corner
32       * @param x2 x value of the second corner
33       * @param y2 y value of the second corner
34       */
35      public Rectangle2D(final double x1, final double y1, final double x2, final double y2) {
36          if (x1 < x2) {
37              left_ = x1;
38              right_ = x2;
39          }
40          else {
41              left_ = x2;
42              right_ = x1;
43          }
44  
45          if (y1 > y2) {
46              top_ = y1;
47              bottom_ = y2;
48          }
49          else {
50              top_ = y2;
51              bottom_ = y1;
52          }
53      }
54  
55      /**
56       * @return the x coord of the leftmost corner.
57       */
58      public double getLeft() {
59          return left_;
60      }
61  
62      /**
63       * @return the y coord of the bottom line.
64       */
65      public double getBottom() {
66          return bottom_;
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public boolean contains(final double x, final double y) {
74          return x >= left_
75                  && x <= right_
76                  && y <= top_
77                  && y >= bottom_;
78      }
79  
80      /**
81       * Makes sure the provided point is part of the extended {@link Rectangle2D} by
82       * moving the right or left border to include y and moving the top or bottom border
83       * to include y.
84       * @param x the x position to include
85       * @param y the y position to include
86       */
87      public void extend(final double x, final double y) {
88          if (x > right_) {
89              right_ = x;
90          }
91          else {
92              if (x < left_) {
93                  left_ = x;
94              }
95          }
96  
97          if (y > top_) {
98              top_ = y;
99          }
100         else {
101             if (y < bottom_) {
102                 bottom_ = y;
103             }
104         }
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public boolean isEmpty() {
112         return Math.abs(top_ - bottom_) < EPSILON || Math.abs(left_ - right_) < EPSILON;
113     }
114 
115     @Override
116     public String toString() {
117         return "Rectangle2D [left_=" + left_ + ", top_=" + top_ + ", right_=" + right_ + ", bottom_=" + bottom_ + "]";
118     }
119 }