//+-------------------------------------------------------------------+ //+ Program SHADES3.CPP - By Fausto A. A. Barbuto, July 3, 1994 + //+ E-Mail: BJ06@C53000.PETROBRAS.ANRJ.BR + //+ Uses Michael Sargent's graphical functions and SVGA256.BGI driver + //+ by Jordan Powell Hargrave. + //+ + //+ Based on the formula z -> exp(i*Theta)*z*(1-z), + //+ where Theta is a fixed angle (c*Pi) and i = sqrt(-1). + //+ + //+ REFERENCE: Pickover, Clifford A.: "Computers, Pattern, Chaos and + //+ Beauty", St. Martin's Press, pp. 200-202, Sept. 1991. + //+ + //+ Press the '+' key at anytime to rotate palettes; any other key + //+ will fade the screen out. + //+-------------------------------------------------------------------+ #include #include #include #include #include #include "svga256.h" #include //*--Graphical routines by Michael Sargent, 1994.--*// void getpalette(void); void cycle(void); void fade(void); //*------------------------------------------------*// int Vid; char palette[256][3]; int huge DetectVGA256() { printf("\n Which video mode would you like to use? \n\n"); printf(" 0 - 320x200x256\n"); printf(" 1 - 640x400x256\n"); printf(" 2 - 640x480x256\n"); printf(" 3 - 800x600x256\n"); printf(" 4 - 1024x768x256\n\n> "); scanf("%d",&Vid); if ((Vid>4) || (Vid<0)) Vid = 2; return Vid; } void main(void) { float xmin=-1.0, xmax=2.0, ymin=-1.5, ymax=1.5, ST, CT, a, b; float x, y, r, deltax, deltay, Theta, Rz, Iz, Mult, Const, aa, bb; register int npix, npiy, k, nx, ny; long int maxiter; complex I; char ch; int graphdriver=DETECT, graphmode, ipen, iopt; clrscr(); printf("\n Program SHADES3\n"); printf(" [z -> exp(i*Theta)*z*(1-z) , i=sqrt(-1)]\n\n"); printf(" Enter a multiplier for the angle Pi=3.1415... radians\n"); printf(" (Suggested: 0.15, 0.25, 0.333..., -0.03457834, 0.95)\n\n > "); scanf("%f",&Mult); printf("\n Enter the maximum number of iterations\n"); printf(" (Suggested: 64 or higher)\n\n > "); scanf("%li",&maxiter); if (maxiter <= 0) maxiter = 100; printf("\n Inner shadings: \n"); printf(" Black to light gray: 1\n"); printf(" Light gray to black: 2\n\n > "); scanf("%d",&iopt); if ((iopt < 1) || (iopt > 2)) iopt = 1; clrscr(); installuserdriver("Svga256",DetectVGA256); initgraph(&graphdriver, &graphmode, "C:\\BORLANDC\\BGI"); if (Vid == 0) { npix = 320; npiy = 200;} if (Vid == 1) { npix = 640; npiy = 400;} if (Vid == 2) { npix = 640; npiy = 480;} if (Vid == 3) { npix = 800; npiy = 600;} if (Vid == 4) { npix =1024; npiy = 768;} I = complex(0.0,1.0); Const = 35.0; Theta = 3.1415926535897932*Mult; CT = real(exp(I*Theta)); ST = imag(exp(I*Theta)); deltax = (xmax-xmin)/(npix-1); deltay = (ymax-ymin)/(npiy-1); cleardevice(); for (nx=0; nx<=npix-1; nx++) { x = xmin + (float)nx*deltax; for (ny=0; ny<=npiy-1; ny++) { y = ymin + (float)ny*deltay; k = 0; aa = x; bb = y; do { a = aa - (aa+bb)*(aa-bb); b = bb*(1.0 - aa - aa); Rz = CT*a - ST*b; Iz = ST*a + CT*b; r = sqrt(Rz*Rz + Iz*Iz); k++; // // Escaped points. // if (r >= maxiter) putpixel(nx,ny,(29+k)); // // Internal points. // if (k == maxiter) { if (fabs(Mult) <= 0.05) Const = 100; if (fabs(Mult) <= 0.01) Const = 500; if (iopt == 1) { //* Black to light gray *// ipen = (29 - (int)(Const*sqrt(Rz*Rz + Iz*Iz))); if (ipen < 16) ipen = 0; } else if (iopt == 2) { //* Light gray to black *// ipen = (int)(16.0 + Const*sqrt(Rz*Rz + Iz*Iz)); if (ipen > 29) ipen = 29; } putpixel(nx,ny,ipen); } aa = Rz; bb = Iz; } while ((r<=maxiter) && (k<=maxiter)); } if(kbhit()) break; } ch = getche(); if (ch == '+') { //* Rotate palettes *// getpalette(); cycle(); } else fade(); closegraph(); } //+======================+ // Color-cycling routine + //+======================+ #pragma warn -eff void cycle(void) { int x=0, y, z; do { for (z=1; z<256; z++) { y=z+x; if (y > 255) y -= 255; outp(0x3C8, z); outp(0x3C9, palette[y][0]); outp(0x3C9, palette[y][1]); outp(0x3C9, palette[y][2]); } x += 1; if (x==255) x=0; delay(100); //* Originally, delay(50) *// } while (kbhit() == 0); getch(); } #pragma warn +eff //+=================+ // Fade-out routine + //==================+ #pragma warn -eff void fade(void) { int a, b, p1, p2, p3; for (a=0; a<64; a++) { for (b=0; b<256; b++) { outp(0x3C7, b); p1 = inp(0x3C9); p2 = inp(0x3C9); p3 = inp(0x3C9); outp (0x3C8, b); if (p1 > 0) outp(0x3C9, p1 - 1); else outp(0x3C9, 0); if (p2 > 0) outp(0x3C9, p2 - 1); else outp(0x3C9, 0); if (p3 > 0) outp(0x3C9, p3 - 1); else outp(0x3C9, 0); } delay(100); } } #pragma warn +eff //+============================================+ // Function to obtain current palette in array + //+============================================+ #pragma warn -eff void getpalette(void) { int p, a, b; for (a=0; a<255; a++) /* a<256 if last color ok */ { outp(0x3C7, a); /* Read values directly from */ for (b=0; b<3; b++) /* hardware port */ { p = inp(0x3C9); palette[a][b] = p; } } for (a=0; a<3; a++) /* Avoid white band if */ palette[255][a] = palette[254][a]; /* 255 == white */ } #pragma warn +eff