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

  1. 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
  2.  
  3. Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU
  4.  
  5. Newsgroups: bit.listserv.frac-l
  6.  
  7. Return-Path: <@AUVM.AMERICAN.EDU,@VTBIT.CC.VT.EDU:FRAC-L@GITVM1.BITNET>
  8.  
  9. X-Envelope-to: FRAC-L@GITVM1.BITNET
  10.  
  11. X-VMS-To: @FRACTAL
  12.  
  13. References: ANSP network   HEPnet SPAN Bitnet Internet gateway
  14.  
  15. Message-ID: <274949F3C0000748@fpsp.fapesp.br>
  16.  
  17. Date: Wed, 5 Jan 1994 07:45:00 BDB
  18.  
  19. Sender: "\"FRACTAL\" discussion list" <FRAC-L@GITVM1.BITNET>
  20.  
  21. Comments: @FPSP.FAPESP.BR - @FPSP.HEPNET - @BRFAPESP.BITNET - .BR gateway
  22.  
  23. From: BJ06@C53000.PETROBRAS.ANRJ.BR
  24.  
  25. Subject: AUTOMATA.CPP 1.2 (C++ 3.1 source code)
  26.  
  27. Lines: 293
  28.  
  29.  
  30.  
  31. //
  32.  
  33. //+-----------------------------------------------------------+
  34.  
  35. //+ Program Cellular_Automata v. 1.1                          +
  36.  
  37. //+ By Ramiro Perez {RPEREZ@UTPVM1.BITNET}, (Panama)          +
  38.  
  39. //+ and Fausto A. A. Barbuto {BJ06@C53000.PETROBRAS.ANRJ.BR}, +
  40.  
  41. //+ (Brazil).                                                 +
  42.  
  43. //+ C++ 3.1 programme's creator: Fausto A. A. Barbuto, 1993.  +
  44.  
  45. //+ After a TURBO BASIC program by Ramiro Perez, 1993         +
  46.  
  47. //+ TRIANGLE and digital pattern added by Fausto, Dec. 1993.  +
  48.  
  49. //+ PENTAGON, HEXAGON and ELLIPSE added by Fausto and Ramiro, +
  50.  
  51. //+ January 3rd, 1994.                                        +
  52.  
  53. //+ RECTANGLE and 4-POINT STAR added by Fausto, Jan. 4th 1993.+
  54.  
  55. //+-----------------------------------------------------------+
  56.  
  57. //
  58.  
  59. #include <dos.h>
  60.  
  61. #include <graphics.h>
  62.  
  63. #include <stdlib.h>
  64.  
  65. #include <conio.h>
  66.  
  67. #include <stdio.h>
  68.  
  69.  
  70.  
  71. void Create_Cellular(int, int, int [53][53]);
  72.  
  73.  
  74.  
  75. void main()
  76.  
  77.  
  78.  
  79. {
  80.  
  81.    int i, j, k, u, t, a[53][53], b[53][53], c[53][53], poly[18];
  82.  
  83.    int ipal[15] = {45,5,33,1,9,11,19,26,22,54,38,36,32,4,8};
  84.  
  85.    int nx, ny, a1, b1, irand1, irand2, option;
  86.  
  87.    float c_1 = 1.154700538, r;
  88.  
  89.    float p1 = 0.587785252, p2 = 0.809016994, p3 = 0.363271264;
  90.  
  91.    float p4 = 1.118033989;
  92.  
  93.  
  94.  
  95.    int graphdriver=DETECT, graphmode;
  96.  
  97.  
  98.  
  99.    clrscr();
  100.  
  101.    printf("\n Cellular Automata of four sides");
  102.  
  103.    printf("\n By Ramiro Perez & Fausto A. A. Barbuto, 1993");
  104.  
  105.    printf("\n States = 12");
  106.  
  107.    printf("\n Select initial pattern");
  108.  
  109.    printf("\n\n 1 - CIRCLE               2 - SQUARE");
  110.  
  111.    printf("\n 3 - CENTRAL POINT        4 - RANDOM POINTS");
  112.  
  113.    printf("\n 5 - TRIANGLE             6 - HEXAGON");
  114.  
  115.    printf("\n 7 - PENTAGON             8 - ELLIPSE");
  116.  
  117.    printf("\n 9 - RECTANGLE           10 - STAR   ");
  118.  
  119.    printf("\n <1 or >10 - SURPRISE!!!");
  120.  
  121.    printf("\n\n (Press any key to stop execution)");
  122.  
  123.    printf("\n\n");
  124.  
  125.    scanf("%d",&option);
  126.  
  127.  
  128.  
  129.    clrscr();
  130.  
  131.    t = 210;
  132.  
  133.    u = 70;
  134.  
  135.  
  136.  
  137.    initgraph(&graphdriver, &graphmode, "c:\\borlandc\\bgi");
  138.  
  139.    cleardevice();
  140.  
  141.  
  142.  
  143. //
  144.  
  145. // Initialization of vectors a[][], b[][], c[][].
  146.  
  147. //
  148.  
  149.  
  150.  
  151.    for (i=0;i<=52;i++) {
  152.  
  153.       for (j=0;j<=52;j++) {
  154.  
  155.          a[i][j] = 0;
  156.  
  157.          b[i][j] = 0;
  158.  
  159.          c[i][j] = 0;
  160.  
  161.       }
  162.  
  163.    }
  164.  
  165.  
  166.  
  167.    for (i=0;i<=14;i++) {
  168.  
  169.      setpalette(i,ipal[i]);
  170.  
  171.    }
  172.  
  173.    setbkcolor(3);
  174.  
  175.  
  176.  
  177.    k = 0;
  178.  
  179. //
  180.  
  181. // The initial pattern is defined here
  182.  
  183. //
  184.  
  185.    switch(option) {
  186.  
  187.       case 1:  /* Circle */
  188.  
  189.          for (i=12;i>=1;i--) {
  190.  
  191.             k++;
  192.  
  193.             setcolor(k);
  194.  
  195.             circle(26,26,i);
  196.  
  197.             setfillstyle(SOLID_FILL,k);
  198.  
  199.             floodfill(26,26,k);
  200.  
  201.          }
  202.  
  203.          break;
  204.  
  205.       case 2: /* Square */
  206.  
  207.          for (i=12;i>=1;i--) {
  208.  
  209.             k++;
  210.  
  211.             setcolor(k);
  212.  
  213.             rectangle (26-i, 26+i, 26+i, 26-i);
  214.  
  215.          }
  216.  
  217.          break;
  218.  
  219.       case 3: /* Single Central Point */
  220.  
  221.          putpixel(26,26,12);
  222.  
  223.          break;
  224.  
  225.       case 4: /* Random Points */
  226.  
  227.          randomize();
  228.  
  229.          for (i=1;i<=12;i++) {
  230.  
  231.             irand1 = (int)(1.4e-3*rand());
  232.  
  233.             irand2 = (int)(1.4e-3*rand());
  234.  
  235.             putpixel(irand1,irand2,i);
  236.  
  237.          }
  238.  
  239.          break;
  240.  
  241.       case 5: /* Triangle */
  242.  
  243.          for (i=12;i>=1;i--) {
  244.  
  245.             k++;
  246.  
  247.             setcolor(k);
  248.  
  249.             poly[0] = 26-i;
  250.  
  251.             poly[1] = poly[0];
  252.  
  253.             poly[2] = 26+i;
  254.  
  255.             poly[3] = poly[0];
  256.  
  257.             poly[4] = 26;
  258.  
  259.             poly[5] = poly[2];
  260.  
  261.             poly[6] = poly[0];
  262.  
  263.             poly[7] = poly[1];
  264.  
  265.             drawpoly(4,poly);
  266.  
  267.             setfillstyle(SOLID_FILL,k);
  268.  
  269.          }
  270.  
  271.          break;
  272.  
  273.       case 6: /* Hexagon */
  274.  
  275.          for (i=12;i>=1;i--) {
  276.  
  277.             k++;
  278.  
  279.             setcolor(k);
  280.  
  281.             r = c_1*i;
  282.  
  283.             poly[0] = 26;
  284.  
  285.             poly[1] = poly[0] - r;
  286.  
  287.             poly[2] = poly[0] + i;
  288.  
  289.             poly[3] = poly[0] - 0.5*r;
  290.  
  291.             poly[4] = poly[0] + i;
  292.  
  293.             poly[5] = poly[0] + 0.5*r;
  294.  
  295.             poly[6] = poly[0];
  296.  
  297.             poly[7] = poly[0] + r;
  298.  
  299.             poly[8] = poly[0] - i;
  300.  
  301.             poly[9] = poly[5];
  302.  
  303.             poly[10] = poly[8];
  304.  
  305.             poly[11] = poly[3];
  306.  
  307.             poly[12] = poly[0];
  308.  
  309.             poly[13] = poly[1];
  310.  
  311.             drawpoly(7,poly);
  312.  
  313.             setfillstyle(SOLID_FILL,k);
  314.  
  315.          }
  316.  
  317.          break;
  318.  
  319.       case 7: /* Pentagon */
  320.  
  321.          for (i=12;i>=1;i--) {
  322.  
  323.             k++;
  324.  
  325.             setcolor(k);
  326.  
  327.             poly[0] = 26 - p1*i;
  328.  
  329.             poly[1] = 26 - p2*i;
  330.  
  331.             poly[2] = 26 + p1*i;
  332.  
  333.             poly[3] = poly[1];
  334.  
  335.             poly[4] = 26 + (p1 + p3)*i;
  336.  
  337.             poly[5] = 26 + (p4 - p2)*i;
  338.  
  339.             poly[6] = 26;
  340.  
  341.             poly[7] = poly[6] + i;
  342.  
  343.             poly[8] = poly[0] - p3*i;
  344.  
  345.             poly[9] = poly[5];
  346.  
  347.             poly[10] = poly[0];
  348.  
  349.             poly[11] = poly[1];
  350.  
  351.             drawpoly(6,poly);
  352.  
  353.             setfillstyle(SOLID_FILL,k);
  354.  
  355.          }
  356.  
  357.          break;
  358.  
  359.       case 8: /* Ellipse */
  360.  
  361.          for (i=12;i>=1;i--) {
  362.  
  363.             k++;
  364.  
  365.             setcolor(k);
  366.  
  367.             fillellipse(26,26,i,0.5*i);
  368.  
  369.             setfillstyle(SOLID_FILL,k);
  370.  
  371.          }
  372.  
  373.          break;
  374.  
  375.       case 9: /* Rectangle */
  376.  
  377.          for (i=12;i>=1;i--) {
  378.  
  379.             k++;
  380.  
  381.             setcolor(k);
  382.  
  383.             rectangle (26-1.4*i, 26+i, 26+1.4*i, 26-i);
  384.  
  385.          }
  386.  
  387.          break;
  388.  
  389.       case 10: /* 4-points Star */
  390.  
  391.          for (i=12;i>=1;i--) {
  392.  
  393.             k++;
  394.  
  395.             setcolor(k);
  396.  
  397.             poly[0] = 26;
  398.  
  399.             poly[1] = poly[0] - i;
  400.  
  401.             poly[2] = poly[0] + 0.25*i;
  402.  
  403.             poly[3] = poly[0] - 0.25*i;
  404.  
  405.             poly[4] = poly[0] + i;
  406.  
  407.             poly[5] = poly[0];
  408.  
  409.             poly[6] = poly[2];
  410.  
  411.             poly[7] = poly[6];
  412.  
  413.             poly[8] = poly[0];
  414.  
  415.             poly[9] = poly[4];
  416.  
  417.             poly[10] = poly[3];
  418.  
  419.             poly[11] = poly[2];
  420.  
  421.             poly[12] = poly[0] - i;
  422.  
  423.             poly[13] = poly[0];
  424.  
  425.             poly[14] = poly[3];
  426.  
  427.             poly[15] = poly[14];
  428.  
  429.             poly[16] = poly[0];
  430.  
  431.             poly[17] = poly[1];
  432.  
  433.             drawpoly(9,poly);
  434.  
  435.             setfillstyle(SOLID_FILL,k);
  436.  
  437.          }
  438.  
  439.          break;
  440.  
  441.       default: /* Surprise! */
  442.  
  443.          printf("\n %d",option);
  444.  
  445.          break;
  446.  
  447.    }
  448.  
  449.  
  450.  
  451.    for (nx=0;nx<=52;nx++) {
  452.  
  453.       for (ny=0;ny<=52;ny++) {
  454.  
  455.          k = getpixel(nx,ny);
  456.  
  457.          a[nx][ny] = k;
  458.  
  459.          c[nx][ny] = k;
  460.  
  461.       }
  462.  
  463.    }
  464.  
  465.  
  466.  
  467.    Create_Cellular(t,u,c);     /* 1st call of Create_Cellular */
  468.  
  469.  
  470.  
  471.    do {
  472.  
  473.      for (i=1;i<=51;i++) {
  474.  
  475.         for (j=1;j<=51;j++) {
  476.  
  477.            a1 = a[i][j-1] + a[i][j+1] + a[i-1][j] + a[i+1][j] + a[i][j];
  478.  
  479.            b[i][j] = a1 % 13;
  480.  
  481.            c[i][j] = b[i][j];
  482.  
  483.         }
  484.  
  485.      }
  486.  
  487.  
  488.  
  489.      Create_Cellular(t,u,c);   /* 2nd call of Create_Cellular */
  490.  
  491.  
  492.  
  493.      for (i=1;i<=51;i++) {
  494.  
  495.         for (j=1;j<=51;j++) {
  496.  
  497.            b1 = b[i][j-1] + b[i][j+1] + b[i-1][j] + b[i+1][j] + b[i][j];
  498.  
  499.            a[i][j] = b1 % 13;
  500.  
  501.            c[i][j] = a[i][j];
  502.  
  503.         }
  504.  
  505.      }
  506.  
  507.  
  508.  
  509.      Create_Cellular(t,u,c);   /* 3rd call of Create_Cellular */
  510.  
  511.  
  512.  
  513.    } while (!kbhit());
  514.  
  515. //
  516.  
  517. // Sound effects and clean-up.
  518.  
  519. //
  520.  
  521.    sound(740);
  522.  
  523.    delay(600);
  524.  
  525.    sound(370);
  526.  
  527.    delay(300);
  528.  
  529.    nosound();
  530.  
  531.    closegraph();
  532.  
  533. }
  534.  
  535.  
  536.  
  537. void Create_Cellular(int t, int u, int c[53][53])
  538.  
  539.  
  540.  
  541. {
  542.  
  543.    int i, k, nx, nx1, nx2, ny, ny1, ny2, cx, cy, kcolorx, kcolory;
  544.  
  545.  
  546.  
  547.    setcolor(4);
  548.  
  549.    for (nx=0;nx<=51;nx++) {
  550.  
  551.       for (ny=0;ny<=51;ny++) {
  552.  
  553.          k = c[nx][ny];
  554.  
  555.          nx1 = 4*nx;
  556.  
  557.          ny1 = 4*ny;
  558.  
  559.          if (k !=0) {
  560.  
  561.            for (i=1;i<=k;i++) {
  562.  
  563.               nx2 = nx1 - i + t;
  564.  
  565.               ny2 = ny1 - i + u;
  566.  
  567.               setcolor(14);
  568.  
  569.               line (nx2, ny2+3, nx2+3, ny2+3);
  570.  
  571.               setcolor(13);
  572.  
  573.               line (nx2+3, ny2, nx2+3, ny2+3);
  574.  
  575.            }
  576.  
  577.            setcolor(k);
  578.  
  579.            cx = nx1-k+t+1;
  580.  
  581.            cy = ny1-k+u+1;
  582.  
  583.            rectangle (nx1-k+t, ny1-k+u, nx1+3-k+t, ny1+3-k+u);
  584.  
  585.            kcolorx = getpixel(nx1-k+t,ny1-k+u);
  586.  
  587.            setfillstyle(SOLID_FILL,k);
  588.  
  589.            floodfill(cx,cy,kcolorx);
  590.  
  591.          }
  592.  
  593.          else {
  594.  
  595.            setcolor(1);
  596.  
  597.            cx = nx1+t+1;
  598.  
  599.            cy = ny1+u+1;
  600.  
  601.            rectangle (nx1+t, ny1+u, nx1+3+t, ny1+3+u);
  602.  
  603.            kcolorx = getpixel(nx1-k+t,ny1-k+u);
  604.  
  605.            setfillstyle(SOLID_FILL,1);
  606.  
  607.            floodfill(cx,cy,kcolorx);
  608.  
  609.          }
  610.  
  611.       }
  612.  
  613.    }
  614.  
  615.    return;
  616.  
  617. }
  618.  
  619.