\Fractals
CA12.CPP
Path: unixg.ubc.ca!vanbc.wimsey.com!cyber2.cyberstore.ca!math.ohio-state.edu!howland.reston.ans.net!paladin.american.edu!auvm!C53000.PETROBRAS.ANRJ.BR!BJ06 Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU Newsgroups: bit.listserv.frac-l Return-Path: <@AUVM.AMERICAN.EDU,@VTBIT.CC.VT.EDU:FRAC-L@GITVM1.BITNET> X-Envelope-to: FRAC-L@GITVM1.BITNET X-VMS-To: @FRACTAL References: ANSP network HEPnet SPAN Bitnet Internet gateway Message-ID: <274949F3C0000748@fpsp.fapesp.br> Date: Wed, 5 Jan 1994 07:45:00 BDB Sender: "\"FRACTAL\" discussion list"Comments: @FPSP.FAPESP.BR - @FPSP.HEPNET - @BRFAPESP.BITNET - .BR gateway From: BJ06@C53000.PETROBRAS.ANRJ.BR Subject: AUTOMATA.CPP 1.2 (C++ 3.1 source code) Lines: 293 // //+-----------------------------------------------------------+ //+ Program Cellular_Automata v. 1.1 + //+ By Ramiro Perez {RPEREZ@UTPVM1.BITNET}, (Panama) + //+ and Fausto A. A. Barbuto {BJ06@C53000.PETROBRAS.ANRJ.BR}, + //+ (Brazil). + //+ C++ 3.1 programme's creator: Fausto A. A. Barbuto, 1993. + //+ After a TURBO BASIC program by Ramiro Perez, 1993 + //+ TRIANGLE and digital pattern added by Fausto, Dec. 1993. + //+ PENTAGON, HEXAGON and ELLIPSE added by Fausto and Ramiro, + //+ January 3rd, 1994. + //+ RECTANGLE and 4-POINT STAR added by Fausto, Jan. 4th 1993.+ //+-----------------------------------------------------------+ // #include #include #include #include #include void Create_Cellular(int, int, int [53][53]); void main() { int i, j, k, u, t, a[53][53], b[53][53], c[53][53], poly[18]; int ipal[15] = {45,5,33,1,9,11,19,26,22,54,38,36,32,4,8}; int nx, ny, a1, b1, irand1, irand2, option; float c_1 = 1.154700538, r; float p1 = 0.587785252, p2 = 0.809016994, p3 = 0.363271264; float p4 = 1.118033989; int graphdriver=DETECT, graphmode; clrscr(); printf("\n Cellular Automata of four sides"); printf("\n By Ramiro Perez & Fausto A. A. Barbuto, 1993"); printf("\n States = 12"); printf("\n Select initial pattern"); printf("\n\n 1 - CIRCLE 2 - SQUARE"); printf("\n 3 - CENTRAL POINT 4 - RANDOM POINTS"); printf("\n 5 - TRIANGLE 6 - HEXAGON"); printf("\n 7 - PENTAGON 8 - ELLIPSE"); printf("\n 9 - RECTANGLE 10 - STAR "); printf("\n <1 or >10 - SURPRISE!!!"); printf("\n\n (Press any key to stop execution)"); printf("\n\n"); scanf("%d",&option); clrscr(); t = 210; u = 70; initgraph(&graphdriver, &graphmode, "c:\\borlandc\\bgi"); cleardevice(); // // Initialization of vectors a[][], b[][], c[][]. // for (i=0;i<=52;i++) { for (j=0;j<=52;j++) { a[i][j] = 0; b[i][j] = 0; c[i][j] = 0; } } for (i=0;i<=14;i++) { setpalette(i,ipal[i]); } setbkcolor(3); k = 0; // // The initial pattern is defined here // switch(option) { case 1: /* Circle */ for (i=12;i>=1;i--) { k++; setcolor(k); circle(26,26,i); setfillstyle(SOLID_FILL,k); floodfill(26,26,k); } break; case 2: /* Square */ for (i=12;i>=1;i--) { k++; setcolor(k); rectangle (26-i, 26+i, 26+i, 26-i); } break; case 3: /* Single Central Point */ putpixel(26,26,12); break; case 4: /* Random Points */ randomize(); for (i=1;i<=12;i++) { irand1 = (int)(1.4e-3*rand()); irand2 = (int)(1.4e-3*rand()); putpixel(irand1,irand2,i); } break; case 5: /* Triangle */ for (i=12;i>=1;i--) { k++; setcolor(k); poly[0] = 26-i; poly[1] = poly[0]; poly[2] = 26+i; poly[3] = poly[0]; poly[4] = 26; poly[5] = poly[2]; poly[6] = poly[0]; poly[7] = poly[1]; drawpoly(4,poly); setfillstyle(SOLID_FILL,k); } break; case 6: /* Hexagon */ for (i=12;i>=1;i--) { k++; setcolor(k); r = c_1*i; poly[0] = 26; poly[1] = poly[0] - r; poly[2] = poly[0] + i; poly[3] = poly[0] - 0.5*r; poly[4] = poly[0] + i; poly[5] = poly[0] + 0.5*r; poly[6] = poly[0]; poly[7] = poly[0] + r; poly[8] = poly[0] - i; poly[9] = poly[5]; poly[10] = poly[8]; poly[11] = poly[3]; poly[12] = poly[0]; poly[13] = poly[1]; drawpoly(7,poly); setfillstyle(SOLID_FILL,k); } break; case 7: /* Pentagon */ for (i=12;i>=1;i--) { k++; setcolor(k); poly[0] = 26 - p1*i; poly[1] = 26 - p2*i; poly[2] = 26 + p1*i; poly[3] = poly[1]; poly[4] = 26 + (p1 + p3)*i; poly[5] = 26 + (p4 - p2)*i; poly[6] = 26; poly[7] = poly[6] + i; poly[8] = poly[0] - p3*i; poly[9] = poly[5]; poly[10] = poly[0]; poly[11] = poly[1]; drawpoly(6,poly); setfillstyle(SOLID_FILL,k); } break; case 8: /* Ellipse */ for (i=12;i>=1;i--) { k++; setcolor(k); fillellipse(26,26,i,0.5*i); setfillstyle(SOLID_FILL,k); } break; case 9: /* Rectangle */ for (i=12;i>=1;i--) { k++; setcolor(k); rectangle (26-1.4*i, 26+i, 26+1.4*i, 26-i); } break; case 10: /* 4-points Star */ for (i=12;i>=1;i--) { k++; setcolor(k); poly[0] = 26; poly[1] = poly[0] - i; poly[2] = poly[0] + 0.25*i; poly[3] = poly[0] - 0.25*i; poly[4] = poly[0] + i; poly[5] = poly[0]; poly[6] = poly[2]; poly[7] = poly[6]; poly[8] = poly[0]; poly[9] = poly[4]; poly[10] = poly[3]; poly[11] = poly[2]; poly[12] = poly[0] - i; poly[13] = poly[0]; poly[14] = poly[3]; poly[15] = poly[14]; poly[16] = poly[0]; poly[17] = poly[1]; drawpoly(9,poly); setfillstyle(SOLID_FILL,k); } break; default: /* Surprise! */ printf("\n %d",option); break; } for (nx=0;nx<=52;nx++) { for (ny=0;ny<=52;ny++) { k = getpixel(nx,ny); a[nx][ny] = k; c[nx][ny] = k; } } Create_Cellular(t,u,c); /* 1st call of Create_Cellular */ do { for (i=1;i<=51;i++) { for (j=1;j<=51;j++) { a1 = a[i][j-1] + a[i][j+1] + a[i-1][j] + a[i+1][j] + a[i][j]; b[i][j] = a1 % 13; c[i][j] = b[i][j]; } } Create_Cellular(t,u,c); /* 2nd call of Create_Cellular */ for (i=1;i<=51;i++) { for (j=1;j<=51;j++) { b1 = b[i][j-1] + b[i][j+1] + b[i-1][j] + b[i+1][j] + b[i][j]; a[i][j] = b1 % 13; c[i][j] = a[i][j]; } } Create_Cellular(t,u,c); /* 3rd call of Create_Cellular */ } while (!kbhit()); // // Sound effects and clean-up. // sound(740); delay(600); sound(370); delay(300); nosound(); closegraph(); } void Create_Cellular(int t, int u, int c[53][53]) { int i, k, nx, nx1, nx2, ny, ny1, ny2, cx, cy, kcolorx, kcolory; setcolor(4); for (nx=0;nx<=51;nx++) { for (ny=0;ny<=51;ny++) { k = c[nx][ny]; nx1 = 4*nx; ny1 = 4*ny; if (k !=0) { for (i=1;i<=k;i++) { nx2 = nx1 - i + t; ny2 = ny1 - i + u; setcolor(14); line (nx2, ny2+3, nx2+3, ny2+3); setcolor(13); line (nx2+3, ny2, nx2+3, ny2+3); } setcolor(k); cx = nx1-k+t+1; cy = ny1-k+u+1; rectangle (nx1-k+t, ny1-k+u, nx1+3-k+t, ny1+3-k+u); kcolorx = getpixel(nx1-k+t,ny1-k+u); setfillstyle(SOLID_FILL,k); floodfill(cx,cy,kcolorx); } else { setcolor(1); cx = nx1+t+1; cy = ny1+u+1; rectangle (nx1+t, ny1+u, nx1+3+t, ny1+3+u); kcolorx = getpixel(nx1-k+t,ny1-k+u); setfillstyle(SOLID_FILL,1); floodfill(cx,cy,kcolorx); } } } return; }