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

/*
 * Draw spirals on the screen as the mouse is moved.
 * The most simple formula for a spiral in polar 
 * coordinates is:  radius = theta
 */
public class Spirals extends WindowController {

    private static final double THETA_STEP = 0.2;
    private static final double MAX_THETA = 15 * 2 * Math.PI;

    private static final RandomDoubleGenerator perturb = new RandomDoubleGenerator(0,10);
    private static final RandomIntGenerator color = new RandomIntGenerator(0,255);
    
    // Draws a new spiral where the mouse is clicked.
    public void onMouseClick(Location center) {
        Location lastPoint = center;  // last point drawn to
        double theta = 0;
        
        // iterate until theta reaches the specified cutoff
        while (theta < MAX_THETA) {
            double radius = theta;
            
            // convert polar coordinates to cartesian
            double fudge = perturb.nextValue();
            Location newPoint = new Location(center.getX() + fudge + radius * Math.cos(theta),
                                             center.getY() + fudge + radius * Math.sin(theta));

            Line l = new Line(lastPoint, newPoint, canvas);
            l.setColor(randomColor());
            
            // get ready for the next iteration
            lastPoint = newPoint;
            theta = theta + THETA_STEP;
        }
    }
    
    public Color randomColor() {
        return (new Color(color.nextValue(), color.nextValue(), color.nextValue()));
    }
    public void onMouseEnter(Location point) {
        canvas.clear();
    }
}
