Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
just all the files
  • Loading branch information
Camto authored Mar 23, 2017
1 parent 961b1bd commit 02a0c35
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Canvas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class Canvas {

constructor(width, height) {

this.canvas = document.createElement("canvas");
this.canvas.width = this.width = width;
this.canvas.height = this.height = height;
this.draw = this.canvas.getContext("2d");
document.body.appendChild(this.canvas);

this.canvas.style.position = "absolute";
this.canvas.style.margin = "auto";
this.canvas.style.top = "0px";
this.canvas.style.left = "0px";
this.canvas.style.bottom = "0px";
this.canvas.style.right = "0px";
// this.canvas.style.border = "5px solid black"; // fancy, but not necessary
// this.canvas.style.backgroundColor = "white"; // optional
// this.canvas.style.cursor = "none"; // also optional

}

rect(x, y, width, height, colour) {

this.draw.fillStyle = colour;
this.draw.fillRect(x, y, width, height);

}

circle(x, y, radius, colour) {

this.draw.beginPath();
this.draw.fillStyle = colour;
this.draw.arc(x, y, radius, 0, 2 * Math.PI);
this.draw.fill();
this.draw.closePath();

}

text(text, x, y, colour, size, font, extras) {

if(extras != undefined) {

this.draw.font = extras + " " + size + "px " + font;

} else {

this.draw.font = size + "px " + font;

}

this.draw.fillStyle = colour;
this.draw.textAlign = "center";
this.draw.fillText(text, x, y + (size / 2));

}

clear_screen() {

this.draw.clearRect(0, 0, this.width, this.height);

}

}
110 changes: 110 additions & 0 deletions Rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
class Rule {

constructor(rules, size) {

this.rules = rules;
this.size = size;
this.ppl = [];
for(var count = 0; count < Math.floor(size); count++) {

this.ppl.push(0);

}
this.ppl.push(1);
for(var count = 0; count < Math.ceil(size); count++) {

this.ppl.push(0);

}

this.history = [this.ppl];

}

generation(canvas) {

var next = [];

for(var count = 0; count < this.ppl.length; count++) {

var first = this.person(count - 1);
var second = this.person(count );
var third = this.person(count + 1);

var neighbors = first.toString() + second.toString() + third.toString();

switch(neighbors) {

case "111": next.push(this.rules[0]); break;

case "110": next.push(this.rules[1]); break;

case "101": next.push(this.rules[2]); break;

case "100": next.push(this.rules[3]); break;

case "011": next.push(this.rules[4]); break;

case "010": next.push(this.rules[5]); break;

case "001": next.push(this.rules[6]); break;

case "000": next.push(this.rules[7]); break;

}

}

this.ppl = next;
this.history.push(this.ppl);
if(this.history.length > canvas.height / 10 + 3) {

this.history.shift();

}

return this.ppl;

}

person(index) {

if(!(index < 0 || index > (this.ppl.length - 1))) {

return this.ppl[index];

} else {

if(index < 0) {

return this.ppl[this.ppl.length - 1];

} else {

return this.ppl[0];

}

}

}

draw(offset, canvas) {

for(var y = 0; y < this.history.length; y++) {

for(var x = 0; x < this.history[y].length; x++) {

if(this.history[y][x]) {

canvas.rect(x * 10, canvas.height + ((y - this.history.length) * 10) - offset + 20, 10, 10, "black");

}

}

}

}

}
18 changes: 18 additions & 0 deletions Rules.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>

<head>

<title>Elementary Cellular Automaton</title>

</head>

<body>

<script src="Rule.js"></script>
<script src="Canvas.js"></script>
<script src="Rules.js"></script>

</body>

</html>
46 changes: 46 additions & 0 deletions Rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function Init() {

rule_num = Math.min(Math.max(parseInt(prompt("Rule?", "73")), 0), 255);
rule = rule_num.toString(2).split("").map(function(num) {return parseInt(num)});
while(rule.length < 8) {

rule.unshift(0);

}

document.getElementsByTagName("title")[0].innerHTML = "Rule " + rule_num;

pattern = new Rule(rule, parseInt(prompt("How big should be the world?", "40")));

screen = new Canvas(Math.min(pattern.ppl.length * 10, window.innerWidth), Math.min(pattern.ppl.length * 15, window.innerHeight));

frame = 0;

}

function Begin() {

Init();

window.requestAnimationFrame(Game);

}
function Game() {

frame++
if(frame > 6) {

frame = 0;

pattern.generation(screen);

}

screen.clear_screen();
pattern.draw((frame / 7) * 10, screen);

window.requestAnimationFrame(Game);

}

Begin();

0 comments on commit 02a0c35

Please sign in to comment.