View Javadoc
1   package org.djutils.draw.surface;
2   
3   import org.djutils.draw.DrawRuntimeException;
4   import org.djutils.draw.line.PolyLine2d;
5   import org.djutils.draw.point.Point2d;
6   import org.djutils.exceptions.Throw;
7   
8   /**
9    * Polygon2d.java. Closed PolyLine2d. The actual closing point (which is the same as the starting point) is NOT included in the
10   * super PolyLine2d.
11   * <p>
12   * Copyright (c) 2020-2021 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
14   * </p>
15   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
17   */
18  public class Polygon2d extends PolyLine2d
19  {
20  
21      /**
22       * Construct a new Polygon2d.
23       * @param point1 Point2d; the first point of the new Polygon2d
24       * @param point2 Point2d; the second point of the new Polygon2d
25       * @param otherPoints Point2d[]; all remaining points of the new Polygon2d (may be null)
26       * @throws DrawRuntimeException when point2 is equal to the last point of otherPoints, or any two successive points are
27       *             equal
28       */
29      public Polygon2d(final Point2d.html#Point2d">Point2d.html#Point2d">Point2d point1, final Point2d.html#Point2d">Point2d point2, final Point2d[] otherPoints) throws DrawRuntimeException
30      {
31          super(checkClosingPoint(point1, otherPoints), point2, otherPoints);
32      }
33  
34      /**
35       * Ensure that the last point of otherPoints is not equal to point1.
36       * @param point1 Point2d; the first point of a new Polygon2d
37       * @param otherPoints Point2d[]; the remaining points of a new Polygon2d (may be null)
38       * @return Point2d; point1; the first point for a new Polygon2d
39       * @throws DrawRuntimeException when point1 is equal to the last point of otherPoints
40       */
41      private static Point2d.html#Point2d">Point2dt2d">Point2d checkClosingPoint(final Point2d.html#Point2d">Point2d point1, final Point2d[] otherPoints) throws DrawRuntimeException
42      {
43          Throw.when(otherPoints != null && otherPoints[otherPoints.length - 1].equals(point1), DrawRuntimeException.class,
44                  "point1 must not be equal to last point of otherPoints");
45          return point1;
46      }
47  
48      /** */
49      private static final long serialVersionUID = 1L;
50  
51      /**
52       * Determine if a point is inside this Polygon.
53       * @param point Point2d; the point
54       * @return boolean; true if the point is inside this polygon, false if the point is outside this polygon. Results are
55       *         ill-defined for points on the edges of this Polygon.
56       */
57      public final boolean contains(final Point2d point)
58      {
59          return false; // TODO
60      }
61  
62      /**
63       * Subtract the overlap with another Polygon2d from this Polygon2d and return the result as a new Polygon2d.
64       * @param otherPolygon Polygon2d; the other Polygon2d
65       * @return Polygon2d; the asymmetrical difference; or null if there otherPolygon completely covers this Polygon2d
66       */
67      public Polygon2dhtml#Polygon2d">Polygon2d difference(final Polygon2d otherPolygon)
68      {
69          return null; // TODO
70      }
71  
72  }