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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
#include <math.h>
#include <ncurses.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define HORIZONTAL_STDEV 50
#define VERTICAL_STDEV 50
// board spec from WDF rules
#define WIRE_WIDTH 1.56
#define INNER_DIAMETER_BULL 12.7
#define INNER_DIAMETER_25 31.8
#define DOUBLE_OUTER_EDGE 170.0
#define TREBLE_OUTER_EDGE 107.4
#define DOUBLE_INSIDE_WIDTH 8.0
#define TREBLE_INSIDE_WIDTH 8.0
#define SEGMENT_MAX_LEN 5
// distance from centre to apex of outer wire
// must be ordered outwards from centre
#define FOREACH_RING(M) \
M(BULL, INNER_DIAMETER_BULL/2 + WIRE_WIDTH/2) \
M(25, INNER_DIAMETER_25/2 + WIRE_WIDTH/2) \
M(SMALL, TREBLE_OUTER_EDGE - WIRE_WIDTH - TREBLE_INSIDE_WIDTH - \
WIRE_WIDTH/2) \
M(TREBLE, TREBLE_OUTER_EDGE - WIRE_WIDTH/2) \
M(BIG, DOUBLE_OUTER_EDGE - WIRE_WIDTH - DOUBLE_INSIDE_WIDTH - \
WIRE_WIDTH/2) \
M(DOUBLE, DOUBLE_OUTER_EDGE - WIRE_WIDTH/2)
#define GEN_RING_ENUM(X, D) R_ ## X,
#define GEN_RING_STRING(X, D) #X,
#define GEN_RING_OUTER_DIST(X, D) D,
int sectors[] = { 20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7,
16, 8, 11, 14, 9, 12, 5 };
enum ring {
FOREACH_RING(GEN_RING_ENUM)
R_OUT
};
char *RING_NAMES[] = {
FOREACH_RING(GEN_RING_STRING)
"OUT"
};
double OUTER_DISTS[] = {
FOREACH_RING(GEN_RING_OUTER_DIST)
};
double CENTRE_DISTS[sizeof(OUTER_DISTS)/sizeof(*OUTER_DISTS)];
void init_centre_dists()
{
CENTRE_DISTS[0] = 0;
for (int i = R_25; i < R_OUT; ++i)
CENTRE_DISTS[i] = (OUTER_DISTS[i] -
((OUTER_DISTS[i] - OUTER_DISTS[i-1]) / 2));
}
struct ccoords {
double x, y;
};
struct pcoords {
double a, r;
};
struct segment {
enum ring ring;
int sector;
};
int get_sector(double angle)
{
//return sectors[(int)(fmod(angle + 279, 360) / 18)];
double shifted = angle + 279;
return sectors[(int)((shifted - (360 * floor(shifted / 360))) / 18)];
}
enum ring get_ring(double radius)
{
for (int i = R_BULL; i < R_OUT; ++i)
if (radius < OUTER_DISTS[i])
return i;
return R_OUT;
}
double drand()
{
return (double)rand() / RAND_MAX;
}
double gauss(double mean, double stdev)
{
static bool have_next;
static double next;
double curr;
if (have_next) {
curr = next;
} else {
double theta = 2 * M_PI * drand();
double r = sqrt(-2 * log(1 - drand()));
curr = r * cos(theta);
next = r * sin(theta);
}
have_next = !have_next;
return mean + (curr * stdev);
}
struct ccoords get_offset()
{
return (struct ccoords){ .x = gauss(0, HORIZONTAL_STDEV),
.y = gauss(0, VERTICAL_STDEV) };
}
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 };
}
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 };
}
struct pcoords throw_dart(struct pcoords target)
{
struct ccoords cc = 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 });
}
struct segment get_segment(struct pcoords c)
{
return (struct segment){ .sector = get_sector(c.a),
.ring = get_ring(c.r) };
}
char *segment_name(struct segment seg)
{
char *str = malloc(SEGMENT_MAX_LEN);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-truncation"
if (seg.ring == R_BULL || seg.ring == R_25 || seg.ring == R_OUT)
strncpy(str, RING_NAMES[seg.ring], SEGMENT_MAX_LEN);
else
snprintf(str, SEGMENT_MAX_LEN,
(seg.ring == R_TREBLE ? "T%d" :
(seg.ring == R_DOUBLE ? "D%d" : "%d")),
seg.sector);
#pragma GCC diagnostic pop
return str;
}
int main()
{
/*
initscr();
addstr("test");
refresh();
getch();
endwin();
*/
srand(time(NULL));
init_centre_dists();
struct pcoords t = { .a = 90, .r = 100 };
struct pcoords res = throw_dart(t);
struct segment seg = get_segment(res);
for (int i = 0; i < 20; ++i) {
res = throw_dart(t);
seg = get_segment(res);
char *name = segment_name(seg);
printf("%d %f %f %d %s %s\n", seg.sector, res.a, res.r, seg.ring,
RING_NAMES[seg.ring], name);
free(name);
}
/*
printf("\n");
for (int i = 0; i < 6; i++) {
printf("%.2f\n%.2f\n\n", CENTRE_DISTS[i], OUTER_DISTS[i]);
}
*/
return 0;
}
|