Polygon.java revision 0
0N/A * Copyright 1995-2006 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Sun in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 0N/A * The <code>Polygon</code> class encapsulates a description of a 0N/A * closed, two-dimensional region within a coordinate space. This 0N/A * region is bounded by an arbitrary number of line segments, each of 0N/A * which is one side of the polygon. Internally, a polygon 0N/A * comprises of a list of {@code (x,y)} 0N/A * coordinate pairs, where each pair defines a <i>vertex</i> of the 0N/A * polygon, and two successive pairs are the endpoints of a 0N/A * line that is a side of the polygon. The first and final 0N/A * pairs of {@code (x,y)} points are joined by a line segment 0N/A * that closes the polygon. This <code>Polygon</code> is defined with 0N/A * an even-odd winding rule. See 0N/A * {@link java.awt.geom.PathIterator#WIND_EVEN_ODD WIND_EVEN_ODD} 0N/A * for a definition of the even-odd winding rule. 0N/A * This class's hit-testing methods, which include the 0N/A * <code>contains</code>, <code>intersects</code> and <code>inside</code> 0N/A * methods, use the <i>insideness</i> definition described in the 0N/A * {@link Shape} class comments. 0N/A * @author Sami Shaio 0N/A * @author Herb Jellinek 0N/A * The total number of points. The value of <code>npoints</code> 0N/A * represents the number of valid points in this <code>Polygon</code> 0N/A * and might be less than the number of elements in 0N/A * {@link #xpoints xpoints} or {@link #ypoints ypoints}. 0N/A * This value can be NULL. 0N/A * @see #addPoint(int, int) 0N/A * The array of X coordinates. The number of elements in 0N/A * this array might be more than the number of X coordinates 0N/A * in this <code>Polygon</code>. The extra elements allow new points 0N/A * to be added to this <code>Polygon</code> without re-creating this 0N/A * array. The value of {@link #npoints npoints} is equal to the 0N/A * number of valid points in this <code>Polygon</code>. 0N/A * @see #addPoint(int, int) 0N/A * The array of Y coordinates. The number of elements in 0N/A * this array might be more than the number of Y coordinates 0N/A * in this <code>Polygon</code>. The extra elements allow new points 0N/A * to be added to this <code>Polygon</code> without re-creating this 0N/A * array. The value of <code>npoints</code> is equal to the 0N/A * number of valid points in this <code>Polygon</code>. 0N/A * @see #addPoint(int, int) 0N/A * The bounds of this {@code Polygon}. 0N/A * This value can be null. 0N/A * @see #getBoundingBox() 0N/A * JDK 1.1 serialVersionUID 0N/A * Default length for xpoints and ypoints. 0N/A * Creates an empty polygon. 0N/A * Constructs and initializes a <code>Polygon</code> from the specified 0N/A * @param xpoints an array of X coordinates 0N/A * @param ypoints an array of Y coordinates 0N/A * @param npoints the total number of points in the 0N/A * <code>Polygon</code> 0N/A * @exception NegativeArraySizeException if the value of 0N/A * <code>npoints</code> is negative. 0N/A * @exception IndexOutOfBoundsException if <code>npoints</code> is 0N/A * greater than the length of <code>xpoints</code> 0N/A * or the length of <code>ypoints</code>. 0N/A * @exception NullPointerException if <code>xpoints</code> or 0N/A * <code>ypoints</code> is <code>null</code>. 0N/A // Fix 4489009: should throw IndexOutofBoundsException instead 0N/A // of OutofMemoryException if npoints is huge and > {x,y}points.length 0N/A "npoints > ypoints.length");
0N/A // Fix 6191114: should throw NegativeArraySizeException with 0N/A // Fix 6343431: Applet compatibility problems if arrays are not 0N/A // exactly npoints in length 0N/A * Resets this <code>Polygon</code> object to an empty polygon. 0N/A * The coordinate arrays and the data in them are left untouched 0N/A * but the number of points is reset to zero to mark the old 0N/A * vertex data as invalid and to start accumulating new vertex 0N/A * data at the beginning. 0N/A * All internally-cached data relating to the old vertices 0N/A * Note that since the coordinate arrays from before the reset 0N/A * are reused, creating a new empty <code>Polygon</code> might 0N/A * be more memory efficient than resetting the current one if 0N/A * the number of vertices in the new polygon data is significantly 0N/A * smaller than the number of vertices in the data from before the 0N/A * @see java.awt.Polygon#invalidate 0N/A * Invalidates or flushes any internally-cached data that depends 0N/A * on the vertex coordinates of this <code>Polygon</code>. 0N/A * This method should be called after any direct manipulation 0N/A * of the coordinates in the <code>xpoints</code> or 0N/A * <code>ypoints</code> arrays to avoid inconsistent results 0N/A * from methods such as <code>getBounds</code> or <code>contains</code> 0N/A * that might cache data from earlier computations relating to 0N/A * the vertex coordinates. 0N/A * @see java.awt.Polygon#getBounds 0N/A * Translates the vertices of the <code>Polygon</code> by 0N/A * <code>deltaX</code> along the x axis and by 0N/A * <code>deltaY</code> along the y axis. 0N/A * @param deltaX the amount to translate along the X axis 0N/A * @param deltaY the amount to translate along the Y axis 0N/A * Calculates the bounding box of the points passed to the constructor. 0N/A * Sets <code>bounds</code> to the result. 0N/A * @param xpoints[] array of <i>x</i> coordinates 0N/A * @param ypoints[] array of <i>y</i> coordinates 0N/A * @param npoints the total number of points 0N/A * Resizes the bounding box to accomodate the specified coordinates. 0N/A * @param x, y the specified coordinates 0N/A // bounds.x = bounds.x; 0N/A // bounds.y = bounds.y; 0N/A * Appends the specified coordinates to this <code>Polygon</code>. 0N/A * If an operation that calculates the bounding box of this 0N/A * <code>Polygon</code> has already been performed, such as 0N/A * <code>getBounds</code> or <code>contains</code>, then this 0N/A * method updates the bounding box. 0N/A * @param x the specified X coordinate 0N/A * @param y the specified Y coordinate 0N/A * @see java.awt.Polygon#getBounds 0N/A * @see java.awt.Polygon#contains 0N/A // Make sure that newLength will be greater than MIN_LENGTH and 0N/A // aligned to the power of 2 0N/A * Gets the bounding box of this <code>Polygon</code>. 0N/A * The bounding box is the smallest {@link Rectangle} whose 0N/A * sides are parallel to the x and y axes of the 0N/A * coordinate space, and can completely contain the <code>Polygon</code>. 0N/A * @return a <code>Rectangle</code> that defines the bounds of this 0N/A * <code>Polygon</code>. 0N/A * Returns the bounds of this <code>Polygon</code>. 0N/A * @return the bounds of this <code>Polygon</code>. 0N/A * @deprecated As of JDK version 1.1, 0N/A * replaced by <code>getBounds()</code>. 0N/A * Determines whether the specified {@link Point} is inside this 0N/A * <code>Polygon</code>. 0N/A * @param p the specified <code>Point</code> to be tested 0N/A * @return <code>true</code> if the <code>Polygon</code> contains the 0N/A * <code>Point</code>; <code>false</code> otherwise. 0N/A * @see #contains(double, double) 0N/A * Determines whether the specified coordinates are inside this 0N/A * <code>Polygon</code>. 0N/A * @param x the specified X coordinate to be tested 0N/A * @param y the specified Y coordinate to be tested 0N/A * @return {@code true} if this {@code Polygon} contains 0N/A * the specified coordinates {@code (x,y)}; 0N/A * {@code false} otherwise. 0N/A * @see #contains(double, double) 0N/A * Determines whether the specified coordinates are contained in this 0N/A * <code>Polygon</code>. 0N/A * @param x the specified X coordinate to be tested 0N/A * @param y the specified Y coordinate to be tested 0N/A * @return {@code true} if this {@code Polygon} contains 0N/A * the specified coordinates {@code (x,y)}; 0N/A * {@code false} otherwise. 0N/A * @see #contains(double, double) 0N/A * @deprecated As of JDK version 1.1, 0N/A * replaced by <code>contains(int, int)</code>. 0N/A // Walk the edges of the polygon 0N/A // Walk the edges of the polygon 0N/A public boolean intersects(
double x,
double y,
double w,
double h) {
0N/A public boolean contains(
double x,
double y,
double w,
double h) {
0N/A * Returns an iterator object that iterates along the boundary of this 0N/A * <code>Polygon</code> and provides access to the geometry 0N/A * of the outline of this <code>Polygon</code>. An optional 0N/A * {@link AffineTransform} can be specified so that the coordinates 0N/A * returned in the iteration are transformed accordingly. 0N/A * @param at an optional <code>AffineTransform</code> to be applied to the 0N/A * coordinates as they are returned in the iteration, or 0N/A * <code>null</code> if untransformed coordinates are desired 0N/A * @return a {@link PathIterator} object that provides access to the 0N/A * geometry of this <code>Polygon</code>. 0N/A * Returns an iterator object that iterates along the boundary of 0N/A * the <code>Shape</code> and provides access to the geometry of the 0N/A * outline of the <code>Shape</code>. Only SEG_MOVETO, SEG_LINETO, and 0N/A * SEG_CLOSE point types are returned by the iterator. 0N/A * Since polygons are already flat, the <code>flatness</code> parameter 0N/A * is ignored. An optional <code>AffineTransform</code> can be specified 0N/A * in which case the coordinates returned in the iteration are transformed 0N/A * @param at an optional <code>AffineTransform</code> to be applied to the 0N/A * coordinates as they are returned in the iteration, or 0N/A * <code>null</code> if untransformed coordinates are desired 0N/A * @param flatness the maximum amount that the control points 0N/A * for a given curve can vary from colinear before a subdivided 0N/A * curve is replaced by a straight line connecting the 0N/A * endpoints. Since polygons are already flat the 0N/A * <code>flatness</code> parameter is ignored. 0N/A * @return a <code>PathIterator</code> object that provides access to the 0N/A * <code>Shape</code> object's geometry. 0N/A // Prevent a spurious SEG_CLOSE segment 0N/A * Returns the winding rule for determining the interior of the 0N/A * @return an integer representing the current winding rule. 0N/A * @see PathIterator#WIND_NON_ZERO 0N/A * Tests if there are more points to read. 0N/A * @return <code>true</code> if there are more points to read; 0N/A * <code>false</code> otherwise. 0N/A * Moves the iterator forwards, along the primary direction of 0N/A * traversal, to the next segment of the path when there are 0N/A * more points in that direction. 0N/A * Returns the coordinates and type of the current path segment in 0N/A * The return value is the path segment type: 0N/A * SEG_MOVETO, SEG_LINETO, or SEG_CLOSE. 0N/A * A <code>float</code> array of length 2 must be passed in and 0N/A * can be used to store the coordinates of the point(s). 0N/A * Each point is stored as a pair of <code>float</code> x, y 0N/A * coordinates. SEG_MOVETO and SEG_LINETO types return one 0N/A * point, and SEG_CLOSE does not return any points. 0N/A * @param coords a <code>float</code> array that specifies the 0N/A * coordinates of the point(s) 0N/A * @return an integer representing the type and coordinates of the 0N/A * current path segment. 0N/A * @see PathIterator#SEG_MOVETO 0N/A * @see PathIterator#SEG_LINETO 0N/A * @see PathIterator#SEG_CLOSE 0N/A * Returns the coordinates and type of the current path segment in 0N/A * The return value is the path segment type: 0N/A * SEG_MOVETO, SEG_LINETO, or SEG_CLOSE. 0N/A * A <code>double</code> array of length 2 must be passed in and 0N/A * can be used to store the coordinates of the point(s). 0N/A * Each point is stored as a pair of <code>double</code> x, y 0N/A * SEG_MOVETO and SEG_LINETO types return one point, 0N/A * and SEG_CLOSE does not return any points. 0N/A * @param coords a <code>double</code> array that specifies the 0N/A * coordinates of the point(s) 0N/A * @return an integer representing the type and coordinates of the 0N/A * current path segment. 0N/A * @see PathIterator#SEG_MOVETO 0N/A * @see PathIterator#SEG_LINETO 0N/A * @see PathIterator#SEG_CLOSE