summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-03-25 18:28:11 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-03-25 18:28:11 (EDT)
commita36d061c8e18122c44cf5ad6e8317f03bc3531eb (patch)
treeca9a0a5ce1f4cf83f7bf3498ca6b537baeff7322
parent008b6bfa3ea42619864db4d8bb0696921deb9bf6 (diff)
downloaddodge-balls-a36d061c8e18122c44cf5ad6e8317f03bc3531eb.zip
dodge-balls-a36d061c8e18122c44cf5ad6e8317f03bc3531eb.tar.gz
dodge-balls-a36d061c8e18122c44cf5ad6e8317f03bc3531eb.tar.bz2
ball: Convert loops to recursions
-rw-r--r--src/ball.c40
1 files changed, 18 insertions, 22 deletions
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;
}