added init function for truly uniform random velocity.

This commit is contained in:
Antonio Carzaniga 2019-07-05 15:58:10 +02:00
parent d74071a339
commit 89c6a75fe3

12
balls.c
View File

@ -39,6 +39,15 @@ unsigned int n_balls = 50;
static double g_y = 20; static double g_y = 20;
static double g_x = 0; static double g_x = 0;
void random_velocity(struct ball * p) {
double r2;
do {
p->v_x = v_min + rand() % (v_max + 1 - v_min);
p->v_y = v_min + rand() % (v_max + 1 - v_min);
r2 = p->v_x*p->v_x + p->v_y*p->v_y;
} while (r2 > v_max*v_max || r2 < v_min*v_min);
}
void balls_init_state () { void balls_init_state () {
srand(time(NULL)); srand(time(NULL));
static const unsigned int border = 10; static const unsigned int border = 10;
@ -48,8 +57,7 @@ void balls_init_state () {
for (unsigned int i = 0; i < n_balls; ++i) { for (unsigned int i = 0; i < n_balls; ++i) {
balls[i].x = border + rand() % w; balls[i].x = border + rand() % w;
balls[i].y = border + rand() % h; balls[i].y = border + rand() % h;
balls[i].v_x = v_min + rand() % (v_max + 1 - v_min); random_velocity(balls + i);
balls[i].v_y = v_min + rand() % (v_max + 1 - v_min);
if (rand() % 2) if (rand() % 2)
balls[i].v_x = -balls[i].v_x; balls[i].v_x = -balls[i].v_x;
if (rand() % 2) if (rand() % 2)