Skip to content

Instantly share code, notes, and snippets.

@mtabini
Created September 30, 2010 14:12
Show Gist options
  • Save mtabini/604626 to your computer and use it in GitHub Desktop.
Save mtabini/604626 to your computer and use it in GitHub Desktop.
protected function fitImagesInClientArea():void {
// Create the main bin
var bins:Array = [new Bin(width, 0)];
// Sort the images
boxes.sort(function(a:Box, b:Box):int {
if (a.width > b.width) {
return -1;
}
if (b.width > a.width) {
return 1;
}
return 0;
});
// Iterate through each image, finding the best possible
// bin for it based on its area.
for each (var box:Box in boxes) {
var bestFit:Bin = null;
for each (var bin:Bin in bins) {
if (bin.width >= box.width) {
// The box fits in the width of the bin. But is
// it a better fit?
if (!bestFit || (bestFit.width - box.width > bin.width - box.width)) {
bestFit = bin;
}
}
}
if (bestFit) {
// Position the box in the bin and add it to the screen
var hbox:HBox = getBinContainerAtIndex(bestFit.rowNumber);
hbox.addChild(box);
hbox.addChild(safeSpacer());
// Reclaim any unused space
bestFit.width -= box.width;
} else {
trace("Unable to fit box with width: " + box.width + " and height: " + box.height);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment