An extension to http://wonderfl.net/c/4NOz
that aligns the created zones in a grid
(so what this things does is that it tries to break a tilemap into rectangles, and i'm experimenting which algorithm solves it with fewer rectangles)
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/cpeI
*/
package {
import flash.display.Graphics;
import flash.geom.Rectangle;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
var pic:Bitmap;
var bm:BitmapData;
var r:Rectangle;
var i:int;
r = new Rectangle();
bm = new BitmapData(64,64,false,0x808080);
//bm.noise(1231);
for (i = 0; i < 256; i++)
{
r.x = Math.random()*bm.width;
r.y = Math.random()*bm.height;
r.width = Math.random() * 12;
r.height = Math.random() * 12;
bm.fillRect(r, 0);
}//nexti
for (i = 0; i < 32; i++)
{
bm.setPixel( Math.random()*bm.width, Math.random()*bm.height, 0x808080);
}//nexti
pic = new Bitmap(bm);
addChild(pic);
pic.scaleX = 4;
pic.scaleY = 4;
var deb:Sprite;
deb = new Sprite();
addChild(deb);
drawZone(makeVector(bm), bm.width, bm.height, deb.graphics);
}//ctor
public function makeVector(bm:BitmapData):Vector.<int>
{
var vec:Vector.<int>;
var mw:int;
var mh:int;
var i:int;
var k:int;
var num:int;
var yt:int;
mw = bm.width;
mh = bm.height;
num = mw * mh;
vec = new Vector.<int>(num, false);
for (i = 0; i < mh; i++)
{
yt = i * mw;
for (k = 0; k < mw; k++)
{
vec[yt+k] = bm.getPixel(k, i);
}//nextk
}//nexti
return vec;
}//makevec
public function drawZone(vec:Vector.<int>, mw:int, mh:int, g:Graphics):void
{
var k:int;
var i:int;
var yt:int;
var ht:int;
var t:int;
var h:int;
var n:int;
var mn:int;
var col:Vector.<int>;
var c:int;
//align zones to a grid
var cellw:Number;
var qi:int;
var qk:int;
cellw = 16;
col = new Vector.<int>(mw, false);
for (c = 0; c < mw; c++) { col[c] = -2; }
g.clear();
g.lineStyle(1, 0xFF0000);
for (i = 0; i < mh; i++)
{
yt = i * mw;
qi = (Math.floor(i/cellw) * cellw) + (cellw - 1);
for (k = 0; k < mw; k++)
{
if (col[k] > i) { continue;}
t = vec[yt+k];
if (t > 0) { continue; }
qk = (Math.floor(k/cellw) * cellw) + (cellw - 1);
mn = mw;
for (h = i; h < (qi+1); h++)
{
ht = h * mw;
t = vec[ht + k];
if (t > 0) { break; }
for (n = k; n < mw; n++)
{
if (col[n]>h) { break;}
t = vec[ht + n];
if (t > 0) { break; }
}
if (mn > n) { mn = n;}
}//nexth
if (mn > qk) { mn = qk+1;}
for (n = k; n < mn; n++)
{
col[n] = h;
}
g.beginFill(0xFFffFF, 0.8);
g.drawRect(k*4,i*4, (mn-k)*4, (h-i)*4 );
g.endFill();
g.moveTo(k*4,i*4);
g.lineTo(mn*4,h*4);
k = mn-1;
}//nextk
}//nexti
g.lineStyle(2, 0);
mw = mw /cellw;
mh = mh / cellw;
for (i = 0; i < mh; i++)
{
for (k = 0; k < mw; k++)
{
g.drawRect(k*cellw*4,i*cellw*4,cellw*4,cellw*4);
}
}
}//drawzone
}//classend
}