
// BouncingBall.java
// Robert Sedgewick and Kevin Wayne
// Slightly reformatted by Andrew Davison, Nov. 2022

// https://algs4.cs.princeton.edu/11model/BouncingBall.java.html

/* A bouncing tennis ball in the box from (-1, -1) to
   (1, 1), using an image.
 
   Usage:
     > compile BouncingBall.java
     > run BouncingBall

*/

public class BouncingBall
{
  public static void main(String[] args)
  {
    // initial values
    double rx = 0.480, ry = 0.860;   // position
    double vx = 0.015, vy = 0.023;   // velocity
    double radius = 0.05; 

    // scale the coordinate system
    StdDraw.setXscale(-1.0, 1.0);
    StdDraw.setYscale(-1.0, 1.0);
    StdDraw.enableDoubleBuffering();

    // animation loop
    while (true) {
      // bounce off the walls
      if (Math.abs(rx + vx) + radius > 1.0)
        vx = -vx;
      if (Math.abs(ry + vy) + radius > 1.0)
        vy = -vy;

      // update position
      rx = rx + vx;
      ry = ry + vy;

      StdDraw.clear(StdDraw.LIGHT_GRAY);
      StdDraw.picture(rx, ry, "TennisBall.png");
      StdDraw.show();
      StdDraw.pause(10);
    }
  } /* main */

}  // end of BouncingBall class
