From a36d061c8e18122c44cf5ad6e8317f03bc3531eb Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Thu, 25 Mar 2021 18:28:11 -0400 Subject: ball: Convert loops to recursions --- (limited to 'src') diff --git a/src/ball.c b/src/ball.c index 7fd7660..826b210 100644 --- a/src/ball.c +++ b/src/ball.c @@ -60,43 +60,39 @@ db_ball_new(int x, int y, int r, int a, int d, int sr, double s, return ball; } -static void -_db_ball_collisions(struct db_ball *this) +void +db_balls_collisions(struct db_ball *ball) { - struct db_ball *that; + struct db_ball *other; int col_x; int col_y; - for (that = this->next; that != NULL; that = that->next) { - if (db_col_pt_cir_cir(this->x, this->y, this->r, - that->x, that->y, that->r, + for (; other != NULL; other = other->next) { + if (db_col_pt_cir_cir(ball->x, ball->y, ball->r, + other->x, other->y, other->r, &col_x, &col_y)) { /* TODO: Reverse direction */ } } -} -void -db_balls_collisions(struct db_ball *ball_head) -{ - struct db_ball *ball; - - for (ball = ball_head; ball != NULL; ball = ball->next) { - _db_ball_collisions(ball); + if (ball->next != NULL) { + db_balls_collisions(ball->next); } } int -db_balls_player_collisions(struct db_ball *ball_head, +db_balls_player_collisions(struct db_ball *ball, int player_x, int player_y, int player_r) { - struct db_ball *ball; + if (db_col_cir_cir(ball->x, ball->y, ball->r, + player_x, player_y, player_r)) { + return 1; + } - for (ball = ball_head; ball != NULL; ball = ball->next) { - if (db_col_cir_cir(ball->x, ball->y, ball->r, - player_x, player_y, player_r)) { - return 1; - } + if (ball->next != NULL) { + return db_balls_player_collisions(ball->next, + player_x, player_y, player_r); + } else { + return 0; } - return 0; } -- cgit v0.9.1