diff --git a/tortoise/.ccls-cache/@home@geir@code@scheme@tortoise/core.c.blob b/tortoise/.ccls-cache/@home@geir@code@scheme@tortoise/core.c.blob deleted file mode 100644 index fb206c0..0000000 Binary files a/tortoise/.ccls-cache/@home@geir@code@scheme@tortoise/core.c.blob and /dev/null differ diff --git a/tortoise/Makefile b/tortoise/Makefile new file mode 100644 index 0000000..ca16aa3 --- /dev/null +++ b/tortoise/Makefile @@ -0,0 +1,20 @@ +# Basic Makefile for the tortoise package. + +CFLAGS = +LIBS = + +.PHONY: clean build run + +build: tortoise + +clean: + rm -f tortoise tortoise.o + +run: tortoise + ./tortoise + +tortoise: tortoise.o + gcc $< -o $@ $(LIBS) + +tortoise.o: tortoise.c + gcc -c $< -o $@ $(CFLAGS) diff --git a/tortoise/core.c b/tortoise/core.c deleted file mode 100644 index 2c363f8..0000000 --- a/tortoise/core.c +++ /dev/null @@ -1,45 +0,0 @@ -/* Simple backend for a Logo like tortoise drawer. */ - -#include -#include -#include - -static const int WIDTH = 10; -static const int HEIGHT = 10; - -static FILE* -start_gnuplot() { - FILE* output; - int pipes[2]; - pid_t pid; - - pipe(pipes); - pid = fork(); - - if (!pid) { - dup2 (pipes[0], STDIN_FILENO); - execlp ("gnuplot", NULL); - return; /* Not reached. */ - } - - output = fdopen(pipes[1], "w"); - - fprintf (output, "set multiplot\n"); - fprintf (output, "set parametric\n"); - fprintf (output, "set xrange [-%d:%d]\n", WIDTH, WIDTH); - fprintf (output, "set yrange [-%d:%d]\n", HEIGHT, HEIGHT); - fprintf (output, "set size ratio -1\n"); - fprintf (output, "unset xtics\n"); - fprintf (output, "unset ytics\n"); - fflush (output); - - return output; - } - -static FILE* global_output; - -int main(int argc, char *argv[]) { - global_output = start_gnuplot(); - - return EXIT_SUCCESS; -} diff --git a/tortoise/tortoise b/tortoise/tortoise index d8b40fa..1358568 100755 Binary files a/tortoise/tortoise and b/tortoise/tortoise differ diff --git a/tortoise/.ccls-cache/@home@geir@code@scheme@tortoise/core.c b/tortoise/tortoise.c similarity index 53% rename from tortoise/.ccls-cache/@home@geir@code@scheme@tortoise/core.c rename to tortoise/tortoise.c index 2c363f8..985a762 100644 --- a/tortoise/.ccls-cache/@home@geir@code@scheme@tortoise/core.c +++ b/tortoise/tortoise.c @@ -3,9 +3,14 @@ #include #include #include +#include static const int WIDTH = 10; static const int HEIGHT = 10; +static double x, y; +static double direction; +static int pendown; + static FILE* start_gnuplot() { @@ -38,8 +43,46 @@ start_gnuplot() { static FILE* global_output; +static void tortoise_reset () { + x = y = 0.0; + direction = 0.0; + pendown = 1; + + fprintf(global_output, "clear\n"); + fflush(global_output); +} + + int main(int argc, char *argv[]) { global_output = start_gnuplot(); - + tortoise_reset (); return EXIT_SUCCESS; } + +static void draw_line(FILE *output, double x1, double y1, double x2, + double y2) { + fprintf(output, "plot [0:1] %f + %f * t, %f + %f * t notitle\n", + x1, x2 -x1, y1, y2 -y1); + fflush(output); +} + +static void tortoise_pendown() { + pendown = 1; +} + +static void tortoise_penup() { + pendown = 0; +} + +static void tortoise_move(double length) { + double newX, newY; + + newX = x + length * cos (direction); + newY = y + length * sin (direction); + + if (pendown) + draw_line(global_output, x, y, newX, newY); + + x = newX; + y = newY; +}