/******************************************************************* spiral.c -- started 8/27/96 from dps.c -- Eric R. Weeks v01: 8-27-96: started; -henslfF email: weeks@chaos.ph.utexas.edu web: http://chaos.ph.utexas.edu/~weeks/ideas/spiral.html This program is public domain, but please keep my name, email, and web address intact. *******************************************************************/ #include #include #include #define PI 3.14159265358979323846264338328 #define EE 2.71828182845904523536 /* ------ function declarations ------ */ void printheader(); void printtrailer(); void triang(float x1,float y1,float x2,float y2,float x3,float y3,int level, float pitch, int fill); float linewidth; float startred,startgre,startblu,endred,endgre,endblu; /* M M AAA IIIII N N * MM MM A A I NN N * M M M AAAAA I N N N ************************************ * M M A A I N NN * M M A A IIIII N N */ main(argc,argv) int argc; char **argv; { int c,numpts; extern int optind; extern char *optarg; double x1,x2,x3,x4,y1,y2,y3,y4,x5,y5,x6,y6,x7,y7; double red,green,blue; int t; float pitch; int level,fill,flip; float size; /* INPUT PROCESSING / POSTSCRIPT SETUP */ FILE *fp, *fopen(); /* ------ set up defaults ------ */ level = 50; pitch = 0.1; linewidth = 0.01; /* width of lines in cm */ size=8.0; /* size on page in cm */ flip = 0; /* don't flip */ fill = 0; /* don't fill */ startred = 1.0; startblu = 1.0; startgre = 1.0; endred = 0.0; endblu = 0.0; endgre = 0.0; while ((c=getopt(argc, argv, "hl:s:fn:e:Fr:g:b:R:G:B:")) != EOF) switch (c) { case 'h': fprintf(stderr,"Usage: %s [options]\n",argv[0]); fprintf(stderr,"Options:\n"); fprintf(stderr," -h : this help message\n"); fprintf(stderr," -l # : linewidth [%.3f] (negative=off)\n",linewidth); fprintf(stderr," -n # : recursion level [%d]\n",level); fprintf(stderr," -e # : tightness of spiral [%.2f]\n",pitch); fprintf(stderr," -f : fill\n"); fprintf(stderr," -s # : size of triangle side [%.1f]\n",size); fprintf(stderr," -F : flip orientation of alternating triangles\n"); fprintf(stderr," -rgb # : start color [1.0]\n"); fprintf(stderr," -RGB # : end color [0.0]\n"); fprintf(stderr,"\n"); fprintf(stderr,"-------------------------------------\n"); exit(1); break; case 'l': linewidth = atof(optarg); break; case 's': size = atof(optarg); break; case 'f': fill = 1; break; case 'n': level = atoi(optarg); break; case 'e': pitch = atof(optarg); break; case 'F': flip = 1; break; case 'r': startred = atof(optarg); break; case 'g': startgre = atof(optarg); break; case 'b': startblu = atof(optarg); break; case 'R': endred = atof(optarg); break; case 'G': endgre = atof(optarg); break; case 'B': endblu = atof(optarg); break; } printheader(); x1 = 6.0; y1 = 2.0; x2 = x1+size; y2 = 2.0; x3 = x1+size*0.5; y3 = 3.0+size*0.5*sqrt(3.0); triang(x1,y1,x2,y2,x3,y3,level,pitch,fill); x4 = x3+size; y4 = y3; if (flip==0) triang(x2,y2,x4,y4,x3,y3,level,pitch,fill); else triang(x4,y4,x2,y2,x3,y3,level,pitch,fill); x5 = x2; y5 = y3+size*0.5*sqrt(3.0); triang(x4,y4,x5,y5,x3,y3,level,pitch,fill); x6 = x1; y6 = y5; if (flip==0) triang(x5,y5,x6,y6,x3,y3,level,pitch,fill); else triang(x6,y6,x5,y5,x3,y3,level,pitch,fill); x7 = x3-size; y7 = y3; triang(x6,y6,x7,y7,x3,y3,level,pitch,fill); if (flip==0) triang(x7,y7,x1,y1,x3,y3,level,pitch,fill); else triang(x1,y1,x7,y7,x3,y3,level,pitch,fill); printtrailer(); exit(0); } /* END OF MAIN /* END OF MAIN /* END OF MAIN /* END OF MAIN /* END OF MAIN */ void triang(float x1,float y1,float x2,float y2,float x3,float y3,int level, float pitch, int fill) { float x4,y4,red,green,blue; int t; float shift; printf("%f %f m\n",x1,y1); printf("%f %f l\n",x2,y2); printf("%f %f l\n",x3,y3); printf("%f %f l\n",x1,y1); if (fill==0) { for (t=0;t0.0) printf("g "); /* gsave */ printf("%f %f %f sr i\n",red,green,blue); /* fill */ if (linewidth>0.0) printf("h\n"); /* grestore */ printf("a\n"); /* stroke */ shift = ((float)(level-t)/((float)(level))); shift = shift*shift*shift; red = shift*startred+(1.0-shift)*endred; green = shift*startgre+(1.0-shift)*endgre; blue = shift*startblu+(1.0-shift)*endblu; x3 = x2; y3 = y2; x2 = x1; y2 = y1; x1 = x4; y1 = y4; } } } /* VARIOUS POSTSCRIPT STUFF */ void printheader() { /* ================================================================ */ /* PostScript Header (originally taken from CGLE output, now much /* more condensed by me, after reading PostScript manual.) * ================================================================ */ printf("%%!PS-Adobe-1.0 %%%%BoundingBox: -1 -1 766.354 567.929 %%%%EndComments %%%%EndProlog gsave %% /f {findfont exch scalefont setfont} bind def /l {lineto} bind def /m {newpath moveto} bind def /sg {setgray} bind def /sr {setrgbcolor} bind def /a {stroke} bind def /cp {closepath} bind def /g {gsave} bind def /h {grestore} bind def /i {fill} bind def %% make a circle /c {0 360 arc closepath stroke} bind def matrix currentmatrix /originmat exch def /umatrix {originmat matrix concatmatrix setmatrix} def [28.3465 0 0 28.3465 10.5 100.0] umatrix 0 0 0 setrgbcolor %f setlinewidth ",linewidth); /* END OF HEADER =============================================== */ } void printtrailer() { printf("showpage grestore %%%%Trailer "); }