Advertisement
spectrumgomas

Tortoise tutorial

Dec 16th, 2011
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.08 KB | None | 0 0
  1. /* Simple backend for a Logo like tortoise drawer.  */
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <math.h>
  6. #include <libguile.h>
  7.  
  8. static const int WIDTH = 10;
  9. static const int HEIGHT = 10;
  10. static FILE* global_output;
  11. static double x = 0.0;
  12. static double y = 0.0;
  13. static double direction = 0.0;
  14. static int pendown = 1;
  15.  
  16. static FILE* start_gnuplot ();
  17. static void draw_line (FILE* output, double x1, double y1, double x2, double y2);
  18. static SCM tortoise_reset ();
  19. static SCM tortoise_pendown ();
  20. static SCM tortoise_penup ();
  21. static SCM tortoise_turn (SCM degrees);
  22. static SCM tortoise_move (SCM length);
  23. static void* register_functions (void* data);
  24.  
  25.  
  26. static FILE* start_gnuplot ()
  27. {
  28.   FILE* output;
  29.   int pipes[2];
  30.   pid_t pid;
  31.  
  32.   pipe (pipes);
  33.   pid = fork ();
  34.  
  35.   if (!pid)
  36.     {
  37.       dup2 (pipes[0], STDIN_FILENO);
  38.       execlp ("gnuplot", NULL);
  39.       return; /* Not reached.  */
  40.     }
  41.  
  42.   output = fdopen (pipes[1], "w");
  43.  
  44.   fprintf (output, "set multiplot\n");
  45.   fprintf (output, "set parametric\n");
  46.   fprintf (output, "set xrange [-%d:%d]\n", WIDTH, WIDTH);
  47.   fprintf (output, "set yrange [-%d:%d]\n", HEIGHT, HEIGHT);
  48.   fprintf (output, "set size ratio -1\n");
  49.   fprintf (output, "unset xtics\n");
  50.   fprintf (output, "unset ytics\n");
  51.   fflush (output);
  52.  
  53.   return output;
  54. }
  55.  
  56.  
  57.  
  58. int main (int argc, char* argv[])
  59. {
  60.   global_output = start_gnuplot ();
  61.  
  62.   scm_with_guile (&register_functions, NULL);
  63.   scm_shell (argc, argv);
  64.  
  65.   return EXIT_SUCCESS;
  66. }
  67.  
  68.  
  69.  
  70.  
  71. static void draw_line (FILE* output, double x1, double y1, double x2, double y2)
  72. {
  73.   fprintf (output, "plot [0:1] %f + %f * t, %f + %f * t notitle\n",
  74.            x1, x2 - x1, y1, y2 - y1);
  75.   fflush (output);
  76. }
  77.  
  78.  
  79. static SCM tortoise_reset ()
  80. {
  81.   x = y = 0.0;
  82.   direction = 0.0;
  83.   pendown = 1;
  84.  
  85.   fprintf (global_output, "clear\n");
  86.   fflush (global_output);
  87.  
  88.   return SCM_UNSPECIFIED;
  89. }
  90.  
  91. static SCM tortoise_pendown ()
  92. {
  93.   SCM result = scm_from_bool (pendown);
  94.   pendown = 1;
  95.   return result;
  96. }
  97.  
  98. static SCM tortoise_penup ()
  99. {
  100.   SCM result = scm_from_bool (pendown);
  101.   pendown = 0;
  102.   return result;
  103. }
  104.  
  105. static SCM tortoise_turn (SCM degrees)
  106. {
  107.   const double value = scm_to_double (degrees);
  108.   direction += M_PI / 180.0 * value;
  109.   return scm_from_double (direction * 180.0 / M_PI);
  110. }
  111.  
  112. static SCM tortoise_move (SCM length)
  113. {
  114.   const double value = scm_to_double (length);
  115.   double newX, newY;
  116.  
  117.   newX = x + value * cos (direction);
  118.   newY = y + value * sin (direction);
  119.  
  120.   if (pendown)
  121.     draw_line (global_output, x, y, newX, newY);
  122.  
  123.   x = newX;
  124.   y = newY;
  125.  
  126.   return scm_list_2 (scm_from_double (x), scm_from_double (y));
  127. }
  128.  
  129.  
  130. static void* register_functions (void* data)
  131. {
  132.   scm_c_define_gsubr ("tortoise-reset", 0, 0, 0, &tortoise_reset);
  133.   scm_c_define_gsubr ("tortoise-penup", 0, 0, 0, &tortoise_penup);
  134.   scm_c_define_gsubr ("tortoise-pendown", 0, 0, 0, &tortoise_pendown);
  135.   scm_c_define_gsubr ("tortoise-turn", 1, 0, 0, &tortoise_turn);
  136.   scm_c_define_gsubr ("tortoise-move", 1, 0, 0, &tortoise_move);
  137.   return NULL;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement