scheme/tortoise/tortoise.c

100 lines
1.9 KiB
C
Raw Normal View History

2024-08-18 09:35:26 +02:00
/* Simple backend for a Logo like tortoise drawer. */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
2024-08-18 11:09:05 +02:00
#include <math.h>
2024-08-18 09:35:26 +02:00
static const int WIDTH = 10;
static const int HEIGHT = 10;
2024-08-18 11:09:05 +02:00
static double x, y;
static double direction;
static int pendown;
2024-08-18 09:35:26 +02:00
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);
2024-08-18 11:55:13 +02:00
return 0; /* Not reached. */
2024-08-18 09:35:26 +02:00
}
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;
2024-08-18 11:09:05 +02:00
static void tortoise_reset () {
x = y = 0.0;
direction = 0.0;
pendown = 1;
fprintf(global_output, "clear\n");
fflush(global_output);
}
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;
}
2024-08-18 11:55:13 +02:00
static void tortoise_turn (double degrees)
{
direction += M_PI / 180.0 * degrees;
}
2024-08-18 11:09:05 +02:00
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;
}
2024-08-18 11:55:13 +02:00
int main(int argc, char *argv[]) {
global_output = start_gnuplot();
tortoise_reset ();
int i;
tortoise_pendown();
for (i = 1; i <= 4; ++i) {
tortoise_move(3.0);
tortoise_turn(90.0);
};
return EXIT_SUCCESS;
}