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

  1. Path: unixg.ubc.ca!vanbc.wimsey.com!cyber2.cyberstore.ca!math.ohio-state.edu!darwin.sura.net!nntp.msstate.edu!olivea!sgigate.sgi.com!sgiblab!swrinde!elroy.jpl.nasa.gov!usc!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: <20C0F76120000770@fpsp.fapesp.br>
  16.  
  17. Date: Mon, 24 Jan 1994 09:06: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: NEWAUTO.CPP (C++ 3.1 source code with ASCII characters)
  26.  
  27. Lines: 340
  28.  
  29.  
  30.  
  31.   Rio de Janeiro, Mon. 01/24/94
  32.  
  33.  
  34.  
  35.   FRAC-Lers:
  36.  
  37.  
  38.  
  39.      This is a new version of the Cellular Automats. Contains ASCII characters
  40.  
  41.   which may not be displayed at your terminals (prefer the UUENCODEd version
  42.  
  43.   posted earlier).
  44.  
  45.  
  46.  
  47.   Regards,
  48.  
  49.          Fausto.
  50.  
  51.  
  52.  
  53.   Fausto A. A. Barbuto,  BJ06@C53000.PETROBRAS.ANRJ.BR
  54.  
  55.   Rio de  Janeiro,  RJ,  Federal  Republic  of  Brazil
  56.  
  57.   FAX (55)(21)5986796 /5986441 - Phone (55)(21)5986706
  58.  
  59.   -----"Friends come and go;  enemies accumulate"-----
  60.  
  61.  
  62.  
  63. ---Program NEWAUTO.CPP---Begin---CUT HERE---------------
  64.  
  65. //
  66.  
  67. //+-----------------------------------------------------------+
  68.  
  69. //+ Program Cellular_Automata v. 1.3 (Pyramidal style)        +
  70.  
  71. //+ By Ramiro Perez {RPEREZ@UTPVM1.BITNET}, (Panama)          +
  72.  
  73. //+ and Fausto A. A. Barbuto {BJ06@C53000.PETROBRAS.ANRJ.BR}, +
  74.  
  75. //+ (Brazil).                                                 +
  76.  
  77. //+ C++ 3.1 programme's creator: Fausto A. A. Barbuto, 1994.  +
  78.  
  79. //+ After a TURBO BASIC program by Ramiro Perez, 1994         +
  80.  
  81. //+ OBS: Contains ASCII characters.                           +
  82.  
  83. //+-----------------------------------------------------------+
  84.  
  85. //
  86.  
  87. #include <dos.h>
  88.  
  89. #include <graphics.h>
  90.  
  91. #include <stdlib.h>
  92.  
  93. #include <conio.h>
  94.  
  95. #include <stdio.h>
  96.  
  97.  
  98.  
  99. void Create_Cellular(int, int, int [53][53]);
  100.  
  101. int  Rulegenerator(int,int,int,int,int,int[13]);
  102.  
  103.  
  104.  
  105. void main()
  106.  
  107. {
  108.  
  109.    int i, j, k, u, t, a[53][53], b[53][53], c[53][53], poly[18];
  110.  
  111.    int ipal[15] = {45,5,33,1,9,11,19,26,22,54,38,36,32,4,8};
  112.  
  113.    int nx, ny, a1, b1, irand1, irand2, option, n, w, s, cc, e;
  114.  
  115.    int wires[13] = {0,2,3,4,5,6,7,8,9,10,11,12,0};
  116.  
  117.    float c_1 = 1.154700538, r;
  118.  
  119.    float p1 = 0.587785252, p2 = 0.809016994, p3 = 0.363271264;
  120.  
  121.    float p4 = 1.118033989;
  122.  
  123.  
  124.  
  125.    int graphdriver=DETECT, graphmode;
  126.  
  127.  
  128.  
  129.    clrscr();
  130.  
  131.    printf("\n                  ┼╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬ê");
  132.  
  133.    printf("\n                  ╫    Cellular Automata of four sides     ╫");
  134.  
  135.    printf("\n                  ╫ By Ramiro Perez & Fausto Barbuto, 1994 ╫");
  136.  
  137.    printf("\n                  ╫ States = 12                            ╫");
  138.  
  139.    printf("\n                  ╫ Select a initial pattern:              ╫");
  140.  
  141.    printf("\n                  ╫                                        ╫");
  142.  
  143.    printf("\n                  ╫ 1 - CIRCLE               2 - SQUARE    ╫");
  144.  
  145.    printf("\n                  ╫ 3 - TRIANGLE             4 - HEXAGON   ╫");
  146.  
  147.    printf("\n                  ╫ 5 - PENTAGON             6 - ELLIPSE   ╫");
  148.  
  149.    printf("\n                  ╫ 7 - RECTANGLE            8 - STAR      ╫");
  150.  
  151.    printf("\n                  ╫ <1 or >8 - CIRCLE (default)            ╫");
  152.  
  153.    printf("\n                  »ûûûûûûûûûûûûûûûûûûûûûûûûûûûûûûûûûûûûûûûû▐");
  154.  
  155.    printf("\n                  ╫(Press any key & wait to stop execution)╫");
  156.  
  157.    printf("\n                  «╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬ö");
  158.  
  159.    printf("\n\n Option = ? ");
  160.  
  161.    scanf("%d",&option);
  162.  
  163.  
  164.  
  165.    clrscr();
  166.  
  167.    t = 210;
  168.  
  169.    u = 70;
  170.  
  171.  
  172.  
  173.    initgraph(&graphdriver, &graphmode, "c:\\borlandc\\bgi");
  174.  
  175.    cleardevice();
  176.  
  177.  
  178.  
  179. //
  180.  
  181. // Initialization of vectors a[][], b[][], c[][].
  182.  
  183. // Inicializa  o dos vetores a[][], b[][], c[][].
  184.  
  185. //
  186.  
  187.  
  188.  
  189.    for (i=0;i<=52;i++) {
  190.  
  191.       for (j=0;j<=52;j++) {
  192.  
  193.          a[i][j] = 0;
  194.  
  195.          b[i][j] = 0;
  196.  
  197.          c[i][j] = 0;
  198.  
  199.       }
  200.  
  201.    }
  202.  
  203.  
  204.  
  205.    for (i=0;i<=14;i++) {
  206.  
  207.      setpalette(i,ipal[i]);
  208.  
  209.    }
  210.  
  211.    setbkcolor(3);
  212.  
  213.  
  214.  
  215.    k = 0;
  216.  
  217. //
  218.  
  219. // The initial pattern is defined here
  220.  
  221. // O padr o inicial   definido aqui.
  222.  
  223. //
  224.  
  225.    switch(option) {
  226.  
  227.       case 1:  /* Circle, Cßrculo */
  228.  
  229.          for (i=12;i>=1;i--) {
  230.  
  231.             k++;
  232.  
  233.             setcolor(k);
  234.  
  235.             circle(26,26,i);
  236.  
  237.             setfillstyle(SOLID_FILL,k);
  238.  
  239.             floodfill(26,26,k);
  240.  
  241.          }
  242.  
  243.          break;
  244.  
  245.       case 2: /* Square, Quadrado */
  246.  
  247.          for (i=12;i>=1;i--) {
  248.  
  249.             k++;
  250.  
  251.             setcolor(k);
  252.  
  253.             rectangle (26-i, 26+i, 26+i, 26-i);
  254.  
  255.          }
  256.  
  257.          break;
  258.  
  259.       case 3: /* Triangle, Tri ngulo */
  260.  
  261.          for (i=12;i>=1;i--) {
  262.  
  263.             k++;
  264.  
  265.             setcolor(k);
  266.  
  267.             poly[0] = 26-i;
  268.  
  269.             poly[1] = poly[0];
  270.  
  271.             poly[2] = 26+i;
  272.  
  273.             poly[3] = poly[0];
  274.  
  275.             poly[4] = 26;
  276.  
  277.             poly[5] = poly[2];
  278.  
  279.             poly[6] = poly[0];
  280.  
  281.             poly[7] = poly[1];
  282.  
  283.             drawpoly(4,poly);
  284.  
  285.             setfillstyle(SOLID_FILL,k);
  286.  
  287.          }
  288.  
  289.          break;
  290.  
  291.       case 4: /* Hexagon, Hexªgono */
  292.  
  293.          for (i=12;i>=1;i--) {
  294.  
  295.             k++;
  296.  
  297.             setcolor(k);
  298.  
  299.             r = c_1*i;
  300.  
  301.             poly[0] = 26;
  302.  
  303.             poly[1] = poly[0] - r;
  304.  
  305.             poly[2] = poly[0] + i;
  306.  
  307.             poly[3] = poly[0] - 0.5*r;
  308.  
  309.             poly[4] = poly[0] + i;
  310.  
  311.             poly[5] = poly[0] + 0.5*r;
  312.  
  313.             poly[6] = poly[0];
  314.  
  315.             poly[7] = poly[0] + r;
  316.  
  317.             poly[8] = poly[0] - i;
  318.  
  319.             poly[9] = poly[5];
  320.  
  321.             poly[10] = poly[8];
  322.  
  323.             poly[11] = poly[3];
  324.  
  325.             poly[12] = poly[0];
  326.  
  327.             poly[13] = poly[1];
  328.  
  329.             drawpoly(7,poly);
  330.  
  331.             setfillstyle(SOLID_FILL,k);
  332.  
  333.          }
  334.  
  335.          break;
  336.  
  337.       case 5: /* Pentagon, Pentªgono */
  338.  
  339.          for (i=12;i>=1;i--) {
  340.  
  341.             k++;
  342.  
  343.             setcolor(k);
  344.  
  345.             poly[0] = 26 - p1*i;
  346.  
  347.             poly[1] = 26 - p2*i;
  348.  
  349.             poly[2] = 26 + p1*i;
  350.  
  351.             poly[3] = poly[1];
  352.  
  353.             poly[4] = 26 + (p1 + p3)*i;
  354.  
  355.             poly[5] = 26 + (p4 - p2)*i;
  356.  
  357.             poly[6] = 26;
  358.  
  359.             poly[7] = poly[6] + i;
  360.  
  361.             poly[8] = poly[0] - p3*i;
  362.  
  363.             poly[9] = poly[5];
  364.  
  365.             poly[10] = poly[0];
  366.  
  367.             poly[11] = poly[1];
  368.  
  369.             drawpoly(6,poly);
  370.  
  371.             setfillstyle(SOLID_FILL,k);
  372.  
  373.          }
  374.  
  375.          break;
  376.  
  377.       case 6: /* Ellipse, Elipse */
  378.  
  379.          for (i=12;i>=1;i--) {
  380.  
  381.             k++;
  382.  
  383.             setcolor(k);
  384.  
  385.             fillellipse(26,26,i,0.5*i);
  386.  
  387.             setfillstyle(SOLID_FILL,k);
  388.  
  389.          }
  390.  
  391.          break;
  392.  
  393.       case 7: /* Rectangle, Ret ngulo */
  394.  
  395.          for (i=12;i>=1;i--) {
  396.  
  397.             k++;
  398.  
  399.             setcolor(k);
  400.  
  401.             rectangle (26-1.4*i, 26+i, 26+1.4*i, 26-i);
  402.  
  403.          }
  404.  
  405.          break;
  406.  
  407.       case 8: /* 4-points Star, Estrela de quatro pontas */
  408.  
  409.          for (i=12;i>=1;i--) {
  410.  
  411.             k++;
  412.  
  413.             setcolor(k);
  414.  
  415.             poly[0] = 26;
  416.  
  417.             poly[1] = poly[0] - i;
  418.  
  419.             poly[2] = poly[0] + 0.25*i;
  420.  
  421.             poly[3] = poly[0] - 0.25*i;
  422.  
  423.             poly[4] = poly[0] + i;
  424.  
  425.             poly[5] = poly[0];
  426.  
  427.             poly[6] = poly[2];
  428.  
  429.             poly[7] = poly[6];
  430.  
  431.             poly[8] = poly[0];
  432.  
  433.             poly[9] = poly[4];
  434.  
  435.             poly[10] = poly[3];
  436.  
  437.             poly[11] = poly[2];
  438.  
  439.             poly[12] = poly[0] - i;
  440.  
  441.             poly[13] = poly[0];
  442.  
  443.             poly[14] = poly[3];
  444.  
  445.             poly[15] = poly[14];
  446.  
  447.             poly[16] = poly[0];
  448.  
  449.             poly[17] = poly[1];
  450.  
  451.             drawpoly(9,poly);
  452.  
  453.             setfillstyle(SOLID_FILL,k);
  454.  
  455.          }
  456.  
  457.          break;
  458.  
  459.       default: /* Circle , Circulo */
  460.  
  461.          for (i=12;i>=1;i--) {
  462.  
  463.             k++;
  464.  
  465.             setcolor(k);
  466.  
  467.             circle(26,26,i);
  468.  
  469.             setfillstyle(SOLID_FILL,k);
  470.  
  471.             floodfill(26,26,k);
  472.  
  473.          }
  474.  
  475.          break;
  476.  
  477.    }
  478.  
  479.  
  480.  
  481.    for (nx=0;nx<=52;nx++) {
  482.  
  483.       for (ny=0;ny<=52;ny++) {
  484.  
  485.          k = getpixel(nx,ny);
  486.  
  487.          a[nx][ny] = k;
  488.  
  489.          c[nx][ny] = k;
  490.  
  491.       }
  492.  
  493.    }
  494.  
  495.  
  496.  
  497.    Create_Cellular(t,u,c);     // 1st call of Create_Cellular
  498.  
  499.                                // 1Γ chamada da Create_Cellular
  500.  
  501.  
  502.  
  503.    do {
  504.  
  505.      irand1 = (int)(rand()/32767.0);
  506.  
  507.      irand2 = (int)(rand()/32767.0);
  508.  
  509.      a[irand1+1][irand2+1] = 1;
  510.  
  511.      for (i=1;i<=51;i++) {
  512.  
  513.         for (j=1;j<=51;j++) {
  514.  
  515.            e = a[i-1][j];
  516.  
  517.            w = a[i+1][j];
  518.  
  519.            n = a[i][j-1];
  520.  
  521.            s = a[i][j+1];
  522.  
  523.            cc = a[i][j];
  524.  
  525.            b[i][j] = Rulegenerator(n,s,e,w,cc,wires);
  526.  
  527.            c[i][j] = b[i][j];
  528.  
  529.         }
  530.  
  531.      }
  532.  
  533.  
  534.  
  535.      Create_Cellular(t,u,c);   // 2nd call of Create_Cellular
  536.  
  537.                                // 2Γ chamada da Create_Cellular
  538.  
  539.  
  540.  
  541.      for (i=1;i<=51;i++) {
  542.  
  543.         for (j=1;j<=51;j++) {
  544.  
  545.            e = b[i-1][j];
  546.  
  547.            w = b[i+1][j];
  548.  
  549.            n = b[i][j-1];
  550.  
  551.            s = b[i][j+1];
  552.  
  553.            cc = b[i][j];
  554.  
  555.            a[i][j] = Rulegenerator(n,s,e,w,cc,wires);
  556.  
  557.            c[i][j] = a[i][j];
  558.  
  559.         }
  560.  
  561.      }
  562.  
  563.  
  564.  
  565.      Create_Cellular(t,u,c);   // 3rd call of Create_Cellular
  566.  
  567.                                // 3Γ chamada da Create_Cellular
  568.  
  569.  
  570.  
  571.    } while (!kbhit());
  572.  
  573. //
  574.  
  575. // Sound effects and clean-up.
  576.  
  577. // Efeitos sonoros e limpeza da tela.
  578.  
  579. //
  580.  
  581.    sound(740);
  582.  
  583.    delay(600);
  584.  
  585.    sound(370);
  586.  
  587.    delay(300);
  588.  
  589.    nosound();
  590.  
  591.    closegraph();
  592.  
  593. }
  594.  
  595.  
  596.  
  597. void Create_Cellular(int t, int u, int c[53][53])
  598.  
  599.  
  600.  
  601. {
  602.  
  603.    int i, k, nx, nx1, nx2, ny, ny1, ny2, cx, cy, kcolorx, kcolory;
  604.  
  605.  
  606.  
  607.    setcolor(4);
  608.  
  609.    for (nx=0;nx<=51;nx++) {
  610.  
  611.       for (ny=0;ny<=51;ny++) {
  612.  
  613.          k = c[nx][ny];
  614.  
  615.          nx1 = 4*nx;
  616.  
  617.          ny1 = 4*ny;
  618.  
  619.          if (k !=0) {
  620.  
  621.            for (i=1;i<=k;i++) {
  622.  
  623.               nx2 = nx1 - i + t;
  624.  
  625.               ny2 = ny1 - i + u;
  626.  
  627.               setcolor(14);
  628.  
  629.               line (nx2, ny2+3, nx2+3, ny2+3);
  630.  
  631.               setcolor(13);
  632.  
  633.               line (nx2+3, ny2, nx2+3, ny2+3);
  634.  
  635.            }
  636.  
  637.            setcolor(k);
  638.  
  639.            cx = nx1-k+t+1;
  640.  
  641.            cy = ny1-k+u+1;
  642.  
  643.            rectangle (nx1-k+t, ny1-k+u, nx1+3-k+t, ny1+3-k+u);
  644.  
  645.            kcolorx = getpixel(nx1-k+t,ny1-k+u);
  646.  
  647.            setfillstyle(SOLID_FILL,k);
  648.  
  649.            floodfill(cx,cy,kcolorx);
  650.  
  651.          }
  652.  
  653.          else {
  654.  
  655.            setcolor(1);
  656.  
  657.            cx = nx1+t+1;
  658.  
  659.            cy = ny1+u+1;
  660.  
  661.            rectangle (nx1+t, ny1+u, nx1+3+t, ny1+3+u);
  662.  
  663.            kcolorx = getpixel(nx1-k+t,ny1-k+u);
  664.  
  665.            setfillstyle(SOLID_FILL,1);
  666.  
  667.            floodfill(cx,cy,kcolorx);
  668.  
  669.          }
  670.  
  671.       }
  672.  
  673.    }
  674.  
  675.    return;
  676.  
  677. }
  678.  
  679.  
  680.  
  681. int Rulegenerator(int n, int s, int e, int w, int cc, int wires[13])
  682.  
  683. {
  684.  
  685.  int ca[13], rule;
  686.  
  687.  
  688.  
  689.  ca[n] = ca[n] + 1;
  690.  
  691.  ca[s] = ca[s] + 1;
  692.  
  693.  ca[w] = ca[w] + 1;
  694.  
  695.  ca[e] = ca[e] + 1;
  696.  
  697.  ca[n] = ca[n] + 1;
  698.  
  699.  rule = wires[cc];
  700.  
  701.  if (cc == 0) {
  702.  
  703.    if (ca[1] > 0) rule = 1;
  704.  
  705.  }
  706.  
  707.  return rule;
  708.  
  709. }
  710.  
  711. ---Program NEWAUTO.CPP---End---CUT HERE---------------
  712.  
  713.