/* mandel.c */ /* Mandelbrot set plotter for Sparcstation. */ /* Probably works on other Sun workstations. */ /* To compile: cc mandel.c -o mandel -lpixrect */ #include #include /* fast low-level graphics */ float xmin, ymin, xmax, ymax; int ix, iy, ixmin, ixmax, iymin, iymax, maxiter; Pixrect *screen; intro() { puts("Mandelbrot set plotter -- M. Covington 1989\n"); puts("Stepping 10 colors at a time on a 256-color wheel\n"); } initialize() { char buffer[20]; puts("Minimum value of x (we suggest -2.0): "); scanf("%f",&xmin); puts("Maximum value of x (we suggest 2.0): "); scanf("%f",&xmax); puts("Minimum value of y (we suggest -2.0): "); scanf("%f",&ymin); puts("Maximum value of y (we suggest 2.0): "); scanf("%f",&ymax); puts("Maximum number of iterations (perhaps 30): "); scanf("%i",&maxiter); } cie_colormap(pr) Pixrect *pr; /* * M. Covington 1989 * * Sets the Pixrect colormap so that the colors make a circle * around the CIE chromaticity chart. This gives the maximum * number of distinguishable hues, but only one brightness in * each hue. * */ { unsigned char red[256], green[256], blue[256], i, r, g, b; /* Colors 0 and 1 are white and black respectively */ red[0] = green[0] = blue[0] = 255; red[1] = green[1] = blue[1] = 0; /* Colors 2-43: blue to cyan */ r = 0; g = 0; b = 252; for (i=2; i<=43; i++) { red[i] = r; green[i] = g; blue[i] = b; g = g + 6; } /* Colors 44-85: cyan to green */ for (i=44; i<=85; i++) { red[i] = r; green[i] = g; blue[i] = b; b = b - 6; } /* Colors 86-127: green to yellow */ for (i=86; i<=127; i++) { red[i] = r; green[i] = g; blue[i] = b; r = r + 6; } /* Colors 128-169: yellow to red */ for (i=128; i<=169; i++) { red[i] = r; green[i] = g; blue[i] = b; g = g - 6; } /* Colors 170-211: red to magenta */ for (i=170; i<=211; i++) { red[i] = r; green[i] = g; blue[i] = b; b = b + 6; } /* Colors 212-253: magenta back to blue */ for (i=212; i<=253; i++) { red[i] = r; green[i] = g; blue[i] = b; r = r - 6; } /* Last two colors are darker blues */ red[254] = 0; green[254] = 0; blue[254] = 196; red[255] = 0; green[255] = 0; blue[255] = 128; return(pr_putcolormap(pr,0,255,red,green,blue)); } /* kbhit.c Michael A. Covington (mcovingt@uga.bitnet) 1989 */ /* This program demonstrates how to wait for a keystroke under UNIX. */ /* The kbhit() function returns the number of bytes waiting to be read */ /* on standard input (or 32767, whichever is lower). If standard input */ /* is a keyboard, this will be the number of keystrokes waiting in the */ /* input buffer. */ /* kbhit() here will substitute for kbhit() under MS-DOS, since it is */ /* nonzero if there are keystrokes waiting. */ /* The kbhit() function substitutes for the same-named function */ /* under MS-DOS, in that it is nonzero if there are keystrokes waiting */ /* to be read from the keyboard. */ /* This works on a Sun Workstation. I got the idea from C-Kermit, which */ /* indicates that this technique is generally applicable to BSD UNIX */ /* but not necessarily other UNIXes. */ /* Note! Any message such as "Press a key..." should end with a newline */ /* so the buffer will be flushed and the user will see the message. */ /* Note! With the keyboard in normal (not "raw") mode, kbhit() will not */ /* know you have hit any keys until you press Return. */ #include #include int kbhit() /* Returns number of characters waiting on standard input, */ /* or 0 if none. Works correctly with keyboard i/o. */ { long n; int x; x = ioctl(0,FIONREAD,&n); if (x<0) { return(0); } else if (n>(long)32767) { return(32767); } else { return((int)n); } } opengraphics() { /* Run at low priority so other users won't be affected */ nice(20); /* Initialize graphics */ screen = pr_open("/dev/fb"); ixmin = 0; ixmax = screen->pr_size.x; iymin = 0; iymax = screen->pr_size.y; cie_colormap(screen); } closegraphics() { ; /* No action needed when using only Pixrect */ } double xfor(ix) int ix; { return((((double)(ix-ixmin))/((double)(ixmax-ixmin)))*(xmax - xmin) + xmin ); } double yfor(iy) int iy; { return((((double)(iy-iymin))/((double)(iymax-iymin)))*(ymax - ymin) + ymin ); } int iterations(ix,iy) int ix, iy; { double x, y, qx, qy, temp; int count = 0; qx = x = xfor(ix); qy = y = yfor(iy); while ((count < maxiter) && ((qx*qx + qy*qy) < 4)) { count++; temp = 2.0*qx*qy + y; qx = qx*qx - qy*qy + x; qy = temp; } return(count); } plotfor(ix,iy) int ix, iy; { pr_put(screen, ix, iy, (1+10*iterations(ix,iy)) % 256 ); } main() { char buffer[20]; intro(); initialize(); opengraphics(); for (ix=0; ix<=ixmax; ix++) for (iy=0; iy