home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo3.zoo / demo / misc / hilbert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-24  |  3.5 KB  |  188 lines

  1. /*                        Copyright (c) 1988 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: hilbert.c,v 4.3 88/05/31 13:22:43 bianchi Exp $
  9.     $Source: /tmp/mgrsrc/demo/misc/RCS/hilbert.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/demo/misc/RCS/hilbert.c,v $$Revision: 4.3 $";
  12.  
  13. #include <signal.h>
  14. #include "term.h"
  15.  
  16. /* program to draw hilbert space filling curves (very quick).
  17.  * Steve Hawley            11/87 (Macintosh C implementation)
  18.  * translated from a pascal version from the Oberlin Computer Science
  19.  * Lab manual, Fall 1984, Author unknown.
  20.  * --------- ported to mgr by SAU: FAST version uses fast vector mode
  21.  */
  22.  
  23. int dir;
  24.  
  25. /* global direction variable.  The original program used turtle graphics
  26.  * with turn functions taking angles.  I cheat since all turns in this
  27.  * program are 90 degrees, and make the directions be:
  28.  *        0: down
  29.  *        1: right
  30.  *        2: up
  31.  *        3: left
  32.  */
  33.  
  34. start()
  35. {
  36.     /* put the graphics cursor somewhere nice, and initialize
  37.      * the direction.
  38.      */
  39.  
  40.     m_go(10,10);
  41.  
  42.     dir = 0;
  43. }
  44.  
  45. left()
  46. {
  47.     /* a turn to the left is actually the direction + 3
  48.      * modulo 3.
  49.      */
  50.     dir = (dir + 3) & 0x3;
  51. }
  52.  
  53. right()
  54. {
  55.     /* a right turn is the direction + 1 modulo 3 */
  56.     dir = (dir + 1) & 0x3;
  57. }
  58.  
  59. forward(size)
  60. register int size;
  61. {
  62.     /* move the graphics cursor and draw a line segment.
  63.      * The Macintosh function Line(dh, dv) draws a line from the
  64.      * current graphics cursor to the graphics cursor displaced
  65.      * by dh and dv (horizontal and vertical deltas).
  66.      */
  67.     switch(dir) {
  68.     case 0:
  69.         Line(0, size);
  70.         break;
  71.     case 1:
  72.         Line(size, 0);
  73.         break;
  74.     case 2:
  75.         Line(0, -size);
  76.         break;
  77.     case 3:
  78.         Line(-size, 0);
  79.         break;
  80.     }
  81. }
  82.  
  83. /* mutually recursive hilbert functions: */
  84. lhilbert(size, level)
  85. register int size, level;
  86. {
  87.     if (level > 0) {
  88.         left();
  89.         rhilbert(size, level-1);
  90.         forward(size);
  91.         right();
  92.         lhilbert(size, level-1);
  93.         forward(size);
  94.         lhilbert(size, level-1);
  95.         right();
  96.         forward(size);
  97.         rhilbert(size, level-1);
  98.         left();
  99.     }
  100. }
  101.  
  102. rhilbert(size, level)
  103. register int size, level;
  104. {
  105.     if (level > 0) {
  106.         right();
  107.         lhilbert(size, level-1);
  108.         forward(size);
  109.         left();
  110.         rhilbert(size, level-1);
  111.         forward(size);
  112.         rhilbert(size, level-1);
  113.         left();
  114.         forward(size);
  115.         lhilbert(size, level-1);
  116.         right();
  117.     }
  118. }
  119.  
  120. main (argc,argv)
  121. int    argc;
  122. char    **argv;
  123. {
  124.     int clean();
  125.  
  126.     ckmgrterm( *argv );
  127.      m_setup(0);    
  128.     signal(SIGTERM,clean);
  129.     signal(SIGINT,clean);
  130.     signal(SIGHUP,clean);
  131.     system("stty litout");
  132.     m_func(B_SET);
  133.     /* initialize */
  134.     start();
  135.     m_clear();
  136.     /* draw the hilbert (this is *very* fast) */
  137.     rhilbert(8, 7);
  138.     clean();
  139. }
  140.  
  141. /* FAST drawing stuff */
  142.  
  143. #define SIZE    75            /* maximum # of points in a shot */
  144. #define MAX    7                /* maximum distance */
  145. static int count = 0;
  146. char buff[1024];    /* grunge buffer */
  147.  
  148. /* add delta to grunge list */
  149.  
  150. int
  151. Line(dx,dy)
  152. register int dx,dy;
  153.     {
  154.     register int mx,my;
  155.  
  156.     if (dx > MAX || dy > MAX) {
  157.         mx = (dx>>1);
  158.         my = (dy>>1);
  159.        buff[count++] = (mx+8)<<4 | (my+8);
  160.         dx = dx-mx;
  161.         dy = dy-my;
  162.         }
  163.  
  164.     buff[count++] = (dx+8)<<4 | (dy+8);
  165.     if (count >= SIZE)
  166.         flush();
  167.  
  168.     }
  169.  
  170. /* flush pending grunge data */
  171.  
  172. int
  173. flush()
  174.     {
  175.     if (count > 0) {
  176.         m_rfastdraw(count,buff);
  177.         count = 0;
  178.         }
  179.     }
  180.  
  181. clean()
  182.     {
  183.     flush();
  184.     m_flush();
  185.     system("stty -litout");
  186.     exit(0);
  187.     }
  188.