home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 250_01 / mandel.c < prev    next >
Text File  |  1987-10-29  |  5KB  |  180 lines

  1. /*
  2. CUG-HEADER:       ;
  3.    TITLE:         Mandelbrot set plotter;
  4.    DATE:          86-SEP-03;
  5.    DESCRIPTION:   "Plots fractals, using the Mandelbrot set (or parts of it),
  6.                    on an Epson MX-80 matrix printer";
  7.    KEYWORDS:      Graphics, bit image printing;
  8.    FILENAME:      mandel.c;
  9.    WARNINGS:      "Change the printer control sequences if your printer is
  10.                    not Epson like. Be prepared to do without your computer
  11.                    for hours if not days, if it has no hardware floating point
  12.                    processor."
  13.    AUTHORS:       E.H. Ramm (converted to C from VMS-FORTRAN);
  14.    O.S.:          CP/M-68K;
  15.    COMPILER(S):   DRI (Alcyon) C;
  16.  
  17. REFERENCES:
  18.       AUTHOR(S):  Benoit Mandelbrot;
  19.       TITLE:      "Fractals: Form, Chance, & Dimension";
  20.       CITATION:   "";
  21. ENDREF
  22. */
  23.  
  24. /* ------------------------------------------------------------------------- */
  25.  
  26. #include <stdio.h>
  27.  
  28. #define void     VOID
  29. #define LF       '\012'
  30. #define CR       '\015'
  31. #define FF       '\014'
  32. #define ESC      '\033'
  33. #define MAXLINES 67                           /* 67 * 8/72" + 3 lines header */
  34.  
  35.  
  36. /* ------------------------------------------------------------------------- */
  37.  
  38. extern char     *gets();
  39. extern int      atoi(),
  40.                 odd();
  41. extern double   atof();
  42. FILE            *fopenb(),
  43.                 *outfile;
  44.  
  45. /* ------------------------------------------------------------------------- */
  46.  
  47. main(argc, argv)
  48. int   argc;
  49. char  *argv[];
  50. {
  51.    register int   k,
  52.                   l,
  53.                   ix,
  54.                   iy;
  55.  
  56.    int      kmax,
  57.             xdots,
  58.             ydots,
  59.             n8;
  60.  
  61.    double   xmax,
  62.             xmin,
  63.             xq,
  64.             xc,
  65.             x,
  66.             ymax,
  67.             ymin,
  68.             yq,
  69.             yc,
  70.             y,
  71.             rqlim,
  72.             rq,
  73.             dx,
  74.             dy;
  75.  
  76.    static char  buf[961],
  77.                 conbuf[129];
  78.  
  79.  
  80.    if (argc != 2)
  81.    {
  82.       fprintf(stderr, "%s", "usage: mandel <picture file>");
  83.       exit(1);
  84.    }
  85.  
  86.    if ( !(outfile = fopenb(argv[1], "w")) )
  87.    {
  88.       fprintf(stderr, "%s%s", "unable to open ", argv[1]);
  89.       exit(1);
  90.    }
  91.    fputc(FF, stdout);                                        /* clear screen */
  92.    fprintf(stdout, "%s", "Xmax: ");
  93.    xmax = atof(gets(conbuf));
  94.    fprintf(stdout, "%s", "Xmin: ");
  95.    xmin = atof(gets(conbuf));
  96.    fprintf(stdout, "%s", "Ymax: ");
  97.    ymax = atof(gets(conbuf));
  98.    fprintf(stdout, "%s", "Ymin: ");
  99.    ymin = atof(gets(conbuf));
  100.    fprintf(stdout, "%s", "Limit: ");
  101.    rqlim = atof(gets(conbuf));
  102.    fprintf(stdout, "%s", "max. iterations: ");
  103.    kmax = atoi(gets(conbuf));
  104.  
  105.    xdots = 880;                                 /* # of horizontal dots/line */
  106.    n8 = xdots / 8;
  107.    xdots = n8 * 8;                    /* make sure xdots are a multiple of 8 */
  108.    ydots = MAXLINES * 8;
  109.    n8 = MAXLINES;
  110.  
  111.    dx = (xmax - xmin) / (double)xdots;
  112.    dy = (ymax - ymin) / (double)ydots;
  113.    yc = ymin;
  114.  
  115.    fprintf(outfile, "%c%c", ESC, '@');                      /* reset printer */
  116.    fprintf(outfile, "Mandelbrot Set\r\n");
  117.    fprintf(outfile, "%s%9.5f%s%9.5f%s", "X:", xmin, " to", xmax, ", ");
  118.    fprintf(outfile, "%s%9.5f%s%9.5f%s", "Y:", ymin, " to", ymax, ", ");
  119.    fprintf(outfile, "%s%5.1f%s", "Limit:", rqlim, ", ");
  120.    fprintf(outfile, "%s%3d\r\n\n", "itmax:", kmax);
  121.    fprintf(outfile, "%c%c%c", ESC, 'A', '\010');          /* 8/72" line feed */
  122.    fprintf(outfile, "%c%c%c", ESC, 'U', '\001');     /* unidirectional print */
  123.  
  124.    for (iy = 1; iy <= n8; iy++)
  125.    {
  126.       clrbuf(buf, xdots);
  127.       fprintf(stdout, "\r%c%s%3d%s%3d", CR, "Line", iy, " of", MAXLINES);
  128.  
  129.       for (l = 7; l >= 0; l--)
  130.       {
  131.          xc = xmin;
  132.          for (ix = 1; ix <= xdots; ix++)
  133.          {
  134.             x = xc;
  135.             y = yc;
  136.             k = 0;
  137.  
  138.             do
  139.             {
  140.                yq = y * y;              /* z**2 = (x+jy)**2 = x**2-y**2+2jxy */
  141.                xq = x * x;
  142.                y = x * (y + y) + yc;
  143.                x = xc + xq - yq;        /* (x,y) <-- (xc,yc)+(x**2-y**2,2xy) */
  144.                rq = xq + yq;
  145.                k++;
  146.             }
  147.             while ( (rq < rqlim) && (k < kmax) );
  148.  
  149.             if ( (rq >= rqlim) && (odd(k)) )
  150.                bitset(buf + ix, l);
  151.             xc += dx;
  152.          }
  153.          yc += dy;
  154.       }
  155.  
  156.       fputc(ESC, outfile);
  157.       fputc('L', outfile);          /* set bit image mode, max 960 dots/line */
  158.       fputc(xdots % 256, outfile);                     /* # of dots, lo byte */
  159.       fputc(xdots / 256, outfile);                     /* # of dots, hi byte */
  160.       for (ix = 1; ix <= xdots; ix++)
  161.          fputc(buf[ix], outfile);
  162.       fputc(CR, outfile);
  163.       fputc(LF, outfile);
  164.    }
  165.    fputc(FF, outfile);
  166.    fclose(outfile);
  167. }
  168.  
  169. /* ------------------------------------------------------------------------- */
  170.  
  171. void clrbuf(bufr, length)
  172. char   *bufr;
  173. int    length;
  174. {
  175.    while (length)
  176.       *(bufr + length--) = 0;
  177. }
  178.  
  179. /* ------------------------------------------------------------------------- */
  180.