import java.awt.*;

public class Polygon {

  private int n;  // the number of sides
  private Point V[]; // the list of vertices

  public Polygon() { n = 0;}

  public Polygon(int n) {
    this.n = n;
    V = new Point[n];
  }

  public Point getVertex(int i) { return V[i]; }

  public void setVertex(int i, Point P) { V[i] = P; }

  public String toString() {
    String S = "[";
    for (int i=0; i<n; ++i) {
      S += V[i];
      if (i < n-1)
        S += ",";
    } // for
    S += "]";
    return S;
  } // toString

  public static Polygon constructCenterPolygon(int n, int k, boolean quasiregular) {
    // Initialize P as the center polygon in an n-k regular or quasiregular tiling.
    // Let ABC be a triangle in a regular (n,k0-tiling, where
    //    A is the center of an n-gon (also center of the disk),
    //    B is a vertex of the n-gon, and
    //    C is the midpoint of a side of the n-gon adjacent to B.
    double angleA = Math.PI/n;
    double angleB = Math.PI/k;
    double angleC = Math.PI/2.0;
    // For a regular tiling, we need to compute the distance s from A to B.
    double sinA = Math.sin(angleA);
    double sinB = Math.sin(angleB);
    double s = Math.sin(angleC - angleB - angleA)
             / Math.sqrt(1.0 - sinB*sinB - sinA*sinA);
    // But for a quasiregular tiling, we need the distance s from A to C.
    if (quasiregular) {
      s = (s*s + 1.0) /  (2.0*s*Math.cos(angleA));
      s = s - Math.sqrt(s*s - 1.0);
    }
    // Now determine the coordinates of the n vertices of the n-gon.
    // They're all at distance s from the center of the Poincare disk.
    Polygon P = new Polygon(n);
    for (int i=0; i<n; ++i)
      P.V[i] = new Point(s * Math.cos((3+2*i)*angleA),
                         s * Math.sin((3+2*i)*angleA));
    return P;
  } // constructCenterPolygon

  // in, ix, and iy are set in setScreenCoordinateArrays
  private int in;  // length of ix and iy
  private int ix[], iy[];

  private void setScreenCoordinateArrays(Dimension d) {
    // first construct a list of all the points
    SCL pointList = null;
    for (int i=0; i<n; ++i)
      pointList = (new Line(V[i],V[(i+1)%n])).appendScreenCoordinates(pointList,d);
    // determine its length
    in = 0;
    for (SCL pl=pointList; pl!=null; pl=pl.link)
      in++;
    ix = new int[in];
    iy = new int[in];
    // now store the coordinates
    SCL pl = pointList;
    for (int i=0; i<in; i++) {
      ix[i] = pl.x;
      iy[i] = pl.y;
      pl = pl.link;
    } // for
    // implicitly return in, ix, and iy
  } // setScreenCoordinateArrays

  public void fill(Graphics g, Dimension d) {
      setScreenCoordinateArrays(d);
      g.fillPolygon(ix,iy,in);
  } // fill

  public void draw(Graphics g, Dimension d) {
      setScreenCoordinateArrays(d);
      g.drawPolygon(ix,iy,in);
  } // draw

} // Polygon