]> pd.if.org Git - hexagon/blob - cli/hexagon.c
add hexagon cli source file
[hexagon] / cli / hexagon.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #include "hexagon.h"
6
7 /*
8  * -s scale by n
9  * -h horizontal layout
10  * -r rotate grid n degrees counter clockwise
11  * -n append a newline
12  */
13 int main(int ac, char *av[]) {
14         double scale = 1.0;
15 #if 0
16         double rotate = 0.0;
17         double tx = 0.0, ty = 0.0; /* translation */
18         int horizontal = 0;
19         int newline = 0;
20 #endif
21         int x, y;
22         char *format = NULL;
23
24         if (ac < 2) {
25                 fprintf(stderr, "must give a command\n");
26                 exit(EXIT_FAILURE);
27         }
28
29         if (!strcmp(av[1], "center")) {
30                 struct HL_hex hex;
31                 struct HL_point center;
32
33                 x = strtol(av[2], NULL, 10);
34                 y = strtol(av[3], NULL, 10);
35                 hex = HL_hex_xy(x, y);
36                 center = HL_hexcenter(hex);
37                 printf(format ? format : "%f %f\n", center.x, center.y);
38         }
39         else if (!strcmp(av[1], "polar")) {
40                 /* angle, radius */
41                 struct HL_point pt;
42                 double angle, dist;
43
44                 angle = strtod(av[2], NULL);
45                 dist = strtod(av[3], NULL);
46                 
47                 pt = HL_polar(angle, dist, NULL);
48                 printf(format ? format : "%f %f\n", pt.x, pt.y);
49         }
50         else if (!strcmp(av[1], "bin")) {
51                 struct HL_hex hex;
52                 /* angle, radius */
53                 double px, py;
54                 px = strtod(av[2], NULL);
55                 py = strtod(av[3], NULL);
56                 
57                 hex = HL_hexbin(scale, px, py);
58                 printf(format ? format : "%d %d\n", hex.x, hex.y);
59         }
60         else if (!strcmp(av[1], "distance")) {
61                 struct HL_hex from, to;
62                 int x, y, n;
63
64                 n = sscanf(av[2], "%d:%d", &x, &y);
65                 if (n != 2) {
66                         n = atoi(av[2]);
67                         x = n % 100;
68                         y = n / 100;
69                 }
70                 from.x = x;
71                 from.y = y;
72
73                 n = sscanf(av[3], "%d:%d", &x, &y);
74                 if (n != 2) {
75                         n = atoi(av[3]);
76                         x = n % 100;
77                         y = n / 100;
78                 }
79                 to.x = x;
80                 to.y = y;
81                 
82                 n = HL_distance(from, to);
83                 printf(format ? format : "%d\n", n);
84         }
85
86         return 0;
87 }