// Computer Science 100J                           Assignment #4
//                                                 Jin-Gap Kim
//
// I have spent around 5 hours implementing YourTurtle,
// then another 6 hours on the rest of the Ball.java program
// 
// Thank you for your careful review.

import java.awt.*;

public class Ball extends YourTurtle {
    
    private int radius;
    private int vx;
    private int vy;
    
    public int getRadius(){
        return radius;
    }
    public int getVx(){
        return vx;
    }
    public int getVy(){
        return vy;
    }

    public Ball(int vx, int vy, int r){
        moveTo( width/2, height/2, 0);
        this.radius = r;
        drawCircle(radius);
    }
        
    
    public Ball (int x, int y, int vx, int vy, int r, int c){
        moveTo(x, y, 0);
        this.vx = vx;
        this.vy = vy;
        radius = r;
        
        setColor(c);
        fillCircle(radius);
    }
    
    /** Moves ball once. */
    public void moveBallOnce(){
        
        // preliminary setup
        
        int xx = getX();
        int yy = getY();
        Color save = getColor();
        
        int vxx = getVx();
        int vyy = getVy();
        
        // erasing the ball
        
        setColor(12);
        fillCircle(radius);
        
        // Changing the coordinate
        
        xx += vxx;
        yy += vyy;
        
        // Speed Adjustment
        
        if ( xx < radius || width - xx < radius ) vx = -vx;
        if ( yy < radius || height - yy < radius ) vy = -vy;
        
        moveTo(xx, yy, 0);
        
        // The ball reappears
        
        setColor(save);
        fillCircle(radius);
        
        
    }
    

    /** Ball in motion */
    public void inMotion(){
        while (true) {
            pause (100);
            moveBallOnce();
        }
    }
    
    /** Something of my own */
    
    /** 1. n-pointed star: draws an n-pointed star at the center of 
     *  the frame with a point on top. */
    
    public void drawNstar(int n, int d, int c){
        
        moveTo(250, 250,0);
        setColor(12);
        fillRectangle(500,500); //Initializing
        
        if (n % 2 == 0) n ++;
        
        double angle = Math.PI/n;
        double Angle = 180.0 + (180.0/n);
        double adjust = (d/2) / (Math.cos(angle/2));
        
        double x = 250;
        double y = 250 - adjust;
        
        setColor(c);
        
        moveTo((int)x, (int)y, (int)(270 + (angle/2)*(180/Math.PI)));
        for (int i = 0; i < n; i++){
            move(d);
            addAngle(Angle);
        }
    }
    
    /** Constructor: a window without anything in it */
    public Ball() {
        clear();
    }
    
    /** 2. Spinning the n-pointed star:
     * the n-pointed star rotates about the center.
     * Line length is d, and color is c.
     * Caution: only stars with length less than 200 is seen rotating within the 
     * frame*/
    
    public void rotateStar(int n, int d, int c){
        
        moveTo(250, 250, 0);
        setColor(12);
        fillRectangle(500,500); // Initializing
        
        if (n % 2 == 0) n ++;
        
        double angle = Math.PI/n;
        double Angle = 180.0 + (180.0/n);
        double adjust = (d/2) / (Math.cos(angle/2));
        
        double x = 250 + adjust;
        double y = 250;
        
        setColor(c);

        double startAngle = 180 + (angle/2)*(180/Math.PI);
        int dummy=0;
        
        while(true){
            
            pause(50);
            
            setColor(12);
            moveTo((int)x, (int)y, (int)startAngle);
            for (int i = 0; i < n; i++){
                move(d);
                addAngle(Angle);
            }
            
            //Place Adjustment
            
            x = 250 + adjust*Math.cos(Math.PI/180*dummy);
            y = 250 + adjust*Math.sin(Math.PI/180*dummy);
            
            startAngle --;
            setColor(c);
            
            moveTo((int)x, (int)y, (int)(startAngle));
            for (int i = 0; i < n; i++){
                move(d);
                addAngle(Angle);
            }
            
            dummy++;
        }
    }
    
}