{"id":134,"date":"2013-02-11T02:57:00","date_gmt":"2013-02-11T02:57:00","guid":{"rendered":"http:\/\/www.celesteh.com\/blog\/2013\/02\/11\/fun-with-cellular-automata\/"},"modified":"2015-06-19T00:23:14","modified_gmt":"2015-06-18T23:23:14","slug":"fun-with-cellular-automata","status":"publish","type":"post","link":"https:\/\/www.celesteh.com\/blog\/2013\/02\/11\/fun-with-cellular-automata\/","title":{"rendered":"Fun with Cellular Automata"},"content":{"rendered":"<p>The SuperCollider code is stolen from <a href=\"http:\/\/www.fredrikolofsson.com\/f0blog\/index.php?q=node\/43\">redFrick&#8217;s blog post about cellular automata<\/a>.  All I have added is the SynthDef, one line at the top to set sound variables and one line in the drawing function to make new synths based on what blocks are coloured in.<br \/>\nErgo, I haven&#8217;t really got anything to add, but this is still kind of fun and Star-Trek-ish<\/p>\n<pre>\n(\n\nSynthDef(sinegrain2, {arg pan = 0, freq, amp, grainDur; var grain, env;\n\n env = EnvGen.kr(Env.sine(grainDur * 2, amp), doneAction:2); \/\/ have some overlap\n grain= SinOsc.ar(freq, 0, env);\n\n\nOut.ar(0,Pan2.ar(grain, pan))}).add;\n\n)\n\n\n\/\/game of life \/redFrik\n(\n\/\/ add sound-related variables\nvar grainDur = 1\/20, lowfreq = 200, hifreq = 800, lowamp =0.005, hiamp = 0.085, rows = 50, cols = 50;\n\n\n      var envir, copy, neighbours, preset, rule, wrap;\n        var w, u, width= 200, height= 200, cellWidth, cellHeight;\n        w= Window(\"ca - 2 pen\", Rect(128, 64, width, height), false);\n        u= UserView(w, Rect(0, 0, width, height));\n        u.background= Color.white;\n        cellWidth= width\/cols;\n        cellHeight= height\/rows;\n        wrap= true;                     \/\/if borderless envir\n        \/*-- select rule here --*\/\n        \/\/rule= #[[], [3]];\n        \/\/rule= #[[5, 6, 7, 8], [3, 5, 6, 7, 8]];\n        \/\/rule= #[[], [2]];                                             \/\/rule \"\/2\" seeds\n        \/\/rule= #[[], [2, 3, 4]];\n        \/\/rule= #[[1, 2, 3, 4, 5], [3]];\n        \/\/rule= #[[1, 2, 5], [3, 6]];\n        \/\/rule= #[[1, 3, 5, 7], [1, 3, 5, 7]];\n        \/\/rule= #[[1, 3, 5, 8], [3, 5, 7]];\n        rule= #[[2, 3], [3]];                                           \/\/rule \"23\/3\" conway's life\n        \/\/rule= #[[2, 3], [3, 6]];                                      \/\/rule \"23\/36\" highlife\n        \/\/rule= #[[2, 3, 5, 6, 7, 8], [3, 6, 7, 8]];\n        \/\/rule= #[[2, 3, 5, 6, 7, 8], [3, 7, 8]];\n        \/\/rule= #[[2, 3, 8], [3, 5, 7]];\n        \/\/rule= #[[2, 4, 5], [3]];\n        \/\/rule= #[[2, 4, 5], [3, 6, 8]];\n        \/\/rule= #[[3, 4], [3, 4]];\n        \/\/rule= #[[3, 4, 6, 7, 8], [3, 6, 7, 8]];               \/\/rule \"34578\/3678\" day&night\n        \/\/rule= #[[4, 5, 6, 7], [3, 5, 6, 7, 8]];\n        \/\/rule= #[[4, 5, 6], [3, 5, 6, 7, 8]];\n        \/\/rule= #[[4, 5, 6, 7, 8], [3]];\n        \/\/rule= #[[5], [3, 4, 6]];\n        neighbours= #[[-1, -1], [0, -1], [1, -1], [-1, 0], [1, 0], [-1, 1], [0, 1], [1, 1]];\n        envir= Array2D(rows, cols);\n        copy= Array2D(rows, cols);\n        cols.do{|x| rows.do{|y| envir.put(x, y, 0)}};\n        \/*-- select preset here --*\/\n        \/\/preset= #[[0, 0], [1, 0], [0, 1], [1, 1]]+(cols\/2); \/\/block\n        \/\/preset= #[[0, 0], [1, 0], [2, 0]]+(cols\/2); \/\/blinker\n        \/\/preset= #[[0, 0], [1, 0], [2, 0], [1, 1], [2, 1], [3, 1]]+(cols\/2); \/\/toad\n        \/\/preset= #[[1, 0], [0, 1], [0, 2], [1, 2], [2, 2]]+(cols\/2); \/\/glider\n        \/\/preset= #[[0, 0], [1, 0], [2, 0], [3, 0], [0, 1], [4, 1], [0, 2], [1, 3], [4, 3]]+(cols\/2); \/\/lwss\n        \/\/preset= #[[1, 0], [5, 0], [6, 0], [7, 0], [0, 1], [1, 1], [6, 2]]+(cols\/2); \/\/diehard\n        \/\/preset= #[[0, 0], [1, 0], [4, 0], [5, 0], [6, 0], [3, 1], [1, 2]]+(cols\/2); \/\/acorn\n        preset= #[[12, 0], [13, 0], [11, 1], [15, 1], [10, 2], [16, 2], [24, 2], [0, 3], [1, 3], [10, 3], [14, 3], [16, 3], [17, 3], [22, 3], [24, 3], [0, 4], [1, 4], [10, 4], [16, 4], [20, 4], [21, 4], [11, 5], [15, 5], [20, 5], [21, 5], [34, 5], [35, 5], [12, 6], [13, 6], [20, 6], [21, 6], [34, 6], [35, 6], [22, 7], [24, 7], [24, 8]]+(cols\/4); \/\/gosper glider gun\n        \/\/preset= #[[0, 0], [2, 0], [2, 1], [4, 2], [4, 3], [6, 3], [4, 4], [6, 4], [7, 4], [6, 5]]+(cols\/2); \/\/infinite1\n        \/\/preset= #[[0, 0], [2, 0], [4, 0], [1, 1], [2, 1], [4, 1], [3, 2], [4, 2], [0, 3], [0, 4], [1, 4], [2, 4], [4, 4]]+(cols\/2); \/\/infinite2\n        \/\/preset= #[[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [9, 0], [10, 0], [11, 0], [12, 0], [13, 0], [17, 0], [18, 0], [19, 0], [26, 0], [27, 0], [28, 0], [29, 0], [30, 0], [31, 0], [32, 0], [34, 0], [35, 0], [36, 0], [37, 0], [38, 0]]+(cols\/4); \/\/infinite3\n        \/\/preset= Array.fill(cols*rows, {[cols.rand, rows.rand]});\n        preset.do{|point| envir.put(point[0], point[1], 1)};\n        i= 0;\n        u.drawFunc= {\n                i= i+1;\n                Pen.fillColor= Color.black;\n                cols.do{|x|\n                        rows.do{|y|\n                                if(envir.at(x, y)==1, {\n                                        Pen.addRect(Rect(x*cellWidth, height-(y*cellHeight), cellWidth, cellHeight));\n    \/\/ the new line\n    Synth.new(sinegrain2,[freq, x.linexp(0, cols, lowfreq, hifreq), amp, y.linexp(0, rows, lowamp, hiamp), pan, 0, grainDur, grainDur])\n                                });\n                        };\n                };\n                Pen.fill;\n                cols.do{|x|\n                        rows.do{|y|\n                                var sum= 0;\n                                neighbours.do{|point|\n                                        var nX= x+point[0];\n                                        var nY= y+point[1];\n                                        if(wrap, {\n                                                sum= sum+envir.at(nX%cols, nY%rows); \/\/no borders\n                                        }, {\n                                                if((nX&gt;=0)&&(nY&gt;=0)&&(nX&lt;cols)&&(nY&lt;rows), {sum= sum+envir.at(nX, nY)}); \/\/borders\n                                        });\n                                };\n                                if(rule[1].includes(sum), {     \/\/borne\n                                        copy.put(x, y, 1);\n                                }, {\n                                        if(rule[0].includes(sum), {     \/\/lives on\n                                                copy.put(x, y, envir.at(x, y));\n                                        }, {    \/\/dies\n                                                copy.put(x, y, 0);\n                                        });\n                                });\n                        };\n                };\n                envir= copy.deepCopy;\n        };\n        Routine({while{w.isClosed.not} {u.refresh; i.postln; (1\/20).wait}}).play(AppClock);\n        w.front;\n)\n\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The SuperCollider code is stolen from redFrick&#8217;s blog post about cellular automata. All I have added is the SynthDef, one line at the top to set sound variables and one line in the drawing function to make new synths based on what blocks are coloured in. Ergo, I haven&#8217;t really got anything to add, but &hellip; <a href=\"https:\/\/www.celesteh.com\/blog\/2013\/02\/11\/fun-with-cellular-automata\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Fun with Cellular Automata<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[1],"tags":[64],"class_list":["post-134","post","type-post","status-publish","format-standard","hentry","category-uncategorised","tag-supercollider"],"_links":{"self":[{"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/posts\/134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/comments?post=134"}],"version-history":[{"count":1,"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"predecessor-version":[{"id":2329,"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/posts\/134\/revisions\/2329"}],"wp:attachment":[{"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.celesteh.com\/blog\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}