-
-
Save spite/02b6f5ad763af903b791c4794530e213 to your computer and use it in GitHub Desktop.
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
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