jdk/src/share/classes/java/awt/Polygon.java
author henryjen
Tue, 10 Jun 2014 16:18:54 -0700
changeset 24865 09b1d992ca72
parent 23010 6dadb192ad81
child 25770 d132697706ea
permissions -rw-r--r--
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo Reviewed-by: mduigou, lancea, alanb, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.geom.PathIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.geom.Point2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.awt.geom.Crossings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The <code>Polygon</code> class encapsulates a description of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * closed, two-dimensional region within a coordinate space. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * region is bounded by an arbitrary number of line segments, each of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * which is one side of the polygon. Internally, a polygon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * comprises of a list of {@code (x,y)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * coordinate pairs, where each pair defines a <i>vertex</i> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * polygon, and two successive pairs are the endpoints of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * line that is a side of the polygon. The first and final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * pairs of {@code (x,y)} points are joined by a line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * that closes the polygon.  This <code>Polygon</code> is defined with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * an even-odd winding rule.  See
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * {@link java.awt.geom.PathIterator#WIND_EVEN_ODD WIND_EVEN_ODD}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * for a definition of the even-odd winding rule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * This class's hit-testing methods, which include the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <code>contains</code>, <code>intersects</code> and <code>inside</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * methods, use the <i>insideness</i> definition described in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * {@link Shape} class comments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author      Sami Shaio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @see Shape
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author      Herb Jellinek
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @since       1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class Polygon implements Shape, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * The total number of points.  The value of <code>npoints</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * represents the number of valid points in this <code>Polygon</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * and might be less than the number of elements in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * {@link #xpoints xpoints} or {@link #ypoints ypoints}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * This value can be NULL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @see #addPoint(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public int npoints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * The array of X coordinates.  The number of elements in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * this array might be more than the number of X coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * in this <code>Polygon</code>.  The extra elements allow new points
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * to be added to this <code>Polygon</code> without re-creating this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * array.  The value of {@link #npoints npoints} is equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * number of valid points in this <code>Polygon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @see #addPoint(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public int xpoints[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * The array of Y coordinates.  The number of elements in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * this array might be more than the number of Y coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * in this <code>Polygon</code>.  The extra elements allow new points
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * to be added to this <code>Polygon</code> without re-creating this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * array.  The value of <code>npoints</code> is equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * number of valid points in this <code>Polygon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @see #addPoint(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public int ypoints[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * The bounds of this {@code Polygon}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * This value can be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @see #getBoundingBox()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @see #getBounds()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    protected Rectangle bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private static final long serialVersionUID = -6460061437900069969L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Default length for xpoints and ypoints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static final int MIN_LENGTH = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Creates an empty polygon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public Polygon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        xpoints = new int[MIN_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        ypoints = new int[MIN_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Constructs and initializes a <code>Polygon</code> from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param xpoints an array of X coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param ypoints an array of Y coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param npoints the total number of points in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *                          <code>Polygon</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @exception  NegativeArraySizeException if the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *                       <code>npoints</code> is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @exception  IndexOutOfBoundsException if <code>npoints</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *             greater than the length of <code>xpoints</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *             or the length of <code>ypoints</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @exception  NullPointerException if <code>xpoints</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *             <code>ypoints</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public Polygon(int xpoints[], int ypoints[], int npoints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // Fix 4489009: should throw IndexOutofBoundsException instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // of OutofMemoryException if npoints is huge and > {x,y}points.length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (npoints > xpoints.length || npoints > ypoints.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new IndexOutOfBoundsException("npoints > xpoints.length || "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                                "npoints > ypoints.length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // Fix 6191114: should throw NegativeArraySizeException with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // negative npoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (npoints < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new NegativeArraySizeException("npoints < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // Fix 6343431: Applet compatibility problems if arrays are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // exactly npoints in length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        this.npoints = npoints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this.xpoints = Arrays.copyOf(xpoints, npoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        this.ypoints = Arrays.copyOf(ypoints, npoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Resets this <code>Polygon</code> object to an empty polygon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * The coordinate arrays and the data in them are left untouched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * but the number of points is reset to zero to mark the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * vertex data as invalid and to start accumulating new vertex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * data at the beginning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * All internally-cached data relating to the old vertices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * are discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Note that since the coordinate arrays from before the reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * are reused, creating a new empty <code>Polygon</code> might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * be more memory efficient than resetting the current one if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * the number of vertices in the new polygon data is significantly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * smaller than the number of vertices in the data from before the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * reset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @see         java.awt.Polygon#invalidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        npoints = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        bounds = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Invalidates or flushes any internally-cached data that depends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * on the vertex coordinates of this <code>Polygon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * This method should be called after any direct manipulation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * of the coordinates in the <code>xpoints</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <code>ypoints</code> arrays to avoid inconsistent results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * from methods such as <code>getBounds</code> or <code>contains</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * that might cache data from earlier computations relating to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * the vertex coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @see         java.awt.Polygon#getBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public void invalidate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        bounds = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Translates the vertices of the <code>Polygon</code> by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * <code>deltaX</code> along the x axis and by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <code>deltaY</code> along the y axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param deltaX the amount to translate along the X axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param deltaY the amount to translate along the Y axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @since 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public void translate(int deltaX, int deltaY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        for (int i = 0; i < npoints; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            xpoints[i] += deltaX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            ypoints[i] += deltaY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (bounds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            bounds.translate(deltaX, deltaY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Calculates the bounding box of the points passed to the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Sets <code>bounds</code> to the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param xpoints[] array of <i>x</i> coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param ypoints[] array of <i>y</i> coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param npoints the total number of points
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    void calculateBounds(int xpoints[], int ypoints[], int npoints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        int boundsMinX = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        int boundsMinY = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        int boundsMaxX = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        int boundsMaxY = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        for (int i = 0; i < npoints; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            int x = xpoints[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            boundsMinX = Math.min(boundsMinX, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            boundsMaxX = Math.max(boundsMaxX, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            int y = ypoints[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            boundsMinY = Math.min(boundsMinY, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            boundsMaxY = Math.max(boundsMaxY, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        bounds = new Rectangle(boundsMinX, boundsMinY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                               boundsMaxX - boundsMinX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                               boundsMaxY - boundsMinY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /*
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
   249
     * Resizes the bounding box to accommodate the specified coordinates.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param x,&nbsp;y the specified coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    void updateBounds(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (x < bounds.x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            bounds.width = bounds.width + (bounds.x - x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            bounds.x = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            bounds.width = Math.max(bounds.width, x - bounds.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // bounds.x = bounds.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (y < bounds.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            bounds.height = bounds.height + (bounds.y - y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            bounds.y = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            bounds.height = Math.max(bounds.height, y - bounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            // bounds.y = bounds.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Appends the specified coordinates to this <code>Polygon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * If an operation that calculates the bounding box of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * <code>Polygon</code> has already been performed, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * <code>getBounds</code> or <code>contains</code>, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * method updates the bounding box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param       x the specified X coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param       y the specified Y coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @see         java.awt.Polygon#getBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @see         java.awt.Polygon#contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public void addPoint(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (npoints >= xpoints.length || npoints >= ypoints.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            int newLength = npoints * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            // Make sure that newLength will be greater than MIN_LENGTH and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // aligned to the power of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (newLength < MIN_LENGTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                newLength = MIN_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            } else if ((newLength & (newLength - 1)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                newLength = Integer.highestOneBit(newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            xpoints = Arrays.copyOf(xpoints, newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            ypoints = Arrays.copyOf(ypoints, newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        xpoints[npoints] = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        ypoints[npoints] = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        npoints++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (bounds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            updateBounds(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Gets the bounding box of this <code>Polygon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * The bounding box is the smallest {@link Rectangle} whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * sides are parallel to the x and y axes of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * coordinate space, and can completely contain the <code>Polygon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @return a <code>Rectangle</code> that defines the bounds of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <code>Polygon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @since 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public Rectangle getBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return getBoundingBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Returns the bounds of this <code>Polygon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @return the bounds of this <code>Polygon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * replaced by <code>getBounds()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public Rectangle getBoundingBox() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (npoints == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (bounds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            calculateBounds(xpoints, ypoints, npoints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return bounds.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Determines whether the specified {@link Point} is inside this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * <code>Polygon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param p the specified <code>Point</code> to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @return <code>true</code> if the <code>Polygon</code> contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *                  <code>Point</code>; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @see #contains(double, double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public boolean contains(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return contains(p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Determines whether the specified coordinates are inside this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * <code>Polygon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param x the specified X coordinate to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param y the specified Y coordinate to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @return {@code true} if this {@code Polygon} contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *         the specified coordinates {@code (x,y)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *         {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @see #contains(double, double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @since 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    public boolean contains(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return contains((double) x, (double) y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Determines whether the specified coordinates are contained in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * <code>Polygon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param x the specified X coordinate to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @param y the specified Y coordinate to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @return {@code true} if this {@code Polygon} contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *         the specified coordinates {@code (x,y)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *         {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @see #contains(double, double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * replaced by <code>contains(int, int)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public boolean inside(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return contains((double) x, (double) y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public Rectangle2D getBounds2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public boolean contains(double x, double y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (npoints <= 2 || !getBoundingBox().contains(x, y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        int hits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        int lastx = xpoints[npoints - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        int lasty = ypoints[npoints - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        int curx, cury;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        // Walk the edges of the polygon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        for (int i = 0; i < npoints; lastx = curx, lasty = cury, i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            curx = xpoints[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            cury = ypoints[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if (cury == lasty) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            int leftx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            if (curx < lastx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                if (x >= lastx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                leftx = curx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                if (x >= curx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                leftx = lastx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            double test1, test2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            if (cury < lasty) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                if (y < cury || y >= lasty) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                if (x < leftx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    hits++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                test1 = x - curx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                test2 = y - cury;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                if (y < lasty || y >= cury) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                if (x < leftx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    hits++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                test1 = x - lastx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                test2 = y - lasty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (test1 < (test2 / (lasty - cury) * (lastx - curx))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                hits++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        return ((hits & 1) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    private Crossings getCrossings(double xlo, double ylo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                                   double xhi, double yhi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        Crossings cross = new Crossings.EvenOdd(xlo, ylo, xhi, yhi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        int lastx = xpoints[npoints - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        int lasty = ypoints[npoints - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        int curx, cury;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        // Walk the edges of the polygon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        for (int i = 0; i < npoints; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            curx = xpoints[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            cury = ypoints[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            if (cross.accumulateLine(lastx, lasty, curx, cury)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            lastx = curx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            lasty = cury;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return cross;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public boolean contains(Point2D p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        return contains(p.getX(), p.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    public boolean intersects(double x, double y, double w, double h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if (npoints <= 0 || !getBoundingBox().intersects(x, y, w, h)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        Crossings cross = getCrossings(x, y, x+w, y+h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        return (cross == null || !cross.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    public boolean intersects(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    public boolean contains(double x, double y, double w, double h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        if (npoints <= 0 || !getBoundingBox().intersects(x, y, w, h)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        Crossings cross = getCrossings(x, y, x+w, y+h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        return (cross != null && cross.covers(y, y+h));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    public boolean contains(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Returns an iterator object that iterates along the boundary of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * <code>Polygon</code> and provides access to the geometry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * of the outline of this <code>Polygon</code>.  An optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * {@link AffineTransform} can be specified so that the coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * returned in the iteration are transformed accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @param at an optional <code>AffineTransform</code> to be applied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *          coordinates as they are returned in the iteration, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *          <code>null</code> if untransformed coordinates are desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @return a {@link PathIterator} object that provides access to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *          geometry of this <code>Polygon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    public PathIterator getPathIterator(AffineTransform at) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        return new PolygonPathIterator(this, at);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * Returns an iterator object that iterates along the boundary of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * the <code>Shape</code> and provides access to the geometry of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * outline of the <code>Shape</code>.  Only SEG_MOVETO, SEG_LINETO, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * SEG_CLOSE point types are returned by the iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * Since polygons are already flat, the <code>flatness</code> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * is ignored.  An optional <code>AffineTransform</code> can be specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * in which case the coordinates returned in the iteration are transformed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @param at an optional <code>AffineTransform</code> to be applied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *          coordinates as they are returned in the iteration, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *          <code>null</code> if untransformed coordinates are desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @param flatness the maximum amount that the control points
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *          for a given curve can vary from colinear before a subdivided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *          curve is replaced by a straight line connecting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *          endpoints.  Since polygons are already flat the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *          <code>flatness</code> parameter is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @return a <code>PathIterator</code> object that provides access to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *          <code>Shape</code> object's geometry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        return getPathIterator(at);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    class PolygonPathIterator implements PathIterator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        Polygon poly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        AffineTransform transform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        public PolygonPathIterator(Polygon pg, AffineTransform at) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            poly = pg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            transform = at;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            if (pg.npoints == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                // Prevent a spurious SEG_CLOSE segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                index = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
         * Returns the winding rule for determining the interior of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
         * path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
         * @return an integer representing the current winding rule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
         * @see PathIterator#WIND_NON_ZERO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        public int getWindingRule() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            return WIND_EVEN_ODD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
         * Tests if there are more points to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
         * @return <code>true</code> if there are more points to read;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
         *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        public boolean isDone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            return index > poly.npoints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         * Moves the iterator forwards, along the primary direction of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         * traversal, to the next segment of the path when there are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
         * more points in that direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        public void next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         * Returns the coordinates and type of the current path segment in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
         * the iteration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
         * The return value is the path segment type:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
         * SEG_MOVETO, SEG_LINETO, or SEG_CLOSE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
         * A <code>float</code> array of length 2 must be passed in and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
         * can be used to store the coordinates of the point(s).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
         * Each point is stored as a pair of <code>float</code> x,&nbsp;y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
         * coordinates.  SEG_MOVETO and SEG_LINETO types return one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
         * point, and SEG_CLOSE does not return any points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
         * @param coords a <code>float</code> array that specifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
         * coordinates of the point(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         * @return an integer representing the type and coordinates of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         *              current path segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
         * @see PathIterator#SEG_MOVETO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         * @see PathIterator#SEG_LINETO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * @see PathIterator#SEG_CLOSE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        public int currentSegment(float[] coords) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            if (index >= poly.npoints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                return SEG_CLOSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            coords[0] = poly.xpoints[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            coords[1] = poly.ypoints[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            if (transform != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                transform.transform(coords, 0, coords, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            return (index == 0 ? SEG_MOVETO : SEG_LINETO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
         * Returns the coordinates and type of the current path segment in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
         * the iteration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
         * The return value is the path segment type:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
         * SEG_MOVETO, SEG_LINETO, or SEG_CLOSE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
         * A <code>double</code> array of length 2 must be passed in and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
         * can be used to store the coordinates of the point(s).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
         * Each point is stored as a pair of <code>double</code> x,&nbsp;y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
         * coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
         * SEG_MOVETO and SEG_LINETO types return one point,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
         * and SEG_CLOSE does not return any points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
         * @param coords a <code>double</code> array that specifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         * coordinates of the point(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
         * @return an integer representing the type and coordinates of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
         *              current path segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
         * @see PathIterator#SEG_MOVETO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
         * @see PathIterator#SEG_LINETO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
         * @see PathIterator#SEG_CLOSE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        public int currentSegment(double[] coords) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            if (index >= poly.npoints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                return SEG_CLOSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            coords[0] = poly.xpoints[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            coords[1] = poly.ypoints[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            if (transform != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                transform.transform(coords, 0, coords, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            return (index == 0 ? SEG_MOVETO : SEG_LINETO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
}