Guest User

Untitled

a guest
Jun 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. protected function fitImagesInClientArea():void {
  2.  
  3. // Create the main bin
  4.  
  5. var bins:Array = [new Bin(0, 0, width, height)];
  6.  
  7. // Sort the images
  8.  
  9. boxes.sort(function(a:Box, b:Box):int {
  10. if (a.area > b.area) {
  11. return -1;
  12. }
  13.  
  14. if (b.area > a.area) {
  15. return 1;
  16. }
  17.  
  18. return 0;
  19. });
  20.  
  21. // Iterate through each image, finding the best possible
  22. // bin for it based on its area.
  23.  
  24. for each (var box:Box in boxes) {
  25. var bestFit:Bin = null;
  26.  
  27. for each (var bin:Bin in bins) {
  28. if (bin.width > box.width && bin.height > box.height) {
  29. // The box fits in the area of the bin. But is
  30. // it a better fit?
  31.  
  32. if (!bestFit || (bestFit.area - box.area > bin.area - box.area)) {
  33. bestFit = bin;
  34. }
  35. }
  36. }
  37.  
  38. if (bestFit) {
  39.  
  40. // Position the box in the bin and add it to the screen
  41.  
  42. addChild(box);
  43.  
  44. box.left = bestFit.x;
  45. box.top = bestFit.y;
  46.  
  47. // Split the bin to reclaim any unused space
  48.  
  49. bins.push(new Bin(bestFit.x + box.width, bestFit.y, bestFit.width - box.width, box.height));
  50. bins.push(new Bin(bestFit.x, bestFit.y + box.height, bestFit.width, bestFit.height - box.height));
  51.  
  52. bins.splice(bins.indexOf(bestFit), 1);
  53. } else {
  54. trace("Unable to fit box with width: " + box.width + " and height: " + box.height);
  55. }
  56. }
  57.  
  58. }
Add Comment
Please, Sign In to add comment