flash on 2012-11-16

by mutantleg
based on the algorithm at
http://www.blackpawn.com/texts/lightmaps/
♥0 | Line 126 | Modified 2012-11-16 18:49:51 | MIT License
play

ActionScript3 source code

/**
 * Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/r6WK
 */

package {
    import flash.display.BitmapData;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        
        public function FlashTest() {
         
         var myNode:Node;
             myNode = new Node();
             myNode.rx = 0;
             myNode.ry = 0;
             myNode.rw = 256;
             myNode.rh = 256;
             
         var i:int;
         var num:int;
         var b:BitmapData;
         var a:Vector.<BitmapData> = new Vector.<BitmapData>;
         
         for (i =0; i < 64; i++)
         {
             b = new BitmapData(Math.random()*32+8,Math.random()*32+8,false,Math.random()*0xFFffFFff);
             //myNode.insertRect(b);
             a.push(b);
         } //nexti    
         
         a.sort(compData);
         
         num = a.length;
         
         for (i =0; i < num; i++)
         { 
            b = a[i];
            myNode.insertRect(b);
         }//nexti
         
         
         graphics.clear();
         graphics.lineStyle(1, 0);
         myNode.draw(graphics);
            
            
            
        }//ctor
        
        public function compData(a:BitmapData, b:BitmapData):Number
        {
            if (a.width > b.width && a.height > b.height)
            { return -1;  }
            if (b.width > a.width && b.height > a.height)
            { return 1;}
            
            return 0;

        }//comp
        
        
    }//classend
}
import flash.display.Graphics;
import flash.display.BitmapData;

//based on
//

internal class Node
{
    public var rx:Number = 0;
    public var ry:Number = 0;
    public var rw:Number = 0;
    public var rh:Number = 0;
    public var bm:BitmapData;
    public var childa:Node;
    public var childb:Node;
  
    public function draw(g:Graphics):void
    {
      
       if (bm != null)
       {
           g.lineStyle(1,0);
        g.beginBitmapFill(bm);
        //g.beginFill(0xFFffFF*Math.random(),1);
         g.drawRect(rx,ry,rw,rh);
        g.endFill();
       }
       else
       {
         g.lineStyle(1,0x7a7a7a);
         g.drawRect(rx,ry,rw,rh);  
       }
        
        if (childa != null)
        {
            childa.draw(g);
            childb.draw(g);
        }//endif
    }//draw
  
    public function insertRect(b:BitmapData):Node
    {
        var n:Node;
        var dw:Number;
        var dh:Number;
        
        if (childa != null)
        {
            //not leaf
            
            n = childa.insertRect(b);
            if (n == null)
            {
              n = childb.insertRect(b);  
            }
            
            return n;    
        }
        else
        {
            //already has image
           if (bm != null) { return null; }
           
           //doesn't fit
           if (b.width > rw || b.height > rh)
           { return null; }
           
           //just right
           if (b.width == rw && b.height == rh)
           { 
            bm = b;
            return this; 
           }
           
           //split
           childa = new Node();
           childb = new Node();
           
           dw = rw - b.width;
           dh = rh - b.height;
           
           if (dw > dh)
           {
               childa.rx = rx;
               childa.ry = ry;
               childa.rw = b.width;
               childa.rh = rh;
               
               childb.rx = rx + b.width;
               childb.ry = ry;
               childb.rw = rw-b.width;
               childb.rh = rh;
               
           }
           else
           {
               childa.rx = rx;
               childa.ry = ry;
               childa.rw = rw;
               childa.rh = b.height;
               
               childb.rx = rx;
               childb.ry = ry+b.height;
               childb.rw = rw;
               childb.rh = rh-b.height;
           }//endif
        
          return childa.insertRect(b);
        
        }//endif
    }//insrect
    
};//node