/**
 * Program to create a line which after a pause pushes out into a _/\_ shape
 * (and then continues recursively pushing out).
 * Written 11/4/99 by Kim Bruce
 * Modified 4/00 by Andrea Danyluk
 */

import java.awt.*;
import objectdraw.*;

public class PushLine extends ActiveObject implements LineInterface {

        private static final int PAUSETIME = 400;

        private Location start,dest;        // Start and end Location of line
        private double length,                         // length of line
                       radians,                 // angle of line with positive x-axis
                       limit;                        // size at which stop expanding line
        private DrawingCanvas canvas;        // canvas to be drawn on
        private AngLine baseLine;                // straight line which will be replaced by "pushed" line

        /**
         * Constructor for line which eventually pushes out into fractal
         * @param start - starting coordinates for line
         * @param length - length of original line
         * @param radians - angle of line from the horizontal
         * @param limit - if length < limit then don't push out into fractal
         * @param canvas - where line will be drawn
         */
        public PushLine(Location start, double length, double radians, double limit,
                                                DrawingCanvas canvas) {

                // create line which will later be replaced by "pushed" out figure
                baseLine = new AngLine(start,length,radians,canvas);

                this.start = start;
                this.length = length;
                this.radians = radians;
                this.limit = limit;
                this.canvas = canvas;

                dest = baseLine.getDest();

                start();
        }


        // After pause, start pushing line out into fractal pattern
        public void run() {

                // Lines to be pushed
                LineInterface firstLine, secondLine, thirdLine, fourthLine;
                Location secondPt, thirdPt, fourthPt;

                pause(PAUSETIME);        // Pause to let user see straight line before "pushing"

                if (length < limit)  { // Base case -- just make lines in _/\_ shape
                          firstLine = new AngLine(start,length/3,radians,canvas);
                          secondPt = firstLine.getDest();
                          secondLine = new AngLine(secondPt,length/3,radians+Math.PI/3,canvas);
                          thirdPt = secondLine.getDest();
                          thirdLine = new AngLine(thirdPt,length/3,radians-Math.PI/3,canvas);
                          fourthPt = thirdLine.getDest();
                          fourthLine = new AngLine(fourthPt,length/3,radians,canvas);
                  }
                  else {                                // Recursive case -- make _/\_ shape with PushLines
                          firstLine = new PushLine(start,length/3,radians,limit,canvas);
                          secondPt = firstLine.getDest();
                          secondLine = new PushLine(secondPt,length/3,radians+Math.PI/3,limit,canvas);
                          thirdPt = secondLine.getDest();
                          thirdLine = new PushLine(thirdPt,length/3,radians-Math.PI/3,limit,canvas);
                          fourthPt = thirdLine.getDest();
                          fourthLine = new PushLine(fourthPt,length/3,radians,limit,canvas);
                }

                baseLine.hide();        // Erase original straight line
        }


        // returns end point of line
        public Location getDest()
        {
                return dest;
        }


}
