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  import java.util.ArrayList;
18  
19  /**
20   * Simple 2D shape polygon.
21   *
22   * @author Ronald Brill
23   */
24  public class Polygon2D implements Shape2D {
25      private final ArrayList<Point2D> points_;
26      private final Rectangle2D boundingBox_;
27  
28      /**
29       * Ctor.
30       * @see #lineTo(double, double)
31       * @param startX the x value of the first point.
32       * @param startY the Y value of the first point.
33       */
34      public Polygon2D(final double startX, final double startY) {
35          points_ = new ArrayList<>();
36          points_.add(new Point2D(startX, startY));
37          boundingBox_ = new Rectangle2D(startX, startY, startX, startY);
38      }
39  
40      /**
41       * Add another corner Point to the polygon.
42       * @param x the x value of the corner to be added
43       * @param y the y value of the corner to be added
44       * @return this to support fluent style construction
45       */
46      public Polygon2D lineTo(final double x, final double y) {
47          points_.add(new Point2D(x, y));
48          boundingBox_.extend(x, y);
49  
50          return this;
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      public boolean contains(final double x, final double y) {
58          if (!boundingBox_.contains(x, y)) {
59              return false;
60          }
61  
62          final double outsideX = boundingBox_.getLeft() - EPSILON;
63          final double outsideY = boundingBox_.getBottom();
64  
65          final Line2D testLine = new Line2D(outsideX, outsideY, x, y);
66          int intersectionCount = 0;
67  
68          int i = 0;
69          while (i < points_.size() - 1) {
70              final Point2D start = points_.get(i);
71              final Point2D end = points_.get(++i);
72              final Line2D border = new Line2D(start, end);
73  
74              if (border.contains(x, y)) {
75                  return true;
76              }
77  
78              final Point2D intersectionPoint = border.intersect(testLine);
79              if (intersectionPoint != null
80                      && border.contains(intersectionPoint.getX(), intersectionPoint.getY())
81                      && testLine.contains(intersectionPoint.getX(), intersectionPoint.getY())) {
82                  intersectionCount++;
83              }
84          }
85  
86          final Point2D start = points_.get(0);
87          final Point2D end = points_.get(i);
88          final Line2D border = new Line2D(start, end);
89  
90          if (border.contains(x, y)) {
91              return true;
92          }
93  
94          final Point2D intersectionPoint = border.intersect(testLine);
95          if (intersectionPoint != null
96                  && border.contains(intersectionPoint.getX(), intersectionPoint.getY())
97                  && testLine.contains(intersectionPoint.getX(), intersectionPoint.getY())) {
98              intersectionCount++;
99          }
100 
101         return intersectionCount % 2 != 0;
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public boolean isEmpty() {
109         return points_.size() < 2;
110     }
111 
112     @Override
113     public String toString() {
114         return "Polygon2D []";
115     }
116 }