/* Interesting procedure creates a spinning pulsar (a spiral "star"
 * which spins and pulsates) */

//Each of us spent about an hour and a half working on this assignment


import java.awt.*;

public class Ball extends YourTurtle {
  
private int radius; //radius of the ball
  private int vx; //horizonal velocity of the ball
  private int vy; //vertical velocity of the ball
  
  /** Constructor: a window without anything in it */
  public Ball() {
      clear();
  }
  
  /** = radius of the ball */
  public int getRadius() {
    return radius;
  }
  /** = x-velocity of the ball */
  public int getVx() {
   return vx; 
  }
  /** = y-velocity of the ball */
  public int getVy() {
    return vy;
  }
  /** first constructor, moves the ball to (x,y) facing east, with a velocity of (VX,VY), radius r, and color c represented as an int*/
  public Ball(int x, int y, int VX, int VY, int r, int c) {
    moveTo(x,y,0);
    radius = r;
    drawCircle(2*radius);
    fillCircle(2*radius);
    vx = VX;
    vy = VY;
    setColor(c);
}
  /**second constructor, which calls first with default (x,y) and color.*/
  public Ball(int VX, int VY, int r) {
    this(width/2,height/2,VX,VY,r,1);
  }
  /**moves ball according to velocity.  Takes into account the boundaries
   * and arranges a bounce*/
  public void moveBallOnce() {
    Color save = getColor();
    setColor(12);
    fillCircle(2*radius);
    setAngle(0);
    move(vx);
    setAngle(270);
    move(vy);
    if(getY() + radius > height) // bottom wall bounce
      vy = - vy;
    if(getY() - radius < 0) // top wall bounce
      vy = - vy;
    if(getX() - radius < 0) // left wall bounce
      vx = - vx;
    if(getX() + radius > width) // right wall bounce
      vx = - vx;
    setColor(save);
    drawCircle(2*radius);
    fillCircle(2*radius);
  }
  /**starts a ball in motion, with a pause inbetween each call to moveBallOnce()*/
  public void inMotion() {
    while(true) {
      pause(100);
      moveBallOnce();
    }
  }

    
    
   
  /*draws a pulsing spiral star that pulses (grows and shrinks
   * while spinning slowly) for c cycles.  The maximum line 
   * length of the pulsing spiral s set to the height of the frame.
   */
  public void pulsar(int c) {
    int fa= 0;
    
    for(int k = 0; k<c; k = k+1){
      
      
      for(int e = 10; e<height; e=e+1){   //outward pulsation
        spiralMod(e, 140, fa, 2, 0);
        fa=fa+1;                          //spins the pulsar
        if (e<110)
          pause(1);
        
      }
      
      for(int f=height; f>10; f=f-1){    //inward pulsation
        spiralMod(f, 140, fa, 2, 0);
        fa=fa+1;                         //spins the pulsar
        if (f<110)
          pause(1);
      }
    
                                  
    }
                                
                              
  }
  
  
  /*  A modification of spiral() with parameter sa that 
   * is the starting angle of the turtle and parameter sec changed
   * to represent microseconds instead of seconds, and the colors
   changed to magenta, orange, and red* */
  public void spiralMod(int n, double a, int sa, int d, int micsec) {
   clear();
   moveTo(width/2, height/2, sa);
   setColor(8);
   for(int k = 0; k<n; k=k+1){
    move(d*(k+1));
    addAngle(a);
    pause(micsec);
    if (k%3 ==1)
      setColor(8);
    else  if (k%3 == 2)
      setColor(9);
    else setColor(11);  
   }
  }
  
  
}