X-Git-Url: https://pd.if.org/git/?p=hexagon;a=blobdiff_plain;f=cli%2Fhexagon.c;fp=cli%2Fhexagon.c;h=6417718c4ec45c2340392157bc9c87d0e9027e58;hp=0000000000000000000000000000000000000000;hb=3672c8cacf689199b14ab47b63123ab037a12b51;hpb=d74b76176b43b9eae3b584c231b077d3a3f0f92b diff --git a/cli/hexagon.c b/cli/hexagon.c new file mode 100644 index 0000000..6417718 --- /dev/null +++ b/cli/hexagon.c @@ -0,0 +1,87 @@ +#include +#include +#include + +#include "hexagon.h" + +/* + * -s scale by n + * -h horizontal layout + * -r rotate grid n degrees counter clockwise + * -n append a newline + */ +int main(int ac, char *av[]) { + double scale = 1.0; +#if 0 + double rotate = 0.0; + double tx = 0.0, ty = 0.0; /* translation */ + int horizontal = 0; + int newline = 0; +#endif + int x, y; + char *format = NULL; + + if (ac < 2) { + fprintf(stderr, "must give a command\n"); + exit(EXIT_FAILURE); + } + + if (!strcmp(av[1], "center")) { + struct HL_hex hex; + struct HL_point center; + + x = strtol(av[2], NULL, 10); + y = strtol(av[3], NULL, 10); + hex = HL_hex_xy(x, y); + center = HL_hexcenter(hex); + printf(format ? format : "%f %f\n", center.x, center.y); + } + else if (!strcmp(av[1], "polar")) { + /* angle, radius */ + struct HL_point pt; + double angle, dist; + + angle = strtod(av[2], NULL); + dist = strtod(av[3], NULL); + + pt = HL_polar(angle, dist, NULL); + printf(format ? format : "%f %f\n", pt.x, pt.y); + } + else if (!strcmp(av[1], "bin")) { + struct HL_hex hex; + /* angle, radius */ + double px, py; + px = strtod(av[2], NULL); + py = strtod(av[3], NULL); + + hex = HL_hexbin(scale, px, py); + printf(format ? format : "%d %d\n", hex.x, hex.y); + } + else if (!strcmp(av[1], "distance")) { + struct HL_hex from, to; + int x, y, n; + + n = sscanf(av[2], "%d:%d", &x, &y); + if (n != 2) { + n = atoi(av[2]); + x = n % 100; + y = n / 100; + } + from.x = x; + from.y = y; + + n = sscanf(av[3], "%d:%d", &x, &y); + if (n != 2) { + n = atoi(av[3]); + x = n % 100; + y = n / 100; + } + to.x = x; + to.y = y; + + n = HL_distance(from, to); + printf(format ? format : "%d\n", n); + } + + return 0; +}