Trying to implement a munching squares algorithm as mentioned in https://nbickford.wordpress.com/2011/04/03/the-minsky-circle-algorithm/
Last active
September 18, 2017 20:53
-
-
Save wwymak/a5743344b008c852ce474d93a20027d5 to your computer and use it in GitHub Desktop.
Munching Squares
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
</style> | |
</head> | |
<body> | |
<script> | |
const frameSize = 25; | |
const cellSize = 10; | |
let row = function() {return Array(frameSize).fill().map(() => Math.random())}; | |
let cellMatrix = Array(frameSize).fill(0); | |
for (let i = 0; i< frameSize; i++) { | |
cellMatrix[i] = row() | |
} | |
function update(frameNumber, cellMatrix) { | |
frameNumber = frameNumber % (frameSize * 1.2) | |
for(let i = 0; i < frameSize; i++) { | |
for (let k = 0; k < frameSize; k++) { | |
// console.log((i+1) ^ (k+1)) | |
if ((i ^ k) < frameNumber) { | |
cellMatrix[i][k] = 1 | |
} else { | |
cellMatrix[i][k] = 0 | |
} | |
} | |
} | |
return cellMatrix | |
} | |
function updateCells(cellMatrix, rectsRow, cells) { | |
rectsRow.data(cellMatrix) | |
cells.selectAll('rect').data(cellMatrix); | |
cells.merge(cells).attr('fill', d => d> 0.5? 'black': 'white') | |
} | |
// Feel free to change or delete any of the code you see in this editor! | |
const svg = d3.select("body").append("svg") | |
.attr("width", 800) | |
.attr("height", 800) | |
function render(cellMatrix) { | |
let rectsRow = svg.selectAll("g") | |
.data(cellMatrix) | |
let rectsRowEnter = rectsRow | |
.enter().append('g') | |
.attr('class', 'cell-row'); | |
let cell = rectsRow.merge(rectsRowEnter) | |
.attr('transform', (d, i) => `translate(0, ${i * cellSize})`) | |
.selectAll("rect") | |
.data(d => {console.log(d); return d}) | |
let cellEnter = cell.enter().append('rect') | |
.attr('width', cellSize) | |
.attr('height', cellSize) | |
.attr('x', (k, i) => cellSize * i ) | |
.attr('y', 0) | |
cell.merge(cellEnter).attr('fill', d => d> 0.5? 'black': 'white'); | |
} | |
let counter = 0; | |
render(cellMatrix); | |
// d3.timeout(() => { | |
// update(2, cellMatrix); | |
// render(cellMatrix) | |
// }, 100) | |
d3.interval(() => { | |
cellMatrix = update(counter, cellMatrix); | |
render(cellMatrix); | |
counter +=1; | |
}, 500); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment