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