Skip to content

Instantly share code, notes, and snippets.

@spite
Created January 17, 2017 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spite/02b6f5ad763af903b791c4794530e213 to your computer and use it in GitHub Desktop.
Save spite/02b6f5ad763af903b791c4794530e213 to your computer and use it in GitHub Desktop.
function Block( x, y, w, h, d ) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.d = d;
this.children = [];
}
Block.prototype.split = function() {
var x = this.x;
var y = this.y;
var w = this.w;
var h = this.h;
var d = this.d;
d *= .75 + Math.random() * .5;
if( Math.random() > .5 ) {
var hw = w / 2;
this.children.push( new Block( x, y, hw, h, d ) );
this.children.push( new Block( x + hw, y, hw, h, d ) );
} else {
var hh = h / 2;
this.children.push( new Block( x, y, w, hh, d ) );
this.children.push( new Block( x, y + hh, w, hh, d ) );
}
}
Block.prototype.getUnsplitBlocks = function() {
if( this.children.length === 0 ) return this;
var nodes = [];
this.children.forEach( function( b ) {
nodes = nodes.concat( b.getUnsplitBlocks() );
});
return nodes;
}
var b = new Block( 0, 0, 100, 100, 5 );
b.split();
for( var j = 0; j < 12; j++ ) {
var nodes = b.getUnsplitBlocks();
nodes.forEach( function( b ) { b.split() })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment