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 --- board.c | 119 ++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 59 insertions(+), 60 deletions(-) (limited to 'board.c') diff --git a/board.c b/board.c index f61bd42..7aceed6 100644 --- a/board.c +++ b/board.c @@ -7,73 +7,101 @@ #include #include -#define NUM_SECTORS (sizeof(SECTORS) / sizeof(*SECTORS)) -#define SECTOR_WIDTH (360.0 / NUM_SECTORS) -int SECTORS[] = { 20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, - 16, 8, 11, 14, 9, 12, 5 }; -int SECTOR_INDS[NUM_SECTORS]; -double SECTOR_ANGLES[NUM_SECTORS]; - +int SECTORS[] = { 20, 1, 18, 4, 13, 6, 10, 15, 2, 17, + 3, 19, 7, 16, 8, 11, 14, 9, 12, 5 }; +static int SECTOR_INDS[NUM_SECTORS]; +static double SECTOR_ANGLES[NUM_SECTORS]; static const char *SECTOR_NAMES[] = { FOREACH_SECTOR(GEN_SECTOR_STR) FOREACH_SECTOR(GEN_SECTOR_STR_D) FOREACH_SECTOR(GEN_SECTOR_STR_T) }; -static const char *RING_NAMES[] = { - FOREACH_RING(GEN_RING_STR) - "OUT" -}; double OUTER_DISTS[] = { FOREACH_RING(GEN_RING_OUTER_DIST) }; -double CENTRE_DISTS[sizeof(OUTER_DISTS) / sizeof(*OUTER_DISTS)]; +static double CENTRE_DISTS[sizeof(OUTER_DISTS) / sizeof(*OUTER_DISTS)]; +static const char *RING_NAMES[] = { + FOREACH_RING(GEN_RING_STR) + "OUT" +}; static void init_sectors() { for (size_t i = 0; i < NUM_SECTORS; ++i) { - SECTOR_INDS[SECTORS[i]-1] = i; + SECTOR_INDS[SECTORS[i] - 1] = i; double angle = 90 - (i * SECTOR_WIDTH); - if (angle < 0) angle += 360; + if (angle < 0) + angle += 360; SECTOR_ANGLES[i] = angle; } } -static void init_centre_dists() +static void init_rings() { - CENTRE_DISTS[0] = 0; + CENTRE_DISTS[R_BULL] = 0; for (int i = R_25; i < R_OUT; ++i) CENTRE_DISTS[i] = (OUTER_DISTS[i] - - ((OUTER_DISTS[i] - OUTER_DISTS[i-1]) / 2)); + ((OUTER_DISTS[i] - OUTER_DISTS[i - 1]) / 2)); } void init_board() { init_sectors(); - init_centre_dists(); + init_rings(); } -int get_sector(double angle) +static int sector_from_angle(double angle) { - double shifted = angle - 90 - (SECTOR_WIDTH/2); - return SECTORS[NUM_SECTORS - 1 - - (int)((shifted - (360 * floor(shifted/360))) / SECTOR_WIDTH)]; + double shifted = angle - 90 - (SECTOR_WIDTH / 2); + shifted -= 360 * floor(shifted / 360); + return SECTORS[NUM_SECTORS - 1 - (int)(shifted / SECTOR_WIDTH)]; } -enum ring get_ring(double radius) +static enum ring ring_from_radius(double radius) { - for (int i = R_BULL; i < R_OUT; ++i) + for (int i = R_BULL; i < R_OUT; ++i) { if (radius < OUTER_DISTS[i]) return i; + } return R_OUT; } -struct segment get_segment(struct pcoords c) +struct segment segment_from_pcoords(struct pcoords c) +{ + return (struct segment){ .sector = sector_from_angle(c.a), + .ring = ring_from_radius(c.r) }; +} + +struct segment segment_from_name(const char *name) +{ + struct segment seg = { .sector = 20 }; + + if (!strcmp(name, "BULL")) { + seg.ring = R_BULL; + } else if (!strcmp(name, "25")) { + seg.ring = R_25; + } else if (name[0] == 'T') { + seg.ring = R_TREBLE; + seg.sector = atoi(name + 1); + } else if (name[0] == 'D') { + seg.ring = R_DOUBLE; + seg.sector = atoi(name + 1); + } else { + seg.ring = R_BIG; + seg.sector = atoi(name); + } + + return seg; +} + +struct pcoords segment_centre(struct segment seg) { - return (struct segment){ .sector = get_sector(c.a), - .ring = get_ring(c.r) }; + return (struct pcoords){ + .a = SECTOR_ANGLES[SECTOR_INDS[seg.sector - 1]], + .r = CENTRE_DISTS[seg.ring] }; } const char *segment_name(struct segment seg) @@ -93,13 +121,13 @@ int segment_points(struct segment seg) { if (seg.ring == R_BULL) return 50; - else if (seg.ring == R_25) + if (seg.ring == R_25) return 25; - else if (seg.ring == R_TREBLE) + if (seg.ring == R_TREBLE) return 3 * seg.sector; - else if (seg.ring == R_DOUBLE) + if (seg.ring == R_DOUBLE) return 2 * seg.sector; - else if (seg.ring == R_SMALL || seg.ring == R_BIG) + if (seg.ring == R_SMALL || seg.ring == R_BIG) return seg.sector; return 0; } @@ -110,32 +138,3 @@ bool segment_is_double(struct segment seg) return true; return false; } - -struct pcoords segment_centre(struct segment seg) -{ - return (struct pcoords){ .a = SECTOR_ANGLES[SECTOR_INDS[seg.sector-1]], - .r = CENTRE_DISTS[seg.ring] }; -} - -struct segment segment_from_name(char *name) -{ - int s = 20; - enum ring r; - - if (!strcmp(name, "BULL")) { - r = R_BULL; - } else if (!strcmp(name, "25")) { - r = R_25; - } else if (name[0] == 'T') { - r = R_TREBLE; - s = atoi(name+1); - } else if (name[0] == 'D') { - r = R_DOUBLE; - s = atoi(name+1); - } else { - r = R_BIG; - s = atoi(name); - } - - return (struct segment){ .sector = s, .ring = r }; -} -- cgit v1.2.3-70-g09d2