/**
 * DrawableSierpinski.java
 * @author Frank Ruskey
 * @version 1.0
 * @revised Johannes Martin
 * @version 1.1
 * @revised Bette Bultena (October 2001)
 * @version 1.2
 */
import java.awt.*;

/** 
 * Class DrawableSierpinski extends class Sierpinski adding methods
 * to draw the Sierpinski curve.
 */
public class DrawableSierpinski extends Sierpinski implements DrawableCurve {

   private static final int GRIDFACTOR = 4; //helpful to divide the canvas into grids
   private static final int XSTART = 2;
   private static final int YSTART = 1;
   private int gridSize;  // The size of the line segments
   private int numGrids;  // Needed to find the right upper x start value
   private boolean fast;  // whether to go fast or slow
   private Turtle mary;   // our dear little turtle
  
   /**
    * Draw the Sierpinski curve.
    *
    * @param depth the order of the highest order curve to draw
    * @param showAll specifies whether to draw all curves or highest order curve only
    * @param goFast specifies whether to draw fast or slow
    * @param size of the drawing area (minimum of width and height)
    * @param graphics handle
    */
   public void drawTheCurves( int depth, boolean showAll, 
			     boolean goFast, int size, Graphics graphics ) {
      numGrids = GRIDFACTOR;
      fast = goFast;

      for (int i = 1; i <= depth; ++i) {
         CurveUtil.setLineColor(i, graphics);
         numGrids *= 2;
         gridSize = size / numGrids;
         if (gridSize == 0) {
	    graphics.drawString("Parameter n too big", 100, 100);
	    return;                               
         }
         if (showAll || i == depth) {
	    mary = new Turtle(graphics, XSTART*gridSize, YSTART*gridSize);
	    makeCurve(i);
         }
      }
   }                                      
  
  /**
   * Draw a line starting at the current position.
   * 
   * @param xh number of units to go right (may be negative to go left)
   * @param yh number of units to go down (may be negative to go up)
   */
  protected void plot ( int xh, int yh ) {
    if (!fast) 
      CurveUtil.slowDown( gridSize );
    mary.move(xh * gridSize, yh * gridSize);
  }
} // end class DrawableSierpinski


