import java.awt.*;

//My interesting procedure is startClock(int hr, int min, int sec) located at the end
//of class Ball. This took me about 2 hours to code.

public class Ball extends YourTurtle {

  private int radius; //contains the radius of the ball
  private int horizontal; //speed the ball is moving in the horizontal direction
  private int vertical; //speed the ball is moving in the vertical direction
  
  
  public double getRadius(){
    return radius;}
  
  public double getHorizontalSpeed(){
    return horizontal;}
  
  public double getVerticalSpeed(){
    return vertical;}
  
  /** Constructor: a window without anything in it */
 public Ball() {
     clear();
 }
  
  /** Draws a Ball at (x,y) on the panel having a horizontal speed vx
   * and a vertical speed vy with a radius r and color c */
  
  public Ball(int x, int y, int vx, int vy, int r, int c){
    moveTo(width/2, height/2, 0);
    // setColor(12);
    // fillRectangle(width,height);
    moveTo(x,y,0);
    horizontal= vx;
    vertical= vy;
    radius= r;
    setColor(c);
    fillCircle(2*r);
    
  }
  
  /** moves ball once using speeds vx and vy in Ball constructor */ 
  public void moveBallOnce(){
    Color save= getColor();
    setAngle(0);
    clear();
    liftPen();
    
    int nx= 0; //number of moves in the horizontal direction
    while (nx < Math.abs(horizontal) + 1){
      if(horizontal==0)
        break;
       if (getX() - radius <= 0){
        horizontal= Math.abs(horizontal);}
      if (getX() + radius >= width){
        horizontal= -Math.abs(horizontal);}
      
      move(horizontal/Math.abs(horizontal));
      nx= nx + 1;}
    
    
    setAngle(270);
    
    int ny= 0; //number of moves in the vertical direction
    while (ny < Math.abs(vertical) + 1){
      if(vertical==0)
        break;
      if (getY() - radius <= 0){
        vertical= Math.abs(vertical);}
      if (getY() + radius >= height){
        vertical= -Math.abs(vertical);}
      
      move (vertical/Math.abs(vertical));
      ny= ny + 1;
    }
    
    putPenDown();
    setColor(save);
    fillCircle(2*radius);
  }
  
  /** moves ball continuously with given speeds vx and vy in the Ball constructor */
  
  public void inMotion(){
    while(true){
      pause(50);
      this.moveBallOnce();
    }
  }
  
  
  
  //Beginning of Code for Analog Clock
  //method for A4 is startClock(int hr, int min, int sec)
  //last method in class ball
  
  public void drawClock(){ //Draws Border of Clock and 12, 3, 6, and 9 hour indicators
    clear();
    setColor(1);
    moveTo(width/2, height/2, 0);
    fillCircle(500);
    setColor(12);
    fillCircle(455);
    moveTo(width/2, 45, 0);
    setColor(1);
    fillRectangle(12,50);
    moveTo(45, height/2, 0);
    fillRectangle(50,12);
    moveTo(width/2, 455, 0);
    fillRectangle(12, 50);
    moveTo(455, height/2, 0);
    fillRectangle(50,12);
    moveTo(width/2, height/2, 0);
    fillCircle(10);
    
  }
  
    int secondhand= 175; //length of second hand
    int minutehand= 150; //length of minute hand
    int hourhand= 115; //length of hour hand
    
    int secangle; //angle of second hand
    int minangle; //angle of minute hand
    int hourangle; //angle of hour hand
    
    int s; //seconds
    int m; //minutes
    int h; //hours
  
   public void drawSecond(){ //used solely to test second hand drawing 
    moveTo(250,250,secangle);
    move((int) Math.sqrt((secondhand*secondhand*Math.cos(secangle)*Math.cos(secangle)) + (secondhand*secondhand*Math.sin(secangle)*Math.sin(secangle))));
  }
  
  public void drawMinute(){ //used solely to test minute hand drawing
    moveTo(250,250,minangle);
    move((int) Math.sqrt((minutehand*minutehand*Math.cos(minangle)*Math.cos(minangle)) + (minutehand*minutehand*Math.sin(minangle)*Math.sin(minangle))));
  }
  
   public void drawHour(){ //used solely to test hour hand drawing
    moveTo(250,250,hourangle);
    move((int) Math.sqrt((hourhand*hourhand*Math.cos(hourangle)*Math.cos(hourangle)) + (hourhand*hourhand*Math.sin(hourangle)*Math.sin(hourangle))));
  }
   
   
   /** Draws a clock and indicates time with hr hour(s), min minute(s), and sec second(s) 
    * Clock is as accurate as the pause method is in class Turtle */
   
   public void startClock(int hr, int min, int sec){
    drawClock();
    setColor(1);
    
    s= sec;
    m= min;
    h= hr;
    
    secangle= 90- (sec*6);
    minangle= 90- ((min)*6);
    hourangle=90- (hr*30);
    
    
    while(true){
      moveTo(250,250,secangle);
      move((int) Math.sqrt((secondhand*secondhand*Math.cos(secangle)*Math.cos(secangle)) + (secondhand*secondhand*Math.sin(secangle)*Math.sin(secangle))));
      moveTo(250,250,minangle);
      move((int) Math.sqrt((minutehand*minutehand*Math.cos(minangle)*Math.cos(minangle)) + (minutehand*minutehand*Math.sin(minangle)*Math.sin(minangle))));
      drawHour();
      pause(1000);
      setColor(12);
      moveTo(250,250,secangle);
      move((int) Math.sqrt((secondhand*secondhand*Math.cos(secangle)*Math.cos(secangle)) + (secondhand*secondhand*Math.sin(secangle)*Math.sin(secangle))));
      secangle= secangle - 6;
      moveTo(250,250,0);
      setColor(1);
      s= s + 1;
      if (s%60==0){
        setColor(12);
        moveTo(250,250,minangle);
        move((int) Math.sqrt((minutehand*minutehand*Math.cos(minangle)*Math.cos(minangle)) + (minutehand*minutehand*Math.sin(minangle)*Math.sin(minangle))));
        minangle= minangle - 6;
        m= m + 1;
        setColor(1);
      }
      if (m%60==0 && m!=0 && s%60==0){
        setColor(12);
        moveTo(250,250,hourangle);
        move((int) Math.sqrt((hourhand*hourhand*Math.cos(hourangle)*Math.cos(hourangle)) + (hourhand*hourhand*Math.sin(hourangle)*Math.sin(hourangle))));
        hourangle= hourangle - 30;
        h= h + 1;
        setColor(1);
      }
      moveTo(250,250,minangle);
      move((int) Math.sqrt((minutehand*minutehand*Math.cos(minangle)*Math.cos(minangle)) + (minutehand*minutehand*Math.sin(minangle)*Math.sin(minangle))));
      moveTo(250,250,hourangle);
      move((int) Math.sqrt((hourhand*hourhand*Math.cos(hourangle)*Math.cos(hourangle)) + (hourhand*hourhand*Math.sin(hourangle)*Math.sin(hourangle))));
      moveTo(250,250,secangle);
      fillCircle(10);
      
    }
  }
}
  
 
  
  
  
