```/**
* Create a regular closed polygon,
* (all sides equal length, all interior angles equal).
* Vertices are generated in counter clockwise order.
*
* @param xcenter where polygon in centred on x-axis in pixels
* @param ycenter where polygon is centred on y-axis, in pixels.
* @param radius radius of circle on which all the vertices fall.
* @param sides n, the number of sides.
* @param rotation angle in radians to the left of the vertical.
* If the rotation is zero, one vertex will fall
* at the center top.
* @return Polygon containing n integer points.
*/
public static Polygon regularPolygon( int xcenter,
int ycenter,
int sides,
double rotation)
{
/* Polygon has no constructor to suggest
how big the final polygon will eventually
be. By building our own point arrays,
we avoid most of the auto-growing overhead in Polygon. */
int [] xpoints = new int[sides];
int [] ypoints = new int[sides];
/* We define vertices in mathematically postive,
counter-clockwise order.
We could negate delta to define in clockwise order.
drawPolygon and fillPolygon don't see to care. */
double delta = Math.PI * 2 / sides;
for ( int i=0; i<sides; i++ )
{
double angle = delta * i + Math.PI/+2 rotation;
xpoints[i] =(int)( radius * Math.cos( angle ) + .5) + xcenter;
/* negative to compensate for
Java.awt y axis reverse convention to math */
ypoints[i] = ycenter - (int)( radius * Math.sin( angle ) + .5);
} /* we don't bother to close the polygon
since fillPolygon etc. does that automatically. */
return new Polygon( xpoints, ypoints, sides );
}```