1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
#include "board.h"
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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)
};
double OUTER_DISTS[] = {
FOREACH_RING(GEN_RING_OUTER_DIST)
};
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;
double angle = 90 - (i * SECTOR_WIDTH);
if (angle < 0)
angle += 360;
SECTOR_ANGLES[i] = angle;
}
}
static void init_rings()
{
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));
}
void init_board()
{
init_sectors();
init_rings();
}
static int sector_from_angle(double angle)
{
double shifted = angle - 90 - (SECTOR_WIDTH / 2);
shifted -= 360 * floor(shifted / 360);
return SECTORS[NUM_SECTORS - 1 - (int)(shifted / SECTOR_WIDTH)];
}
static enum ring ring_from_radius(double radius)
{
for (int i = R_BULL; i < R_OUT; ++i) {
if (radius < OUTER_DISTS[i])
return i;
}
return R_OUT;
}
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 pcoords){
.a = SECTOR_ANGLES[SECTOR_INDS[seg.sector - 1]],
.r = CENTRE_DISTS[seg.ring] };
}
const char *segment_name(struct segment seg)
{
if (seg.ring == R_BULL || seg.ring == R_25 || seg.ring == R_OUT)
return RING_NAMES[seg.ring];
int ind = seg.sector - 1;
if (seg.ring == R_DOUBLE)
ind += 20;
else if (seg.ring == R_TREBLE)
ind += 40;
return SECTOR_NAMES[ind];
}
int segment_points(struct segment seg)
{
if (seg.ring == R_BULL)
return 50;
if (seg.ring == R_25)
return 25;
if (seg.ring == R_TREBLE)
return 3 * seg.sector;
if (seg.ring == R_DOUBLE)
return 2 * seg.sector;
if (seg.ring == R_SMALL || seg.ring == R_BIG)
return seg.sector;
return 0;
}
bool segment_is_double(struct segment seg)
{
if (seg.ring == R_DOUBLE || seg.ring == R_BULL)
return true;
return false;
}
|