/** Total time= 5.5 hours
 * Added procedure creates a spiral and 
 * animates a ball descending into the center of the spiral */

import java.awt.*;

public class Ball extends YourTurtle {
    private int radius; // Radius of ball
    private int vx; // Speed of ball in horizontal direction
    private int vy; // Speed of ball in vertical direction
    
    /** = Radius of ball */
    public int getRadius() {
        return radius;
    }
    
    /** = Horizontal speed of ball */
    public int getVX() {
        return vx;
    }
    
    /** = Vertical speed of ball */
    public int getVY() {
        return vy;
    }
    
    /** Constructor: a ball of color c and radius r starting 
     at point (x,y) and with speed (vx,vy). */
    public Ball(int x,int y,int vx,int vy,int r,Color c) {
        moveTo(x,y,0);
        setColor(c);
        fillCircle(r);
        radius= r;
        this.vx= vx;
        this.vy= vy;
    }
    
    /** Constructor: a window without anything in it */
    public Ball() {
        clear();
    }
    /** Constructor: a black ball with radius r and speed (vx,vy)
     starting at the midpoint of the window. */
    public Ball(int vx, int vy, int r) {
        this(width/2, height/2, vx, vy, r, Color.black);
    }
    
    /** Moves ball one step, given by its speed (vx,vy) */
    public void moveBallOnce() {
        Color save= getColor();
        setColor(Color.white); // Erases ball by filling in ball
        fillCircle(radius);    // with white
        moveTo(getX()+vx,getY()+vy,0);
        if (getY() < radius ||             // Negates speed in vertical 
            (height-getY()) < radius) {    // direction to mimic bouncing
            this.vy= -vy;                  // off a wall
        }
        if (getX() < radius ||             // Negates speed in horizontal
            (width-getX()) < radius) {     // direction to mimic bouncing
            this.vx= -vx;                  // off a wall
        }
        setColor(save);        // Redraws ball using original
        fillCircle(radius);    // color
    }
    
    /** Moves ball perpetually, given by its speed (vx,vy) */
    public void inMotion() {
        int x= 0;
        while(x==0) {         //Perpetual loop
            moveBallOnce();
            pause(100);
        }
    }
    
    /** Creates spiral and animates a ball of radius 10 and speed (10,0)
     swirling inward until it reaches the center */
    public void spiralToInf() {
        YourTurtle d= new YourTurtle();
        moveTo(0,10,0); 
        this.radius= 10;  
        this.vx= 10;
        this.vy= 10;
        d.spiral(1000,90,1,7);
        int stepX= width/vx;
        int stepY= height/vy;
        this.vy= 0;
        boolean k= false;
        int n=0;
        while (k!=true) {                                     // Loop moves ball in one direction until wall is
            for (int j=1; j<stepX-n;j=j+1) {                  // reached and changes speed in order to continue
                this.moveBallOnce();                          // path along edges of spiral
                pause(10);                                    
            }
            n=n+1;
            this.vy= vx;
            this.vx= 0;
            for (int j=1; j<stepY-n;j=j+1) {
                this.moveBallOnce();
                pause(10);
            } 
            this.vx= -vy;
            this.vy= 0;
            for (int j=1; j<stepX-n;j=j+1) {
                this.moveBallOnce();
                pause(10);
            }
            this.vy= vx;
            this.vx= 0;
            
            for (int j=1; j<(stepY-(n+1));j=j+1) {
                this.moveBallOnce();
                pause(10);
            }            
            this.vx= -vy;
            this.vy= 0;
            stepX= stepX+vx;
            stepY= stepY+vx;
            n=n+11;
            if (getX()==width/2 && getY()==height/2) {   //Loop ends when ball reaches center
                k= true; 
            }
        }
        
    }
            
    
}
    


