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

/*
 * An active object to draw Spirals.  Demonstrates
 * how to pass parameters to a Spiral that then get
 * used in the run() method.
 */
public class Spiral extends ActiveObject {

    private final static double DELAY = 2;
    private final static double MAX_THETA = 20 * Math.PI;
    private static final double THETA_STEP = 0.05;

    // Information provided by the window controller
    //  to tell us where to draw and what we should look like.
    private Location center;
    private double magnitude;
    private double bumps;
    
    // the canvas we are drawing on
    private DrawingCanvas canvas;

    /*
     * Create a new spiral with the given center, number
     * of bumps and magnitude.
     */
    public Spiral(Location aCenter,
                  double aMag, 
                  double aBump, 
                  DrawingCanvas aCanvas) {  
                      
        center = aCenter;
        canvas = aCanvas;
        bumps = aBump;
        magnitude = aMag;

        this.start();
    }

    public void run() {
        Location lastPoint = center;
        
        double theta = 0;
        
        // The loop is the same as the loop from the spiral code
        // that draws it all at once - except for the call to pause...
        while (theta < MAX_THETA) {
       
            double radius = theta * (1 + Math.sin(bumps * theta) * magnitude);

            // Convert polar to cartesian
            Location newPoint = 
                new Location(center.getX() + radius * Math.cos(theta),
                             center.getY() + radius * Math.sin(theta));

            new Line(lastPoint, newPoint, canvas);
            
            pause(DELAY);
            
            lastPoint = newPoint;
            
            theta = theta + THETA_STEP;
        }
    }
}
