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

  1. //
  2.  
  3. //+-----------------------------------------------------------+
  4.  
  5. //+ Program Cellular_Automata v. 1.1                          +
  6.  
  7. //+ By Ramiro Perez {RPEREZ@UTPVM1.BITNET}, (Panama)          +
  8.  
  9. //+ and Fausto A. A. Barbuto {BJ06@C53000.PETROBRAS.ANRJ.BR}, +
  10.  
  11. //+ (Brazil).                                                 +
  12.  
  13. //+ C++ 3.1 programme's creator: Fausto A. A. Barbuto, 1993.  +
  14.  
  15. //+ After a TURBO BASIC program by Ramiro Perez, 1993         +
  16.  
  17. //+ TRIANGLE and digital pattern added by Fausto, Dec. 1993.  +
  18.  
  19. //+ PENTAGON, HEXAGON and ELLIPSE added by Fausto and Ramiro, +
  20.  
  21. //+ January 3rd, 1994.                                        +
  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[14];
  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.    float c_1 = 1.154700538, r;
  56.  
  57.    float p1 = 0.587785252, p2 = 0.809016994, p3 = 0.363271264;
  58.  
  59.    float p4 = 1.118033989;
  60.  
  61.  
  62.  
  63.    int graphdriver=DETECT, graphmode;
  64.  
  65.  
  66.  
  67.    clrscr();
  68.  
  69.    printf("\n Cellular Automata of four sides");
  70.  
  71.    printf("\n By Ramiro Perez & Fausto A. A. Barbuto, 1993");
  72.  
  73.    printf("\n States = 12");
  74.  
  75.    printf("\n Select initial pattern");
  76.  
  77.    printf("\n\n 1 - CIRCLE               2 - SQUARE");
  78.  
  79.    printf("\n 3 - CENTRAL POINT        4 - RANDOM POINTS");
  80.  
  81.    printf("\n 5 - TRIANGLE             6 - HEXAGON");
  82.  
  83.    printf("\n 7 - PENTAGON             8 - ELLIPSE");
  84.  
  85.    printf("\n <1 or >8 - SURPRISE!!!");
  86.  
  87.    printf("\n\n (Press any key to stop execution)");
  88.  
  89.    printf("\n\n");
  90.  
  91.    scanf("%d",&option);
  92.  
  93.  
  94.  
  95.    clrscr();
  96.  
  97.    t = 210;
  98.  
  99.    u = 70;
  100.  
  101.  
  102.  
  103.    initgraph(&graphdriver, &graphmode, "c:\\borlandc\\bgi");
  104.  
  105.    cleardevice();
  106.  
  107.  
  108.  
  109. //
  110.  
  111. // Initialization of vectors a[][], b[][], c[][].
  112.  
  113. //
  114.  
  115.  
  116.  
  117.    for (i=0;i<=52;i++) {
  118.  
  119.       for (j=0;j<=52;j++) {
  120.  
  121.          a[i][j] = 0;
  122.  
  123.          b[i][j] = 0;
  124.  
  125.          c[i][j] = 0;
  126.  
  127.       }
  128.  
  129.    }
  130.  
  131.  
  132.  
  133.    for (i=0;i<=14;i++) {
  134.  
  135.      setpalette(i,ipal[i]);
  136.  
  137.    }
  138.  
  139.    setbkcolor(3);
  140.  
  141.  
  142.  
  143.    k = 0;
  144.  
  145. //
  146.  
  147. // The initial pattern is defined here
  148.  
  149. //
  150.  
  151.    switch(option) {
  152.  
  153.       case 1:  /* Circle */
  154.  
  155.          for (i=12;i>=1;i--) {
  156.  
  157.             k++;
  158.  
  159.             setcolor(k);
  160.  
  161.             circle(26,26,i);
  162.  
  163.             setfillstyle(SOLID_FILL,k);
  164.  
  165.             floodfill(26,26,k);
  166.  
  167.          }
  168.  
  169.          break;
  170.  
  171.       case 2: /* Square */
  172.  
  173.          for (i=12;i>=1;i--) {
  174.  
  175.             k++;
  176.  
  177.             setcolor(k);
  178.  
  179.             rectangle (26-i, 26+i, 26+i, 26-i);
  180.  
  181.          }
  182.  
  183.          break;
  184.  
  185.       case 3: /* Single Central Point */
  186.  
  187.          putpixel(26,26,12);
  188.  
  189.          break;
  190.  
  191.       case 4: /* Random Points */
  192.  
  193.          randomize();
  194.  
  195.          for (i=1;i<=12;i++) {
  196.  
  197.             irand1 = (int)(1.4e-3*rand());
  198.  
  199.             irand2 = (int)(1.4e-3*rand());
  200.  
  201.             putpixel(irand1,irand2,i);
  202.  
  203.          }
  204.  
  205.          break;
  206.  
  207.       case 5: /* Triangle */
  208.  
  209.          for (i=12;i>=1;i--) {
  210.  
  211.             k++;
  212.  
  213.             setcolor(k);
  214.  
  215.             poly[0] = 26-i;
  216.  
  217.             poly[1] = poly[0];
  218.  
  219.             poly[2] = 26+i;
  220.  
  221.             poly[3] = poly[0];
  222.  
  223.             poly[4] = 26;
  224.  
  225.             poly[5] = poly[2];
  226.  
  227.             poly[6] = poly[0];
  228.  
  229.             poly[7] = poly[1];
  230.  
  231.             drawpoly(4,poly);
  232.  
  233.             setfillstyle(SOLID_FILL,k);
  234.  
  235.          }
  236.  
  237.          break;
  238.  
  239.       case 6: /* Hexagon */
  240.  
  241.          for (i=12;i>=1;i--) {
  242.  
  243.             k++;
  244.  
  245.             setcolor(k);
  246.  
  247.             r = c_1*i;
  248.  
  249.             poly[0] = 26;
  250.  
  251.             poly[1] = poly[0] - r;
  252.  
  253.             poly[2] = poly[0] + i;
  254.  
  255.             poly[3] = poly[0] - 0.5*r;
  256.  
  257.             poly[4] = poly[0] + i;
  258.  
  259.             poly[5] = poly[0] + 0.5*r;
  260.  
  261.             poly[6] = poly[0];
  262.  
  263.             poly[7] = poly[0] + r;
  264.  
  265.             poly[8] = poly[0] - i;
  266.  
  267.             poly[9] = poly[5];
  268.  
  269.             poly[10] = poly[8];
  270.  
  271.             poly[11] = poly[3];
  272.  
  273.             poly[12] = poly[0];
  274.  
  275.             poly[13] = poly[1];
  276.  
  277.             drawpoly(7,poly);
  278.  
  279.             setfillstyle(SOLID_FILL,k);
  280.  
  281.          }
  282.  
  283.          break;
  284.  
  285.       case 7: /* Pentagon */
  286.  
  287.          for (i=12;i>=1;i--) {
  288.  
  289.             k++;
  290.  
  291.             setcolor(k);
  292.  
  293.             poly[0] = 26 - p1*i;
  294.  
  295.             poly[1] = 26 - p2*i;
  296.  
  297.             poly[2] = 26 + p1*i;
  298.  
  299.             poly[3] = poly[1];
  300.  
  301.             poly[4] = 26 + (p1 + p3)*i;
  302.  
  303.             poly[5] = 26 + (p4 - p2)*i;
  304.  
  305.             poly[6] = 26;
  306.  
  307.             poly[7] = poly[6] + i;
  308.  
  309.             poly[8] = poly[0] - p3*i;
  310.  
  311.             poly[9] = poly[5];
  312.  
  313.             poly[10] = poly[0];
  314.  
  315.             poly[11] = poly[1];
  316.  
  317.             drawpoly(6,poly);
  318.  
  319.             setfillstyle(SOLID_FILL,k);
  320.  
  321.          }
  322.  
  323.          break;
  324.  
  325.       case 8: /* Ellipse */
  326.  
  327.          for (i=12;i>=1;i--) {
  328.  
  329.             k++;
  330.  
  331.             setcolor(k);
  332.  
  333.             fillellipse(26,26,i,0.5*i);
  334.  
  335.             setfillstyle(SOLID_FILL,k);
  336.  
  337.          }
  338.  
  339.          break;
  340.  
  341.       default: /* Surprise! */
  342.  
  343.          printf("\n %d",option);
  344.  
  345.          break;
  346.  
  347.    }
  348.  
  349.  
  350.  
  351.    for (nx=0;nx<=52;nx++) {
  352.  
  353.       for (ny=0;ny<=52;ny++) {
  354.  
  355.          k = getpixel(nx,ny);
  356.  
  357.          a[nx][ny] = k;
  358.  
  359.          c[nx][ny] = k;
  360.  
  361.       }
  362.  
  363.    }
  364.  
  365.  
  366.  
  367.    Create_Cellular(t,u,c);     /* 1st call of Create_Cellular */
  368.  
  369.  
  370.  
  371.    do {
  372.  
  373.      for (i=1;i<=51;i++) {
  374.  
  375.         for (j=1;j<=51;j++) {
  376.  
  377.            a1 = a[i][j-1] + a[i][j+1] + a[i-1][j] + a[i+1][j] + a[i][j];
  378.  
  379.            b[i][j] = a1 % 13;
  380.  
  381.            c[i][j] = b[i][j];
  382.  
  383.         }
  384.  
  385.      }
  386.  
  387.  
  388.  
  389.      Create_Cellular(t,u,c);   /* 2nd call of Create_Cellular */
  390.  
  391.  
  392.  
  393.      for (i=1;i<=51;i++) {
  394.  
  395.         for (j=1;j<=51;j++) {
  396.  
  397.            b1 = b[i][j-1] + b[i][j+1] + b[i-1][j] + b[i+1][j] + b[i][j];
  398.  
  399.            a[i][j] = b1 % 13;
  400.  
  401.            c[i][j] = a[i][j];
  402.  
  403.         }
  404.  
  405.      }
  406.  
  407.  
  408.  
  409.      Create_Cellular(t,u,c);   /* 3rd call of Create_Cellular */
  410.  
  411.  
  412.  
  413.    } while (!kbhit());
  414.  
  415. //
  416.  
  417. // Sound effects and clean-up.
  418.  
  419. //
  420.  
  421.    sound(740);
  422.  
  423.    delay(600);
  424.  
  425.    sound(370);
  426.  
  427.    delay(300);
  428.  
  429.    nosound();
  430.  
  431.    closegraph();
  432.  
  433. }
  434.  
  435.  
  436.  
  437. void Create_Cellular(int t, int u, int c[53][53])
  438.  
  439.  
  440.  
  441. {
  442.  
  443.    int i, k, nx, nx1, nx2, ny, ny1, ny2, cx, cy, kcolorx, kcolory;
  444.  
  445.  
  446.  
  447.    setcolor(4);
  448.  
  449.    for (nx=0;nx<=51;nx++) {
  450.  
  451.       for (ny=0;ny<=51;ny++) {
  452.  
  453.          k = c[nx][ny];
  454.  
  455.          nx1 = 4*nx;
  456.  
  457.          ny1 = 4*ny;
  458.  
  459.          if (k !=0) {
  460.  
  461.            for (i=1;i<=k;i++) {
  462.  
  463.               nx2 = nx1 - i + t;                      ny2 = ny1 - i + u;
  464.  
  465.               setcolor(14);
  466.  
  467.               line (nx2, ny2+3, nx2+3, ny2+3);
  468.  
  469.               setcolor(13);
  470.  
  471.               line (nx2+3, ny2, nx2+3, ny2+3);
  472.  
  473.            }
  474.  
  475.            setcolor(k);
  476.  
  477.            cx = nx1-k+t+1;
  478.  
  479.            cy = ny1-k+u+1;
  480.  
  481.            rectangle (nx1-k+t, ny1-k+u, nx1+3-k+t, ny1+3-k+u);
  482.  
  483.            kcolorx = getpixel(nx1-k+t,ny1-k+u);
  484.  
  485.            setfillstyle(SOLID_FILL,k);
  486.  
  487.            floodfill(cx,cy,kcolorx);
  488.  
  489.          }
  490.  
  491.          else {
  492.  
  493.            setcolor(1);
  494.  
  495.            cx = nx1+t+1;
  496.  
  497.            cy = ny1+u+1;
  498.  
  499.            rectangle (nx1+t, ny1+u, nx1+3+t, ny1+3+u);
  500.  
  501.            kcolorx = getpixel(nx1-k+t,ny1-k+u);
  502.  
  503.            setfillstyle(SOLID_FILL,1);
  504.  
  505.            floodfill(cx,cy,kcolorx);
  506.  
  507.          }
  508.  
  509.       }
  510.  
  511.    }
  512.  
  513.    return;
  514.  
  515. }
  516.  
  517.