From 2652dec6b38f04f96a9832622418d01805bb7d4a Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Wed, 25 May 2022 19:53:22 -0700 Subject: minor cleanup and organisation of lib code --- comp.c | 51 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) (limited to 'comp.c') diff --git a/comp.c b/comp.c index 8304f20..4f40609 100644 --- a/comp.c +++ b/comp.c @@ -9,16 +9,15 @@ double horizontal_stdev = 16, vertical_stdev = 16; -double drand() +static double drand() { return (double)rand() / RAND_MAX; } -double gauss(double mean, double stdev) +static double gauss(double mean, double stdev) { static bool have_next; static double next; - double curr; if (have_next) { @@ -35,7 +34,7 @@ double gauss(double mean, double stdev) return mean + (curr * stdev); } -struct ccoords get_offset() +static struct ccoords get_offset() { return (struct ccoords){ .x = gauss(0, horizontal_stdev), .y = gauss(0, vertical_stdev) }; @@ -44,36 +43,33 @@ struct ccoords get_offset() struct ccoords pol_to_cart(struct pcoords c) { double t = c.a * (M_PI / 180); - double x = c.r * cos(t); - double y = c.r * sin(t); - - return (struct ccoords){ .x = x, .y = y }; + return (struct ccoords){ .x = c.r * cos(t), .y = c.r * sin(t) }; } -struct pcoords cart_to_pol(struct ccoords c) +static struct pcoords cart_to_pol(struct ccoords c) { - double a = atan2(c.y, c.x) * (180 / M_PI); - double r = sqrt(pow(c.x, 2) + pow(c.y, 2)); - - return (struct pcoords){ .a = a, .r = r }; + return (struct pcoords){ .a = atan2(c.y, c.x) * (180 / M_PI), + .r = sqrt(pow(c.x, 2) + pow(c.y, 2)) }; } -struct pcoords throw_dart(struct pcoords target) +static struct pcoords throw_dart(struct pcoords target, struct ccoords *cc) { - struct ccoords cc = pol_to_cart(target); + struct ccoords tc = pol_to_cart(target); struct ccoords offset = get_offset(); - return cart_to_pol((struct ccoords){ .x = cc.x + offset.x, - .y = cc.y + offset.y }); + cc->x = tc.x + offset.x; + cc->y = tc.y + offset.y; + + return cart_to_pol(*cc); } struct segment next_dart(int rem, int darts_in_hand) { char *c = NULL; - if (rem <= 170) - c = CHECKOUTS[darts_in_hand-1][rem-1]; - if (!c && rem <= 191) - c = SETUP_SHOTS[rem-1]; + if (rem <= NUM_CHECKOUTS) + c = CHECKOUTS[darts_in_hand - 1][rem - 1]; + if (!c && rem <= NUM_SETUP_SHOTS) + c = SETUP_SHOTS[rem - 1]; if (!c) c = "T20"; @@ -85,17 +81,18 @@ void comp_visit(struct leg *l) if (l->n_visits == l->size_visits) leg_grow_visits(l); struct visit *v = l->visits + l->n_visits++; - v->darts = calloc(3, sizeof(*(v->darts))); - v->ccoords = calloc(3, sizeof(*(v->ccoords))); + v->darts = calloc(3, sizeof(*v->darts)); + v->ccoords = calloc(3, sizeof(*v->ccoords)); for (int i = 0; i < 3; ++i) { struct segment ts = next_dart(l->rem - v->points, 3 - i); struct pcoords tc = segment_centre(ts); - struct pcoords dc = throw_dart(tc); - struct segment ds = get_segment(dc); - // FIXME double conversion - v->ccoords[v->n_darts] = pol_to_cart(dc); + struct ccoords dcart; + struct pcoords dc = throw_dart(tc, &dcart); + struct segment ds = segment_from_pcoords(dc); + + v->ccoords[v->n_darts] = dcart; v->darts[v->n_darts++] = ds; v->points += segment_points(ds); -- cgit v1.2.3-70-g09d2