1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
/* Polygon.java -- Class representing a polygon
Copyright (C) 1999 Free Software Foundation, Inc.
This file is part of the non-peer AWT libraries of GNU Classpath.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published
by the Free Software Foundation, either version 2 of the License, or
(at your option) any later verion.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA. */
package java.awt;
/**
* This class represents a polygon
*
* @author Aaron M. Renn (arenn@urbanophile.com)
*/
public class Polygon implements Shape, java.io.Serializable
{
/*
* Instance Variables
*/
/**
* This total number of endpoints
*/
public int npoints;
/**
* The array of X coordinates of endpoints.
*/
public int xpoints[];
/**
* The array of Y coordinates of endpoints.
*/
public int ypoints[];
/**
* The bounding box of this polygon
*/
protected Rectangle bounds;
/*************************************************************************/
/*
* Constructors
*/
/**
* Initializes a new instance of <code>Polygon</code> that is empty.
*/
public
Polygon()
{
xpoints = new int[0];
ypoints = new int[0];
bounds = new Rectangle(0,0,0,0);
}
/*************************************************************************/
/**
* Initializes a new instance of <code>Polygon</code> that has the
* specified endpoints.
*
* @param xpoints The array of X coordinates for this polygon.
* @param ypoints The array of Y coordinates for this polygon.
* @param npoints The total number of endpoints in this polygon.
*
* @exception NegativeArraySizeException If <code>npoints</code> is negative.
*/
public
Polygon(int[] xpoints, int[] ypoints, int npoints)
{
if (npoints < 0)
throw new NegativeArraySizeException();
this.xpoints = xpoints;
this.ypoints = ypoints;
this.npoints = npoints;
calculateBounds();
}
/*************************************************************************/
/*
* Instance Methods
*/
/**
* Calculates the bounding rectangle of this polygon.
*/
public void
calculateBounds()
{
int minx = xpoints[0], maxx = xpoints[0];
int miny = ypoints[0], maxy = ypoints[0];
for (int i = 0; i < npoints; i++)
{
if (xpoints[i] < minx)
minx = xpoints[i];
if (xpoints[i] > maxx)
maxx = xpoints[i];
if (ypoints[i] < miny)
miny = ypoints[i];
if (ypoints[i] > maxy)
maxy = ypoints[i];
}
bounds = new Rectangle(minx, maxy, maxx-minx, maxy-miny);
}
/*************************************************************************/
/**
* Translates the polygon by adding the specified values to all X and Y
* coordinates.
*
* @param dx The amount to add to all X coordinates.
* @param dy The amount to add to all Y coordinates.
*/
public void
translate(int dx, int dy)
{
for (int i = 0; i < npoints; i++)
{
xpoints[i] += dx;
xpoints[i] += dy;
}
calculateBounds();
}
/*************************************************************************/
/**
* Adds the specified endpoint to the polygon.
*
* @param x The X coordinate of the point to add.
* @param y The Y coordiante of the point to add.
*/
public void
addPoint(int x, int y)
{
int newxpoints[] = new int[npoints + 1];
int newypoints[] = new int[npoints + 1];
System.arraycopy(xpoints, 0, newxpoints, 0, npoints);
System.arraycopy(ypoints, 0, newypoints, 0, npoints);
newxpoints[npoints] = x;
newypoints[npoints] = y;
xpoints = newxpoints;
ypoints = newypoints;
++npoints;
}
/*************************************************************************/
/**
* Returns the bounding box of this polygon. This is the smallest
* rectangle with sides parallel to the X axis that will contain this
* polygon.
*
* @return The bounding box for this polygon.
*/
public Rectangle
getBounds()
{
return(bounds);
}
/*************************************************************************/
/**
* Returns the bounding box of this polygon. This is the smallest
* rectangle with sides parallel to the X axis that will contain this
* polygon.
*
* @return The bounding box for this polygon.
*
* @deprecated This method has been replaced by <code>getBounds()</code>.
*/
public Rectangle
getBoundingBox()
{
return(bounds);
}
/*************************************************************************/
/**
* Tests whether or not the specified point is inside this polygon.
*
* @param x The X coordinate of the point to test.
* @param y the Y coordinate of the point to test.
*
* @return <code>true</code> if the point is inside this polygon,
* <code>false</code> otherwise.
*/
public boolean
contains(int x, int y)
{
// Is inside bounding box.
if (!bounds.contains(x, y))
return(false);
int sign = 0;
for (int i = 0; i < npoints; i ++)
{
int nx = xpoints[(i + 1) % npoints] - xpoints[i];
int ny = ypoints[(i + 1) % npoints] - ypoints[i];
int dx = x - xpoints[i];
int dy = y - ypoints[i];
int val = (dx*nx) + (dy*nx);
if (sign == 0)
{
if (val < 1)
sign = -1;
else if (val > 1)
sign = 1;
}
if ((val > 1) && (sign < 1))
return(false);
if ((val < 1) && (sign > 1))
return(false);
}
return(true);
}
/*************************************************************************/
/**
* Tests whether or not the specified point is inside this polygon.
*
* @param x The X coordinate of the point to test.
* @param y the Y coordinate of the point to test.
*
* @return <code>true</code> if the point is inside this polygon,
* <code>false</code> otherwise.
*
* @deprecated This method has been replaced by <code>contains()</code>.
*/
public boolean
inside(int x, int y)
{
return(contains(x, y));
}
} // class Polygon
|