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 circle shape.
19   *
20   * @author Ronald Brill
21   */
22  public class Circle2D implements Shape2D {
23      private final double centerX_;
24      private final double centerY_;
25      private final double radius_;
26  
27      /**
28       * Creates a new circle with the given center and radius.
29       *
30       * @param centerX the x coordinate of the center
31       * @param centerY the y coordinate of the center
32       * @param radius the radius
33       */
34      public Circle2D(final double centerX, final double centerY, final double radius) {
35          centerX_ = centerX;
36          centerY_ = centerY;
37          radius_ = radius;
38      }
39  
40      /**
41       * {@inheritDoc}
42       */
43      @Override
44      public boolean contains(final double x, final double y) {
45          final double offsetX = centerX_ - x;
46          final double offsetY = centerY_ - y;
47  
48          return offsetX * offsetX + offsetY * offsetY <= radius_ * radius_;
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      @Override
55      public boolean isEmpty() {
56          return radius_ < EPSILON;
57      }
58  
59      @Override
60      public String toString() {
61          return "Circle2D [ (" + centerX_ + ", " + centerY_ + ") radius = " + radius_ + "]";
62      }
63  }