sob@soma.UUCP (Stan Barber) (09/03/86)
This is a 2-d fractal program that has passed around on the net. I have attempted to make it work on the Masscomp. Modify it if you wish, but pass the changes along. Stan #! /bin/sh # This is a shell archive, meaning: # 1. Remove everything above the #! /bin/sh line. # 2. Save the resulting text in a file. # 3. Execute the file with /bin/sh (not csh) to create: # Makefile # README # general.c # gosper.c # init.c # koch.c # main.c # monkey.c # plot.c # defs.h # g.h # gen.h # pix.h # This archive created: Tue Sep 2 15:52:49 1986 export PATH; PATH=/bin:/usr/bin:$PATH echo shar: "extracting 'Makefile'" '(563 characters)' if test -f 'Makefile' then echo shar: "will not over-write existing file 'Makefile'" else sed 's/^ X//' << \SHAR_EOF > 'Makefile' XGENOBJ = init.o plot.o XFRACTOBJ = general.o koch.o monkey.o gosper.o X# LIB = -lm -lpixrect XLIB = -lm -lgp XCFLAGS = -O X Xfractals: main.o $(FRACTOBJ) $(GENOBJ) X cc -o fractals $(CFLAGS) main.o $(FRACTOBJ) $(GENOBJ) $(LIB) X Xkoch.o: koch.c gen.h g.h X cc $(CFLAGS) -c koch.c X Xmonkey.o: monkey.o gen.h g.h X cc $(CFLAGS) -c monkey.c X Xgosper.o: gosper.c gen.h g.h X cc $(CFLAGS) -c gosper.c X Xgeneral.o: general.c gen.h g.h X cc $(CFLAGS) -c general.c X Xplot.o: plot.c g.h pix.h X cc $(CFLAGS) -c plot.c X Xinit.o: init.c defs.h X cc $(CFLAGS) -c init.c X Xclean: X rm *.o fractals SHAR_EOF if test 563 -ne "`wc -c < 'Makefile'`" then echo shar: "error transmitting 'Makefile'" '(should have been 563 characters)' fi fi echo shar: "extracting 'README'" '(1270 characters)' if test -f 'README' then echo shar: "will not over-write existing file 'README'" else sed 's/^ X//' << \SHAR_EOF > 'README' X X X These files: X X defs.h random definitions X pix.h pixrect definitions X g.h few constants X gen.h generator structure X X general.c a general curve generator X gosper.c gosper curve generator X koch.c koch curve generator X monkey.c monkey curve generator (My favorite) X X plot.c plot a line relatively X main.c trivial driver for all the routines X init.c routines to setup screen under pixrect X X Comprise a collection of simple fractal curve generators. X They do not generate nifty 3D dragons or towers or archs, X but they are entertaining and probably good for tiling. X X All I ask is that you not (how incredibly unlikely) try X to sell these for profit. I can ofcourse assume no respon- X sibility for how these work out for you. They work fine for X me on a SUN (TM) workstation under 4.2 Unix (TM) using SUN- X core. [Now under pixrect ... -David Kovar] X X Jim Hutchison X X hutch@sdcsvax X X {ihnp4,ucbvax}!sdcsvax!hutch XADDITIONAL NOTE: XI have added modification to work on Masscomp displays. To use under Sun's again, Xadjust the Makefile as marked. XStan Barber (sob@rice.edu or siesmo!soma!sob) X SHAR_EOF if test 1270 -ne "`wc -c < 'README'`" then echo shar: "error transmitting 'README'" '(should have been 1270 characters)' fi fi echo shar: "extracting 'general.c'" '(676 characters)' if test -f 'general.c' then echo shar: "will not over-write existing file 'general.c'" else sed 's/^ X//' << \SHAR_EOF > 'general.c' X/* X * general() X * X * Draw a Generic Fractal curve. X * X * Author: Jim Hutchison (hutch@sdcsvax) X * (this is free, but credit me) X */ X X#ifdef sun X#include <usercore.h> X#endif X#include <math.h> X#include "g.h" X#include "gen.h" X Xgeneral(len,angle,min_len,scale,flip,shape,size) Xdouble len,angle,min_len,scale; Xint flip; XGEN *shape; Xint size; X{ Xregister int i; XGEN *gp; X X if (len > min_len) { /* draw generator */ X gp = shape + ((flip == 1)? 0 : size ); X for(i = 0 ; i < GEN_SIDES ; i++, gp += flip) X general(len/gp->div, X angle + gp->angle, X min_len, X scale * gp->scale, X gp->flip * flip); X X } else /* draw side */ X plot_line(len * scale, angle); X} SHAR_EOF if test 676 -ne "`wc -c < 'general.c'`" then echo shar: "error transmitting 'general.c'" '(should have been 676 characters)' fi fi echo shar: "extracting 'gosper.c'" '(1000 characters)' if test -f 'gosper.c' then echo shar: "will not over-write existing file 'gosper.c'" else sed 's/^ X//' << \SHAR_EOF > 'gosper.c' X/* X * Gosper() X * X * Draw a Gosper curve according to Dr. Mandelbrot. X * start with flip at 1 usually, but -1 is fine also. X * X * Author: Jim Hutchison (hutch@sdcsvax) X * (this is free, but credit me) X */ X X#ifdef sun X#include <usercore.h> X#endif X#include <math.h> X#include "g.h" X#include "gen.h" X X#ifdef GEN_SIDES X#undef GEN_SIDES X#endif X#define GEN_SIDES 7 X#define GEN_MAX GEN_SIDES - 1L Xstatic GEN generator[GEN_SIDES] = { X /* angle flip */ X X { 0.0, 1 }, X { PIO3, -1 }, X { PI, -1 }, X { (2.0 * PIO3), 1 }, X { 0.0, 1 }, X { 0.0, 1 }, X { (TWOPI - PIO3), -1 } X}; X Xgosper(len,angle,min_len,flip) Xdouble len,angle,min_len; Xint flip; X{ Xregister int i; Xregister GEN *gp; X X if (len > min_len) { /* draw generator */ X angle += (TWOPI - PIO6);/* tilt */ X gp = &generator[ (flip == 1)? 0 : GEN_MAX ]; X for(i = 0 ; i < GEN_SIDES ; i++, gp += flip) X gosper(len/2.0, angle + gp->angle, min_len, gp->flip * flip); X } X else /* draw side */ X plot_line(len, angle); X} SHAR_EOF if test 1000 -ne "`wc -c < 'gosper.c'`" then echo shar: "error transmitting 'gosper.c'" '(should have been 1000 characters)' fi fi echo shar: "extracting 'init.c'" '(2600 characters)' if test -f 'init.c' then echo shar: "will not over-write existing file 'init.c'" else sed 's/^ X//' << \SHAR_EOF > 'init.c' X#ifdef mc300 X#define MASSCOMP X#endif X#ifdef mc500 X#define MASSCOMP X#endif X#ifdef mc700 X#define MASSCOMP X#endif X#include <stdio.h> X#ifndef MASSCOMP X#include <pixrect/pixrect_hs.h> X#endif X#include <sys/ioctl.h> X#ifdef sun X#include <sun/fbio.h> X#endif X#include <sys/file.h> X X#include "defs.h" X X#ifndef MASSCOMP Xchar *PR_DEVICE = "/dev/fb"; /* for console frame buffer */ X Xstruct pixrect *px_screen; /* console screen pixrect */ X#else Xint gp_assigned; X#endif Xint SCREEN_WIDTH, SCREEN_HEIGHT; X X/* X * Set up the screen for the subsequent screen graphics mmanipulations. X * All operations are done with the pixrect facilities of the SUN X * workstation 1.1 release. X */ X Xinitpix() X{ X char *getenv(); X X if (init_screen()) X return(-1); X X#ifdef notdef X pr_rop(px_screen, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, PIX_SET, X (struct pixrect *)NULL, 0, 0); X#endif X return(0); X} X X X/* X * Initialize the pixrect from the whole screen. X */ X Xinit_screen() X{ X X#ifdef MASSCOMP X int hwconfig[18]; X if (gp_assigned == 0){ X mgiasngp(0,0); /* assign default graphics processor */ X mgigethc(18, hwconfig); X SCREEN_WIDTH = hwconfig[0]; X SCREEN_HEIGHT = hwconfig[1]; X gp_assigned++; X if ( SCREEN_WIDTH <= 0 || SCREEN_HEIGHT <= 0 ) { X fprintf(stderr, "Can't get the window size.\n"); X return(-1); X } X } X mgiclearpln(0, -1, 0); X mgihue(2); X#else X int fd; X if ( (fd = open(PR_DEVICE, O_RDWR)) < 0 ) { X fprintf(stderr, "Cannot open \"%s\": %s\n", PR_DEVICE, ERRSTR); X return(-1); X } X if ( getsize(fd) ) X return(-1); X close(fd); X X if ((px_screen = pr_open(PR_DEVICE)) == NULL) { X fputs("Cannot open ", stderr); X perror(PR_DEVICE); X return(-1); X } X X return(0); X#endif X} X X X#ifndef MASSCOMP X/* X * Get the number of pixels of the screen size. If the size X * is zero, we assume an error. X */ X Xgetsize(fd) X{ X struct fbtype fbt; X X if (ioctl(fd, FBIOGTYPE, (char *)&fbt) < 0) { X perror("FBIOGTYPE ioctl"); X return(-1); X } X X SCREEN_WIDTH = fbt.fb_width; X SCREEN_HEIGHT = fbt.fb_height; X X if ( SCREEN_WIDTH <= 0 || SCREEN_HEIGHT <= 0 ) { X fprintf(stderr, "Can't get the window size.\n"); X return(-1); X } X X return(0); X} X#endif X Xclosepix() X{ X#ifdef MASSCOMP X if (gp_assigned) mgideagp(); X#endif X return(0); X} echo shar: "a missing newline was added to 'init.c'" SHAR_EOF if test 2600 -ne "`wc -c < 'init.c'`" then echo shar: "error transmitting 'init.c'" '(should have been 2600 characters)' fi fi echo shar: "extracting 'koch.c'" '(940 characters)' if test -f 'koch.c' then echo shar: "will not over-write existing file 'koch.c'" else sed 's/^ X//' << \SHAR_EOF > 'koch.c' X/* X * koch() X * X * Draw a Koch lake according to Dr. Mandelbrot. X * X * Author: Jim Hutchison (hutch@sdcsvax) X * (this is free, but credit me) X */ X X#ifdef sun X#include <usercore.h> X#endif X#include <math.h> X#include "g.h" X#include "gen.h" X Xkoch(len,angle,min_len) Xdouble len,angle,min_len; X{ Xint i; X X for (i = 0 ; i < 4 ; i++, angle += PIO2) /* square seed */ X koch_side(len, angle, min_len); X} X X#ifdef GEN_SIDES X#undef GEN_SIDES X#endif X#define GEN_SIDES 8 X XGEN genkoch[GEN_SIDES] = { X /* Angle */ X { 0.0 }, X { PIO2 }, X { 0.0 }, X { - PIO2 }, X { - PIO2 }, X { 0.0 }, X { PIO2 }, X { 0.0 } X}; X X/* X * Draw a side recursively. X */ X Xkoch_side(len,angle,min_len) Xdouble len,angle,min_len; X{ Xregister int i; XGEN *gp; X X if (len > min_len) /* draw generator */ X for(i = 0, gp = genkoch ; i < GEN_SIDES ; i++, gp++) X koch_side(len/2.0, angle + gp->angle, min_len); X else /* draw side */ X plot_line(len, angle); X} SHAR_EOF if test 940 -ne "`wc -c < 'koch.c'`" then echo shar: "error transmitting 'koch.c'" '(should have been 940 characters)' fi fi echo shar: "extracting 'main.c'" '(1292 characters)' if test -f 'main.c' then echo shar: "will not over-write existing file 'main.c'" else sed 's/^ X//' << \SHAR_EOF > 'main.c' X#include <stdio.h> Xmain() X{ X int choice = 0; X double len, angle, min_len, scale; X int flip; X X while(choice < 1 || choice > 4) { X printf("Choose a fractal: \n"); X printf("\t1) Monkey \n"); X printf("\t2) Koch lakes \n"); X printf("\t3) General (not handled yet)\n"); X printf("\t4) Gosper\n"); X X printf("\nChoice? "); X scanf("%d", &choice); X } X X printf("All values should be doubles (xxx.x) save for 'flip', an int.\n"); X printf("Reasonable values for monkey are: 10.0 0.0 1.0 1.0 1 \n"); X Xswitch (choice) { X case 1: /* Monkey */ X printf ("Length, angle, min. length, scale, flip: "); X scanf ("%lf %lf %lf %lf %d", X &len, &angle, &min_len, &scale, &flip); X initpix(); /* initialize the graphics device */ X monkey (len, angle, min_len, scale, flip); X break; X case 2: /* Koch lakes */ X printf ("Length, angle, minimum length: "); X scanf ("%lf %lf %lf", &len, &angle, &min_len); X initpix(); X koch (len, angle, min_len); X break; X case 3: /* General. Not handled yet */ X printf ("Not handled.\n"); X break; X case 4: X printf ("Length, angle, minimum length, flip: "); X scanf ("%lf %lf %lf %", &len, &angle, &min_len, &flip); X initpix(); X gosper (len, angle, min_len, flip); X break; X} X closepix(); /* close the graphics device */ X} SHAR_EOF if test 1292 -ne "`wc -c < 'main.c'`" then echo shar: "error transmitting 'main.c'" '(should have been 1292 characters)' fi fi echo shar: "extracting 'monkey.c'" '(1281 characters)' if test -f 'monkey.c' then echo shar: "will not over-write existing file 'monkey.c'" else sed 's/^ X//' << \SHAR_EOF > 'monkey.c' X/* X * Monkey() X * X * Draw a Monkey curve according to Dr. Mandelbrot. X * X * Author: Jim Hutchison (hutch@sdcsvax) X * (this is free, but credit me) X */ X X#ifdef sun X#include <usercore.h> X#endif X#include <math.h> X#include "g.h" X#include "gen.h" X X#ifdef GEN_SIDES X#undef GEN_SIDES X#endif X#define GEN_SIDES 11 X#define GEN_MAX GEN_SIDES - 1 X XGEN genmonkey[GEN_SIDES] = { X /* Angle Flip Scale */ X X { PIO3, -1, 1.0 }, /* R */ X { PIO3, 1, 1.0 }, /* L */ X { 0.0, 1, 1.0 }, /* L */ X { (TWOPI - PIO3), 1, 1.0 }, /* L */ X X { (2.0 * PIO3 + PIO6), 1, 0.5 }, /* L */ X { (2.0 * PIO3 + PIO6), -1, 0.5 }, /* R */ X { (PI + PIO6), -1, 0.5 }, /* R */ X { (TWOPI - PIO2), -1, 0.5 }, /* R */ X { (TWOPI - PIO2), 1, 0.5 }, /* L */ X X { 0.0, -1, 1.0 }, /* R */ X { 0.0, 1, 1.0 } /* L */ X}; X X/* X * Draw a monkey curve X */ X Xmonkey(len,angle,min_len,scale,flip) Xdouble len,angle,min_len,scale; Xint flip; X{ Xregister int i; XGEN *gp; X X if (len > min_len) { /* draw generator */ X gp = &genmonkey[ (flip == 1)? 0 : GEN_MAX ]; X for(i = 0 ; i < GEN_SIDES ; i++, gp += flip) X monkey(len/2.0, X angle + gp->angle, X min_len, X scale * gp->scale, X gp->flip * flip); X X } else /* draw side */ X plot_line(len * scale, angle); X} SHAR_EOF if test 1281 -ne "`wc -c < 'monkey.c'`" then echo shar: "error transmitting 'monkey.c'" '(should have been 1281 characters)' fi fi echo shar: "extracting 'plot.c'" '(987 characters)' if test -f 'plot.c' then echo shar: "will not over-write existing file 'plot.c'" else sed 's/^ X//' << \SHAR_EOF > 'plot.c' X/* X * Plot_line() X * X * Draw a line by length and angle relative to current x,y X * X * Author: Jim Hutchison (hutch@sdcsvax) X * (this is free, but dis/credit me) X */ X#ifdef mc300 X#define MASSCOMP X#endif X#ifdef mc500 X#define MASSCOMP X#endif X#ifdef mc700 X#define MASSCOMP X#endif X#include <math.h> X#include "g.h" X#ifdef sun X#include "pix.h" X#endif X Xstatic double crntx = D_X, crnty = D_Y; /* or global for presetting */ X Xset_coord(x,y) Xdouble x,y; X{ X crntx = x; X crnty = y; X} X X/* X * Plot a line by length and angle from current position X */ X Xplot_line(length,angle) Xdouble length,angle; X{ Xdouble dx,dy; X X MOD(angle,TWOPI); X X dx = length * cos(angle); X dy = length * sin(angle); X X#ifdef MASSCOMP X mgil((int)(crntx * 10.0), (int)(crnty * 10.0), X (int)((crntx + dx) * 10.0), (int)((crnty + dy) * 10.0)); X#else X line((int)(crntx * 10.0), (int)(crnty * 10.0), X (int)((crntx + dx) * 10.0), (int)((crnty + dy) * 10.0), OP_WRITE); X#endif X crntx += dx; X crnty += dy; X} X X SHAR_EOF if test 987 -ne "`wc -c < 'plot.c'`" then echo shar: "error transmitting 'plot.c'" '(should have been 987 characters)' fi fi echo shar: "extracting 'defs.h'" '(530 characters)' if test -f 'defs.h' then echo shar: "will not over-write existing file 'defs.h'" else sed 's/^ X//' << \SHAR_EOF > 'defs.h' X#define LEFT 01 X#define RIGHT 02 X X#define MINXPOS 50 X#define MAXXPOS (SCREEN_WIDTH-50) X#define MINYPOS 100 X#define MAXYPOS (SCREEN_HEIGHT-100) X X#ifndef CTRL X# define CTRL(c) ('c' & 037) X#endif X X#define MAX(a,b) ((a) > (b) ? (a) : (b)) X#define ABS(d) ((d) < 0 ? -(d) : (d)) X Xextern int errno, sys_nerr; Xextern char *sys_errlist[]; X X#define ERRSTR (errno < sys_nerr ? sys_errlist[errno] : "Unknown Error") X X#define MEM_WIDTH 32 X#define MEM_HEIGHT 32 X SHAR_EOF if test 530 -ne "`wc -c < 'defs.h'`" then echo shar: "error transmitting 'defs.h'" '(should have been 530 characters)' fi fi echo shar: "extracting 'g.h'" '(471 characters)' if test -f 'g.h' then echo shar: "will not over-write existing file 'g.h'" else sed 's/^ X//' << \SHAR_EOF > 'g.h' X/* X * G.h X * X * Constants for convenience when doing radial fractal work under SUNCORE X * X * Author: Jim Hutchison (hutch@sdcsvax) X */ X X#define SQRT2 1.41421 X#define PI 3.1415926536 X#define PIO2 (PI / 2.0) X#define PIO3 (PI / 3.0) X#define PIO4 (PI / 4.0) X#define PIO6 (PI / 6.0) X#define TWOPIO3 (2.0 * PI / 3.0) X#define TWOPI (2.0 * PI) X#define D_X 0.0 X#define D_Y 0.0 X#define GEN_SIDES 10 X X#define MOD(a,b) while(a > b) a -= b /* sad but true */ SHAR_EOF if test 471 -ne "`wc -c < 'g.h'`" then echo shar: "error transmitting 'g.h'" '(should have been 471 characters)' fi fi echo shar: "extracting 'gen.h'" '(425 characters)' if test -f 'gen.h' then echo shar: "will not over-write existing file 'gen.h'" else sed 's/^ X//' << \SHAR_EOF > 'gen.h' X/* X * Author: Jim Hutchison (hutch@sdcsvax) X * Curve generators and initiators ( can be same structure ) X * Various and sundry generators and a very general one. X */ X Xtypedef struct gen { /* an initiator or generator */ X double angle; /* next turtle turn */ X int flip; /* Flip flop multiplier */ X double scale; /* scale factor for sides */ X double div; /* division factor for length */ X} GEN; SHAR_EOF if test 425 -ne "`wc -c < 'gen.h'`" then echo shar: "error transmitting 'gen.h'" '(should have been 425 characters)' fi fi echo shar: "extracting 'pix.h'" '(857 characters)' if test -f 'pix.h' then echo shar: "will not over-write existing file 'pix.h'" else sed 's/^ X//' << \SHAR_EOF > 'pix.h' X/* X * Basic operations on the screen as a whole pixrect. X */ X X#include <pixrect/pixrect_hs.h> X Xextern struct pixrect *px_screen; X X#define OP_CLEAR PIX_CLR X#define OP_SET PIX_SET X#define OP_PAINT (PIX_SRC|PIX_DST) X#define OP_WRITE PIX_SRC X#define OP_MASK (PIX_SRC&PIX_DST) X#define OP_ERASE (PIX_NOT(PIX_SRC)&PIX_DST) X#define OP_INVERT (PIX_NOT(PIX_SRC^PIX_DST)) X#define OP_NOT(op) PIX_NOT(op) X X#define WHITE CLEAR X#define BLACK SET X Xextern int SCREEN_WIDTH, SCREEN_HEIGHT; X X#define point(x,y,op) pr_put(px_screen, x, y, (op == PIX_CLR) ? 0 : 1) X#define line(x1,y1,x2,y2,op) pr_vector(px_screen, x1, y1, x2, y2, op, 0) X#define block(x,y,w,h,op) pr_rop(px_screen, x, y, w, h, op, \ X (struct pixrect *)0, 0, 0) SHAR_EOF if test 857 -ne "`wc -c < 'pix.h'`" then echo shar: "error transmitting 'pix.h'" '(should have been 857 characters)' fi fi exit 0 # End of shell archive