home *** CD-ROM | disk | FTP | other *** search
/ Rat's Nest 1 / ratsnest1.iso / prgmming / c / ca.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-19  |  5.6 KB  |  204 lines

  1. //
  2.  
  3. //+------------------------------------------------------------+
  4.  
  5. //+ Program Cellular_Automata (AUTOMATA.CPP)                   +
  6.  
  7. //+ By Ramiro Perez {RPEREZ@UTPVM1.BITNET}                     +
  8.  
  9. //+ and Fausto A. A. Barbuto {BJ06@C53000.PETROBRAS.ANRJ.BR}.  +
  10.  
  11. //+ C++ 3.1 programme's creator: Fausto A. A. Barbuto, 1993.   +
  12.  
  13. //+ After a TURBO BASIC program by Ramiro Perez, 1993.         +
  14.  
  15. //+ TRIANGLE and digital pattern added by Fausto Barbuto, 1993.+
  16.  
  17. //+                                                            +
  18.  
  19. //+ Requires VGA screen with 640 x 480 points, 16 colours.     +
  20.  
  21. //+ C++ and TURBO BASIC are trademarks of Borland Int., Inc.   +
  22.  
  23. //+------------------------------------------------------------+
  24.  
  25. //
  26.  
  27. #include <dos.h>
  28.  
  29. #include <graphics.h>
  30.  
  31. #include <stdlib.h>
  32.  
  33. #include <conio.h>
  34.  
  35. #include <stdio.h>
  36.  
  37.  
  38.  
  39. void Create_Cellular(int, int, int [53][53]);
  40.  
  41.  
  42.  
  43. void main()
  44.  
  45.  
  46.  
  47. {
  48.  
  49.    int i, j, k, u, t, a[53][53], b[53][53], c[53][53], poly[8];
  50.  
  51.    int ipal[15] = {45,5,33,1,9,11,19,26,22,54,38,36,32,4,8};
  52.  
  53.    int nx, ny, a1, b1, irand1, irand2, option;
  54.  
  55.  
  56.  
  57.    int graphdriver=DETECT, graphmode;
  58.  
  59.  
  60.  
  61.    clrscr();
  62.  
  63.    printf("\n Cellular Automata of four sides");
  64.  
  65.    printf("\n By Ramiro Perez & Fausto A. A. Barbuto, 1993");
  66.  
  67.    printf("\n States = 12");
  68.  
  69.    printf("\n Select initial pattern");
  70.  
  71.    printf("\n\n 1 - CIRCLES");
  72.  
  73.    printf("\n 2 - SQUARES");
  74.  
  75.    printf("\n 3 - CENTRAL POINT");
  76.  
  77.    printf("\n 4 - RAMDOM POINTS");
  78.  
  79.    printf("\n 5 - TRIANGLE");
  80.  
  81.    printf("\n <1 or >5 - SURPRISE!!!");
  82.  
  83.    printf("\n (Press any key to stop the execution)");
  84.  
  85.    printf("\n\n");
  86.  
  87.    scanf("%d",&option);
  88.  
  89.  
  90.  
  91.    clrscr();
  92.  
  93.    t = 210;
  94.  
  95.    u = 70;
  96.  
  97.  
  98.  
  99.    initgraph(&graphdriver, &graphmode, "c:\\borlandc\\bgi");
  100.  
  101.    cleardevice();
  102.  
  103.  
  104.  
  105. //
  106.  
  107. // Initialization of vectors a[][], b[][], c[][]
  108.  
  109. //
  110.  
  111.    for (i=0;i<=52;i++) {
  112.  
  113.       for (j=0;j<=52;j++) {
  114.  
  115.          a[i][j] = 0;
  116.  
  117.          b[i][j] = 0;
  118.  
  119.          c[i][j] = 0;
  120.  
  121.       }
  122.  
  123.    }
  124.  
  125.  
  126.  
  127.    for (i=0;i<=14;i++) {
  128.  
  129.      setpalette(i,ipal[i]);
  130.  
  131.    }
  132.  
  133.    setbkcolor(3);
  134.  
  135.  
  136.  
  137.    k = 0;
  138.  
  139. //
  140.  
  141. // The initial pattern in defined below.
  142.  
  143. //
  144.  
  145.    switch(option) {
  146.  
  147.       case 1:  /* Circle */
  148.  
  149.          for (i=12;i>=1;i--) {
  150.  
  151.             k++;
  152.  
  153.             setcolor(k);
  154.  
  155.             circle(26,26,i);
  156.  
  157.             setfillstyle(SOLID_FILL,k);
  158.  
  159.             floodfill(26,26,k);
  160.  
  161.          }
  162.  
  163.          break;
  164.  
  165.       case 2: /* Square */
  166.  
  167.          for (i=12;i>=1;i--) {
  168.  
  169.             k++;
  170.  
  171.             setcolor(k);
  172.  
  173.             rectangle (26-i, 26+i, 26+i, 26-i);
  174.  
  175.          }
  176.  
  177.          break;
  178.  
  179.       case 3: /* Single Central Point */
  180.  
  181.          putpixel(26,26,12);
  182.  
  183.          break;
  184.  
  185.       case 4: /* Ramdomized Points */
  186.  
  187.          randomize();
  188.  
  189.          for (i=1;i<=12;i++) {
  190.  
  191.             irand1 = (int)(1.4e-3*rand());
  192.  
  193.             irand2 = (int)(1.4e-3*rand());
  194.  
  195.             putpixel(irand1,irand2,i);
  196.  
  197.          }
  198.  
  199.          break;
  200.  
  201.       case 5: /* Triangle */
  202.  
  203.          for (i=12;i>=1;i--) {
  204.  
  205.             k++;
  206.  
  207.             setcolor(k);
  208.  
  209.             poly[0] = 26-i;
  210.  
  211.             poly[1] = poly[0];
  212.  
  213.             poly[2] = 26+i;
  214.  
  215.             poly[3] = poly[0];
  216.  
  217.             poly[4] = 26;
  218.  
  219.             poly[5] = poly[2];
  220.  
  221.             poly[6] = poly[0];
  222.  
  223.             poly[7] = poly[1];
  224.  
  225.             drawpoly(4,poly);
  226.  
  227.             setfillstyle(SOLID_FILL,k);
  228.  
  229.          }
  230.  
  231.          break;
  232.  
  233.       default: /* Surprise! */
  234.  
  235.          printf("\n %d",option);
  236.  
  237.          break;
  238.  
  239.    }
  240.  
  241.  
  242.  
  243.    for (nx=0;nx<=52;nx++) {
  244.  
  245.       for (ny=0;ny<=52;ny++) {
  246.  
  247.          k = getpixel(nx,ny);
  248.  
  249.          a[nx][ny] = k;
  250.  
  251.          c[nx][ny] = k;
  252.  
  253.       }
  254.  
  255.    }
  256.  
  257.  
  258.  
  259.    Create_Cellular(t,u,c);     /* 1st call of Create_Cellular */
  260.  
  261.  
  262.  
  263.    do {
  264.  
  265.      for (i=1;i<=51;i++) {
  266.  
  267.         for (j=1;j<=51;j++) {
  268.  
  269.            a1 = a[i][j-1] + a[i][j+1] + a[i-1][j] + a[i+1][j] + a[i][j];
  270.  
  271.            b[i][j] = a1 % 13;
  272.  
  273.            c[i][j] = b[i][j];
  274.  
  275.         }
  276.  
  277.      }
  278.  
  279.  
  280.  
  281.      Create_Cellular(t,u,c);   /* 2nd call of Create_Cellular */
  282.  
  283.  
  284.  
  285.      for (i=1;i<=51;i++) {
  286.  
  287.         for (j=1;j<=51;j++) {
  288.  
  289.            b1 = b[i][j-1] + b[i][j+1] + b[i-1][j] + b[i+1][j] + b[i][j];
  290.  
  291.            a[i][j] = b1 % 13;
  292.  
  293.            c[i][j] = a[i][j];
  294.  
  295.         }
  296.  
  297.      }
  298.  
  299.  
  300.  
  301.      Create_Cellular(t,u,c);   /* 3rd call of Create_Cellular */
  302.  
  303.  
  304.  
  305.    } while (!kbhit());         /* Escape */
  306.  
  307. //
  308.  
  309. // Sound effects and clean-up.
  310.  
  311. //
  312.  
  313.    sound(740);
  314.  
  315.    delay(600);
  316.  
  317.    nosound();
  318.  
  319.    closegraph();
  320.  
  321. }
  322.  
  323.  
  324.  
  325. void Create_Cellular(int t, int u, int c[53][53])
  326.  
  327.  
  328.  
  329. {
  330.  
  331.    int i, k, nx, nx1, nx2, ny, ny1, ny2, cx, cy, kcolorx, kcolory;
  332.  
  333.  
  334.  
  335.    setcolor(4);
  336.  
  337.    for (nx=0;nx<=51;nx++) {
  338.  
  339.       for (ny=0;ny<=51;ny++) {
  340.  
  341.          k = c[nx][ny];
  342.  
  343.          nx1 = 4*nx;
  344.  
  345.          ny1 = 4*ny;
  346.  
  347.          if (k !=0) {
  348.  
  349.            for (i=1;i<=k;i++) {
  350.  
  351.               nx2 = nx1 - i + t;
  352.  
  353.               ny2 = ny1 - i + u;
  354.  
  355.               setcolor(14);
  356.  
  357.               line (nx2, ny2+3, nx2+3, ny2+3);
  358.  
  359.               setcolor(13);
  360.  
  361.               line (nx2+3, ny2, nx2+3, ny2+3);
  362.  
  363.            }
  364.  
  365.            setcolor(k);
  366.  
  367.            cx = nx1-k+t+1;
  368.  
  369.            cy = ny1-k+u+1;
  370.  
  371.            rectangle (nx1-k+t, ny1-k+u, nx1+3-k+t, ny1+3-k+u);
  372.  
  373.            kcolorx = getpixel(nx1-k+t,ny1-k+u);
  374.  
  375.            setfillstyle(SOLID_FILL,k);
  376.  
  377.            floodfill(cx,cy,kcolorx);
  378.  
  379.          }
  380.  
  381.          else {
  382.  
  383.            setcolor(1);
  384.  
  385.            cx = nx1+t+1;
  386.  
  387.            cy = ny1+u+1;
  388.  
  389.            rectangle (nx1+t, ny1+u, nx1+3+t, ny1+3+u);
  390.  
  391.            kcolorx = getpixel(nx1-k+t,ny1-k+u);
  392.  
  393.            setfillstyle(SOLID_FILL,1);
  394.  
  395.            floodfill(cx,cy,kcolorx);
  396.  
  397.          }
  398.  
  399.       }
  400.  
  401.    }
  402.  
  403.    return;
  404.  
  405. }
  406.  
  407.