////////////////////////////////////////////////////////////////////////////
// "nGon.java" - a class for manipulating <i>n</i>-sided polygons whose
// vertices are of type double.
//
// Author: Joe Ganley, ngon.10.ganley@spamgourmet.com
////////////////////////////////////////////////////////////////////////////


// This is a really awful way to implement this - it'll beat the hell out of
// the garbage collector - but this application is hardly time-intensive...


// import Java junk
import java.lang.*;
import java.awt.*;



public class nGon extends Object {

private int sides;
private double theta, radius, cx, cy;
private double x[], y[];


nGon(int n, double r, double xCenter, double yCenter) {
  sides = n;
  x = new double[sides];
  y = new double[sides];
  radius = r;
  theta = 2.0 * Math.PI / sides;
  cx = xCenter;
  cy = yCenter;
  for (int i = 0; i < sides; i++) {
    x[i] = cx + Math.cos(i * theta) * radius;
    y[i] = cy + Math.sin(i * theta) * radius;
  }
} // constructor nGon(int,double,double,double)


private double dist(double x1, double y1, double x2, double y2) {
  return(Math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)));
} // dist(double,double,double,double)


public double getRadius() {
  return(radius);
} // getRadius()


public double getPerimeter() {
  return(sides * dist(x[0], y[0], x[1], y[1]));
} // getPerimeter()


// returns a suitable-for-drawing version of the polygon
public Polygon getPolygon() {
  int ix[] = new int[sides + 1], iy[] = new int[sides + 1];
 
  for (int i = 0; i < sides; i++) {
    ix[i] = (int) Math.round(x[i]);
    iy[i] = (int) Math.round(y[i]);
  }
  ix[sides] = ix[0];
  iy[sides] = iy[0];
  return(new Polygon(ix, iy, sides + 1));
} // getPolygon()

} // class nGon extends Object


