In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: fladdict challenge for professionals

Theme:
* Play with BitmapPatterBuilder.
* Purpose of this trial is to find the possibility of the dot pattern.
*
* by Takayuki Fukatsu aka fladdict
/**
 * Copyright faseer ( http://wonderfl.net/user/faseer )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/r06i
 */

// forked from checkmate's fladdict challenge for professionals
/**
 * Theme:
 * Play with BitmapPatterBuilder.
 * Purpose of this trial is to find the possibility of the dot pattern.
 *
 * by Takayuki Fukatsu aka fladdict
 **/
package {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.geom.Matrix;
    
    
    public class Professional extends Sprite {
		private var pattern:BitmapData;
		private var mat:Matrix = new Matrix();
		private var colors:Array = [0xFFFFFFFF, 0xFF000000];
		private var map:Array;
		private var size:int;
		private var ix:int;
		private var iy:int;
		private var dir:int=0;
		private var v:int = 0;
		
 		public function Professional()
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;

			size = 8;

			map = createMap(size, size);

			addEventListener(Event.ENTER_FRAME, update);
		}
		
		public function update(e:Event):void 
		{
			if (isNewRound())
			{
				v = int(!v);
				colors[v] = 0xFF000000 | (Math.random() * 0xFFFFFF);
				updateTransform();
			}
			updateMap();
			updatePattern();

			var g:Graphics = graphics;
			g.beginBitmapFill(pattern, mat);
			g.drawRect(0,0,480,480);
			g.endFill();
		}

		
		private function isNewRound():Boolean
		{
			return 1 == dir && 1 == ix && 1 == iy;
		}
		
		private function updateTransform():void
		{
			var _size:int = size;
			var t:Array = [2, 2, 3, 5];
			for (var i:int = t.length - 1; i > -1;--i)
				if (Math.random() > .5) _size *= t[i];
				
			mat.identity();
			mat.a = mat.d = _size / size;
		}
		
		private function updatePattern():void
		{
			if (pattern) pattern.dispose();
			pattern = BitmapPatternBuilder.build(map, colors);
		}
		
		private function updateMap():void
		{
			var r:int = size;
			var c:int = size;
			
			switch(dir)
			{
				case 1: if (++ix == c) dir = 2; break;
				case 2: if (++iy == r) dir = 3; break;
				case 3: if (--ix == 1) dir = 4; break;
				case 4: if (--iy == 1) dir = 1; break;
				default: iy = 1, ix = 1, dir = 1;
			}
			drawMap(map, r/2, c/2, ix-1, iy-1, v);
		}
		
		private function createMap(w:int, h:int):Array
		{
			var a:Array = [];
			for (var y:int = 0; y < h; ++y)
			{
				a[y] = [];
				for (var x:int = 0; x < w; ++x)
					a[y][x] = 0;
			}
			return a;
		}
		private function drawMap(map:Array, x0:int, y0:int, x1:int, y1:int, v:int):void
		{
			var xy2yx:Boolean = Math.abs(y1-y0) > Math.abs(x1-x0);
			if (xy2yx)
			{
				x0 ^= y0; y0 ^= x0; x0 ^= y0;
				x1 ^= y1; y1 ^= x1; x1 ^= y1;
			}
			if (x0 > x1)
			{
				x0 ^= x1; x1 ^= x0; x0 ^= x1;
				y0 ^= y1; y1 ^= y0; y0 ^= y1;
			}
			var ydir:int = y0 > y1 ? -1 : 1;
			var dy:int = Math.abs(y1 - y0);
			var dx:int = x1 - x0;
			var d:int = 2 * dy - dx;
			var ix:int = x0;
			var iy:int = y0;
			while (ix <= x1)
			{
				if (xy2yx)
					map[iy][ix] = v;
				else
					map[ix][iy] = v;
					
				++ix;
				if (d > 0)
				{
					iy += ydir;
					d += 2 * (dy - dx);
				}
				else
					d += 2 * dy;
			}
		}

    }
}

/**-----------------------------------------------------
 * Use following BitmapPatternBuilder class 
 * 
 * DO NOT CHANGE any codes below this comment.
 *
 * -----------------------------------------------------
*/
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
    
class BitmapPatternBuilder{
    /**
     * creates BitmapData filled with dot pattern.
     * First parameter is 2d array that contains color index for each pixels;
     * Second parameter contains color reference table.
     *
     * @parameter pattern:Array 2d array that contains color index for each pixel.
     * @parameter colors:Array 1d array that contains color table.
     * @returns BitmapData
     */
    public static function build(pattern:Array, colors:Array):BitmapData{
        var bitmapW:int = pattern[0].length;
        var bitmapH:int = pattern.length;
        var bmd:BitmapData = new BitmapData(bitmapW,bitmapH,true,0x000000);
        for(var yy:int=0; yy<bitmapH; yy++){
            for(var xx:int=0; xx<bitmapW; xx++){
                var color:int = colors[pattern[yy][xx]];
                bmd.setPixel32(xx, yy, color);
            }
        }
        return bmd;
    }
    
    /**
     * short cut function for Graphics.beginBitmapFill with pattern.
     */
    public static function beginBitmapFill(pattern:Array, colors:Array, graphics:Graphics):void{
        var bmd:BitmapData = build(pattern, colors);
        graphics.beginBitmapFill(bmd);
        bmd.dispose();        
    }
}