ede1c371ac0ff192e130bc63498a7d79ee61b097
[fw/altos] / src / draw / make-logo
1 #!/usr/bin/nickle
2
3 typedef struct {
4         string  cmd;
5         real    x, y;
6 } coord_t;
7
8 coord_t[] top = {
9         { .cmd = "m",  .x = 931.07168, .y = 27.69425 },
10         { .cmd = "l",  .x = 224.03682, .y = 720.46517 },
11         { .cmd = "l",  .x = -63.341, .y = 76.00913 },
12         { .cmd = "L",  .x = 931.07168, .y = 486.3269 },
13         { .cmd = "L",  .x = 770.37586, .y = 824.16855 },
14         { .cmd = "L",  .x = 707.03486, .y = 748.15942 },
15         { .cmd = "L",  .x = 931.07168, .y = 27.69425 },
16 };
17
18 coord_t[] bottom = {
19         { .cmd = "m",  .x = 931.07168, .y = 1164.597 },
20         { .cmd = "l",  .x = 248.86992, .y = -331.80265 },
21         { .cmd = "l",  .x = 416.1687, .y = 1338.32935 },
22         { .cmd = "l",  .x = 286.6484, .y = 267.1042 },
23         { .cmd = "l",  .x = -520.4224, .y = 0 },
24         { .cmd = "l",  .x = -270.2797, .y = -262.2181 },
25         { .cmd = "l",  .x = 0, .y = -1033.0627 },
26         { .cmd = "l",  .x = -160.98492, .y = 106.6818 },
27         { .cmd = "l",  .x = -160.98492, .y = -106.6818 },
28         { .cmd = "l",  .x = 0, .y = 1033.0627 },
29         { .cmd = "l",  .x = -270.2797, .y = 262.2181 },
30         { .cmd = "l",  .x = -520.4224, .y = 0 },
31         { .cmd = "l",  .x = 286.6484, .y = -267.1042 },
32         { .cmd = "l",  .x = 416.1687, .y = -1338.32935 },
33         { .cmd = "l",  .x = 248.86992, .y = 331.80265 },
34 };
35
36 typedef struct {
37         bool    set;
38         real    min_x, max_x, min_y, max_y;
39 } bounds_t;
40
41 bounds_t
42 merge_bounds(coord_t[] polygon, bounds_t bounds)
43 {
44         real x = 0.0;
45         real y = 0.0;
46
47         for (int i = 0; i < dim(polygon); i++) {
48                 switch (polygon[i].cmd) {
49                 case "m":
50                 case "l":
51                         x += polygon[i].x;
52                         y += polygon[i].y;
53                         break;
54                 case "M":
55                 case "L":
56                         x = polygon[i].x;
57                         y = polygon[i].y;
58                         break;
59                 }
60                 if (!bounds.set) {
61                         bounds.min_x = bounds.max_x = x;
62                         bounds.min_y = bounds.max_y = y;
63                         bounds.set = true;
64                 } else {
65                         bounds.min_x = min(x, bounds.min_x);
66                         bounds.max_x = max(x, bounds.max_x);
67                         bounds.min_y = min(y, bounds.min_y);
68                         bounds.max_y = max(y, bounds.max_y);
69                 }
70         }
71         return bounds;
72 }
73
74 void
75 print_poly(coord_t[] polygon, real height, bounds_t bounds, real x_pos, real y_pos)
76 {
77         real x = 0.0;
78         real y = 0.0;
79
80         real scale = height / (bounds.max_y - bounds.min_y);
81         real x_off = bounds.min_x;
82         real y_off = bounds.min_y;
83
84         for (int i = 0; i < dim(polygon); i++) {
85                 switch (polygon[i].cmd) {
86                 case "m":
87                 case "l":
88                         x += polygon[i].x;
89                         y += polygon[i].y;
90                         break;
91                 case "M":
92                 case "L":
93                         x = polygon[i].x;
94                         y = polygon[i].y;
95                         break;
96                 }
97                 printf("\t{ .x = %8g, .y = %8g },\n",
98                        (x - x_off) * scale + x_pos,
99                        (y - y_off) * scale + y_pos);
100         }
101 }
102
103 void
104 print_logo(string name, real height, real x_pos, real y_pos)
105 {
106         bounds_t bounds = { .set = false };
107         bounds = merge_bounds(top, bounds);
108         bounds = merge_bounds(bottom, bounds);
109         printf("const struct ao_coord %s_top[] = {\n", name);
110         print_poly(top, height, bounds, x_pos, y_pos);
111         printf("};\n");
112         printf("\n");
113         printf("const struct ao_coord %s_bottom[] = {\n", name);
114         print_poly(bottom, height, bounds, x_pos, y_pos);
115         printf("};\n");
116 }
117
118 print_logo(argv[1], string_to_real(argv[2]), string_to_real(argv[3]), string_to_real(argv[4]));