BinPacker¶
Overview¶
Given a rectangular containing area, takes care of allocating and deallocating smaller rectangular "bins" that fit together inside the area and do not overlap. Optimized more for runtime CPU usage than space currently.
For example:
begin canvasExample binPacker 256x256¶
on¶
var binPacker = new phet.dot.BinPacker( new dot.Bounds2( 0, 0, 256, 256 ) ); var bins = []; for ( var i = 0; i < 100; i++ ) { var bin = binPacker.allocate( Math.random() * 64, Math.random() * 64 ); if ( bin ) { bins.push( bin ); } }
off¶
context.strokeStyle = '#000'; bins.forEach( function( bin ) { var bounds = bin.bounds; context.strokeRect( bounds.x, bounds.y, bounds.width, bounds.height ); } );
end canvasExample¶
@author Sharfudeen Ashraf @author Jonathan Olson <jonathan.olson@colorado.edu>
Class BinPacker¶
Constructor¶
new BinPacker( bounds : Bounds2 )¶
Instance Methods¶
allocate( width : number, height : number ) : Bin | null¶
Allocates a bin with the specified width and height if possible (returning a {Bin}), otherwise returns null.
deallocate( bin : Bin )¶
Deallocates a bin, so that its area can be reused by future allocations.
@param bin - The bin that was returned from allocate().
toString() : string¶
Static Properties¶
Bin : typeof Bin¶
Class Bin¶
Constructor¶
new Bin( bounds : Bounds2, parent : Bin | null )¶
Instance Methods¶
toString() : string¶
Instance Properties¶
bounds : Bounds2¶
Our containing bounds
children : Bin[]¶
Source Code¶
See the source for BinPacker.ts in the dot repository.