// This program bounces multiple balls at once #include #include #include #include #include #define XDIM 600 // x dimension of table #define YDIM 400 // y dimension of table #define TABLE_COLOR COLOR(0,50,0) // color of table #define N_BALLS 100 #define BALL_RADIUS 20 // function prototypes void initialize_screen(); void initialize_ball(double &ball_loc_x, double &ball_loc_y, double &ball_direction, double &ball_velocity); void update_ball_loc(double &ball_loc_x, double &ball_loc_y, double &ball_direction, double ball_velocity); void update_screen(int old_ball_screen_x, int old_ball_screen_y, int new_ball_screen_x, int new_ball_screen_y, int ball_color); int random_in_range(int lower, int upper); int main () { int i; double ball_loc_x[N_BALLS]; // ball x coordinate in screen units double ball_loc_y[N_BALLS]; // ball y coordinate in screen units double ball_direction[N_BALLS]; // ball angle in radians double ball_velocity[N_BALLS]; // ball velocity in screen units double old_ball_loc_x; double old_ball_loc_y; initialize_screen(); for (i=0; i= XDIM - BALL_RADIUS) || (ball_loc_x + delta_x < BALL_RADIUS)) ball_direction = 3.14159 - ball_direction; else ball_loc_x += delta_x; // check whether ball has hit upper or lower wall if ((ball_loc_y + delta_y >= YDIM - BALL_RADIUS) || (ball_loc_y + delta_y < BALL_RADIUS)) ball_direction = -ball_direction; else ball_loc_y += delta_y; } /************************************ draw_object **************************/ void draw_object(int x, int y) { int i; const int N_POINTS = 5; int objectx[N_POINTS] = {-20, -20, 0, 20, 20}; int objecty[N_POINTS] = {20, -20, 0, -20, 20}; setlinestyle(SOLID_LINE, 0, 3); moveto(objectx[0] + x, objecty[0] + y); for (i=1; i