aboutsummaryrefslogtreecommitdiff
path: root/src/io/svg.c
blob: a8e2129d60b4907e9676425450be2a13a52b348f (plain)
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
#include "svg.h"

void print_pattern_svg(FILE *out, struct print_params params, struct pattern *pattern)
{
    // Init variables.
    double horizontal = 100 / (pattern->type.height + 1);
    double vertical = 100 / (pattern->type.width + 1);
    int hd = horizontal; // Horizontal distance
    int vd = vertical;   // Vertical distance
    // Print
    fprintf(out, "<svg width=\"500\" height=\"500\" style=\"background-color:black\">");
    fprintf(out, "<rect width=\"100%%\" height=\"100%%\" fill=\"black\" />");

    if (params.print_code)
    {
        char code[128];
        pattern_nums_to_str(code, pattern);
        fprintf(out, "<text x=\"0\" y=\"1em\" font-size=\"2em\" fill=\"white\">Code:%s</text>", code);
    }
    if (params.print_count)
        fprintf(out, "<text x=\"0\" y=\"2em\" font-size=\"2em\" fill=\"white\">Count:%d</text>", params.count);
    if (params.print_author)
        fprintf(out, "<text x=\"500\" y=\"500\" font-size=\"1em\" fill=\"white\" text-anchor=\"end\">adriansopar.hu</text>");

    // TODO: Print text!
    // Print points.
    for (int y = 0; y < pattern->type.height; y++)
    {
        for (int x = 0; x < pattern->type.width; x++)
        {
            fprintf(out,
                    "<circle cx=\"%f%%\" cy=\"%f%%\" r=\"%d%%\" fill=\"white\" />",
                    (double)(x + 1) * hd, (double)(y + 1) * vd, 3);
        }
    }
    // Print pattern.
    struct coord prev;
    for (int i = 0; i < pattern->length; i++)
    {
        struct coord current = pattern->path[i];
        fprintf(out,
                "<circle cx=\"%f%%\" cy=\"%f%%\" r=\"6%%\" stroke=\"#00ff00\" stroke-width=\"2\" fill=\"none\" />",
                (double)(current.x + 1) * hd, (double)(current.y + 1) * vd);
        if (i > 0)
            fprintf(out,
                    "<line x1=\"%f%%\" y1=\"%f%%\" x2=\"%f%%\" y2=\"%f%%\" stroke=\"white\" stroke-width=\"6%%\" stroke-opacity=\"0.5\" />",
                    (double)(prev.x + 1) * hd, (double)(prev.y + 1) * vd,
                    (double)(current.x + 1) * hd, (double)(current.y + 1) * vd);
        prev = current;
    }
    fprintf(out, "</svg>");
}