Skip to content

Instantly share code, notes, and snippets.

@nyango
Created December 1, 2013 11:31
Show Gist options
  • Save nyango/7732408 to your computer and use it in GitHub Desktop.
Save nyango/7732408 to your computer and use it in GitHub Desktop.
一次元オートマトンの全パターン(遷移あり)
ruleは
1,2,...,16
16,
32,
:
, ... ...,255
と並んでます。
クリックすると遷移します。
* {
overflow:hidden;
margin: 0;
padding: 0;
border: 0;
}
<canvas id="canvas"></canvas>
<div id="test"></div>
// forked from jag's "一次元オートマトンの全パターン:forked: 一次元オートマトン" http://jsdo.it/jag/iIqr
// forked from jag's "一次元オートマトン" http://jsdo.it/jag/lESE
var zentai = true;
function drawAutomatonAll(isPart,pRule){
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var width = canvas.width = window.innerWidth;
var height = canvas.height = window.innerHeight;
if(!isPart){
width = Math.floor(width/16);
height = Math.floor(height/16);
}
// VVVVV
var rule=0; // rule start
for(;rule<256;rule++){
var off_i = width*(rule%16);
var off_j = height*((rule>>4)%16);
// AAAAA
if(isPart) {
off_i = off_j = 0;
if(rule-pRule > 0) break;
rule = pRule;
}
var row = new Array(height);
// Initialize row[0]
var padd="";
for(var tmp=0;tmp<width;tmp++){
if (tmp==parseInt(width/2)){
padd+='1';
}else{
padd+='0';
}
}
row[0] = padd;
// Run 1 dimentional cell automaton
var row_idx=0;
for(;row_idx<height-1;row_idx++){
var buf="";
var str=row[row_idx];
for(var col=0;col<width;col++){
var j;
if(col==0) {
j = parseInt('0'+str.substr(0,2),2);
} else {
j = parseInt((str.substr(col-1,3)+'00').slice(0,3),2);
}
buf = buf + ((rule>>j)%2).toString(2);
}
row[row_idx+1] = buf;
}
// Draw in canvas
for(j=0;j<height;j++){
for(i=0;i<width;i++){
if(row[j].substr(i,1) == '1')ctx.fillRect(off_i+i,off_j+j,1,1);
}
}
ctx.stroke();
// VVVVV
}// rule end
// AAAAA
}
canvas.addEventListener('click', function(e){
if(zentai) {
mouseX = e.clientX ;
mouseY = e.clientY ;
var width = Math.floor(window.innerWidth/16);
var height = Math.floor(window.innerHeight/16);
idx = Math.floor(mouseX/width);
idy = Math.floor(mouseY/height);
alert('rule:'+(idy*16+idx));
zentai = false;
drawAutomatonAll(true,(idy*16+idx));
} else {
zentai = true;
drawAutomatonAll(false,0);
}
}, false);
// VVVVV
drawAutomatonAll(false,0);
// AAAAA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment