summaryrefslogtreecommitdiff
path: root/web/svg_dartboard.c
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-04-28 18:27:47 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-04-28 18:27:47 -0700
commit0fdc050919f0a7a2dce53baf348ab1f9b328d6da (patch)
tree5fa41806e0eb8140b815a4dd901de31a12fabe24 /web/svg_dartboard.c
parentfcc02efbff337c05bf448231cd8bfbe016939f8a (diff)
downloaddartboat-0fdc050919f0a7a2dce53baf348ab1f9b328d6da.tar.gz
dartboat-0fdc050919f0a7a2dce53baf348ab1f9b328d6da.tar.xz
web: less javascript, more c
Diffstat (limited to 'web/svg_dartboard.c')
-rw-r--r--web/svg_dartboard.c280
1 files changed, 280 insertions, 0 deletions
diff --git a/web/svg_dartboard.c b/web/svg_dartboard.c
new file mode 100644
index 0000000..74c4f1c
--- /dev/null
+++ b/web/svg_dartboard.c
@@ -0,0 +1,280 @@
+#include "board.h"
+#include "comp.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include <emscripten/emscripten.h>
+
+#define NUM_SECTORS 20
+#define SECTOR_WIDTH 18.0
+
+#define C_BLACK "#272b2c"
+#define C_WHITE "#fbe3ba"
+#define C_RED "#f6302f"
+#define C_GREEN "#22912d"
+#define C_WIRE "#909ca0"
+#define C_WIRE_INNER "#d8e6ec"
+
+struct elem {
+ char *name;
+ int n_attrs;
+ int size_attrs;
+ char **attr_names;
+ char **attr_vals;
+ char *content;
+};
+
+struct elem *elem_init(char *name, int size_attrs)
+{
+ struct elem *e = malloc(sizeof(*e));
+
+ size_t size = strlen(name) + 1;
+ e->name = malloc(size * sizeof(*(e->name)));
+ memcpy(e->name, name, size);
+
+ e->n_attrs = 0;
+ e->size_attrs = size_attrs;
+
+ e->attr_names = malloc(size_attrs * sizeof(*(e->attr_names)));
+ e->attr_vals = malloc(size_attrs * sizeof(*(e->attr_vals)));
+ e->content = NULL;
+
+ return e;
+}
+
+void elem_free(struct elem *e)
+{
+ for(int i = 0; i < e->n_attrs; ++i) {
+ free(e->attr_names[i]);
+ free(e->attr_vals[i]);
+ }
+
+ free(e->content);
+ free(e->attr_names);
+ free(e->attr_vals);
+ free(e->name);
+ free(e);
+}
+
+void elem_add_attr(struct elem *e, char *name, char *val)
+{
+ if (e->n_attrs == e->size_attrs) {
+ e->size_attrs *= 2;
+ e->attr_names = realloc(e->attr_names,
+ e->size_attrs * sizeof(*(e->attr_names)));
+ e->attr_vals = realloc(e->attr_vals,
+ e->size_attrs * sizeof(*(e->attr_vals)));
+ }
+
+ size_t size = strlen(name) + 1;
+ e->attr_names[e->n_attrs] = malloc(size * sizeof(**(e->attr_names)));
+ memcpy(e->attr_names[e->n_attrs], name, size);
+
+ size = strlen(val) + 1;
+ e->attr_vals[e->n_attrs] = malloc(size * sizeof(**(e->attr_vals)));
+ memcpy(e->attr_vals[e->n_attrs++], val, size);
+}
+
+struct elem *gen_arc(double a1, double a2, double r1, double r2, char *col)
+{
+ struct ccoords cc1 = pol_to_cart((struct pcoords){
+ .a = (a1 == a2 ? 1 : a2), .r = r1 });
+ struct ccoords cc2 = pol_to_cart((struct pcoords){
+ .a = (a1 == a2 ? 0 : a1), .r = r1 });
+ struct ccoords cc3 = pol_to_cart((struct pcoords){
+ .a = (a1 == a2 ? 0 : a1), .r = r2 });
+ struct ccoords cc4 = pol_to_cart((struct pcoords){
+ .a = (a1 == a2 ? 1 : a2), .r = r2 });
+
+ struct elem *e = elem_init("path", 2);
+ elem_add_attr(e, "fill", col);
+
+ char buf[512];
+ int buflen = 0;
+ buflen += sprintf(buf + buflen, "M%f %f", cc1.x, cc1.y);
+ buflen += sprintf(buf + buflen, "A%f %f 0 0 0 %f %f",
+ r1, r1, cc2.x, cc2.y);
+ if (a1 == a2)
+ buflen += sprintf(buf + buflen, "A%f %f 0 1 0 %f %f",
+ r1, r1, cc1.x, cc1.y);
+ buflen += sprintf(buf + buflen, "L%f %f", cc3.x, cc3.y);
+ buflen += sprintf(buf + buflen, "A%f %f 0 0 1 %f %f",
+ r2, r2, cc4.x, cc4.y);
+ if (a1 == a2)
+ buflen += sprintf(buf + buflen, "A%f %f 0 1 1 %f %f",
+ r2, r2, cc3.x, cc3.y);
+ buf[buflen++] = 'Z';
+ elem_add_attr(e, "d", buf);
+
+ return e;
+}
+
+struct elem *gen_segment(double a, double r1, double r2, char *col)
+{
+ return gen_arc(a - SECTOR_WIDTH/2, a + SECTOR_WIDTH/2, r1, r2, col);
+}
+
+struct elem *gen_ring(double r, double w, char *col)
+{
+ return gen_arc(0, 0, r - w/2, r + w/2, col);
+}
+
+struct elem *gen_circle(double r, char *col)
+{
+ struct elem *e = elem_init("circle", 2);
+ elem_add_attr(e, "fill", col);
+
+ char buf[512];
+ sprintf(buf, "%f", r);
+ elem_add_attr(e, "r", buf);
+
+ return e;
+}
+
+struct elem *gen_line(double a, double r1, double r2, double w, char *col)
+{
+ struct ccoords cc1 = pol_to_cart((struct pcoords){ .a = a, .r = r1 });
+ struct ccoords cc2 = pol_to_cart((struct pcoords){ .a = a, .r = r2 });
+
+ struct elem *e = elem_init("line", 6);
+ elem_add_attr(e, "stroke", col);
+
+ char buf[512];
+ sprintf(buf, "%f", cc1.x);
+ elem_add_attr(e, "x1", buf);
+ sprintf(buf, "%f", cc1.y);
+ elem_add_attr(e, "y1", buf);
+ sprintf(buf, "%f", cc2.x);
+ elem_add_attr(e, "x2", buf);
+ sprintf(buf, "%f", cc2.y);
+ elem_add_attr(e, "y2", buf);
+ sprintf(buf, "%f", w);
+ elem_add_attr(e, "stroke-width", buf);
+
+ return e;
+}
+
+void draw_elem(struct elem *e)
+{
+ EM_ASM({drawElemBoard($0, $1, $2, $3, $4)},
+ e->name, e->n_attrs, e->attr_names, e->attr_vals, e->content);
+}
+
+void draw_spider()
+{
+ struct elem *e;
+
+ for (int i = 5; i > 1; --i) {
+ e = gen_ring(OUTER_DISTS[i], WIRE_WIDTH, C_WIRE);
+ draw_elem(e);
+ elem_free(e);
+ e = gen_ring(OUTER_DISTS[i], WIRE_WIDTH/2, C_WIRE_INNER);
+ draw_elem(e);
+ elem_free(e);
+ }
+
+ for (int i = 0; i < NUM_SECTORS; ++i) {
+ double a = 90 - i*SECTOR_WIDTH - SECTOR_WIDTH/2;
+ if (a < 0) a += 360;
+
+ e = gen_line(a, OUTER_DISTS[1], OUTER_DISTS[5] + 10,
+ WIRE_WIDTH, C_WIRE);
+ draw_elem(e);
+ elem_free(e);
+ e = gen_line(a, OUTER_DISTS[1], OUTER_DISTS[5] + 10,
+ WIRE_WIDTH/2, C_WIRE_INNER);
+ draw_elem(e);
+ elem_free(e);
+ }
+
+ for (int i = 1; i >= 0; --i) {
+ e = gen_ring(OUTER_DISTS[i], WIRE_WIDTH, C_WIRE);
+ draw_elem(e);
+ elem_free(e);
+ e = gen_ring(OUTER_DISTS[i], WIRE_WIDTH/2, C_WIRE_INNER);
+ draw_elem(e);
+ elem_free(e);
+ }
+}
+
+EMSCRIPTEN_KEEPALIVE void draw_numbers()
+{
+ struct elem *e;
+
+ e = gen_ring(DIAMETER/2 - WIRE_WIDTH*4, WIRE_WIDTH, "#ddd");
+ draw_elem(e);
+ elem_free(e);
+
+ int r = DIAMETER/2 - 33/2;
+ for (int i = 0; i < 20; ++i) {
+ double a = 90 - i*SECTOR_WIDTH;
+ if (a < 0) a += 360;
+ struct ccoords cc = pol_to_cart((struct pcoords){ .a = a, .r = r });
+
+ e = elem_init("text", 5);
+ elem_add_attr(e, "font-size", "33");
+ elem_add_attr(e, "fill", "#fff");
+ elem_add_attr(e, "text-anchor", "middle");
+ elem_add_attr(e, "dominant-baseline", "central");
+
+ char buf[512];
+ sprintf(buf, "scale(1 -1) translate(%f %f) rotate(%f)",
+ cc.x, -cc.y, a <= 180 ? 90 -a : 270 -a);
+ elem_add_attr(e, "transform", buf);
+
+ e->content = malloc(3);
+ sprintf(e->content, "%d", SECTORS[i]);
+
+ draw_elem(e);
+ elem_free(e);
+ }
+}
+
+void draw_board()
+{
+ struct elem *e;
+
+ e = gen_circle(DIAMETER/2, C_BLACK);
+ draw_elem(e);
+ elem_free(e);
+
+ for (int i = 5; i > 1; --i) {
+ for (int j = 0; j < NUM_SECTORS; ++j) {
+ double a = 90 - j*SECTOR_WIDTH;
+ if (a < 0) a += 360;
+
+ e = gen_segment(a, OUTER_DISTS[i-1], OUTER_DISTS[i],
+ i%2 ? (j%2 ? C_GREEN : C_RED) : (j%2 ? C_WHITE : C_BLACK));
+ draw_elem(e);
+ elem_free(e);
+ }
+ }
+
+ e = gen_circle(OUTER_DISTS[1], C_GREEN);
+ draw_elem(e);
+ elem_free(e);
+ e = gen_circle(OUTER_DISTS[0], C_RED);
+ draw_elem(e);
+ elem_free(e);
+
+ draw_spider();
+ draw_numbers();
+}
+
+void draw_point(double x, double y) {
+ struct elem *e = gen_circle(8, "#33f");
+ elem_add_attr(e, "stroke", "#ff0");
+ elem_add_attr(e, "stroke-width", "2");
+
+ char buf[512];
+ sprintf(buf, "%f", x);
+ elem_add_attr(e, "cx", buf);
+ sprintf(buf, "%f", y);
+ elem_add_attr(e, "cy", buf);
+
+ EM_ASM({drawElemPoint($0, $1, $2, $3, $4)},
+ e->name, e->n_attrs, e->attr_names, e->attr_vals);
+ elem_free(e);
+}