flash on 2010-5-13

by kihon
♥0 | Line 107 | Modified 2010-05-13 18:15:23 | MIT License
play

ActionScript3 source code

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

package
{
	import com.bit101.components.PushButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import frocessing.math.Random;
	import jp.progression.commands.Func;
	import jp.progression.commands.lists.SerialList;
	import jp.progression.commands.Wait;
	
	public class Main extends Sprite
	{
		private var data:Array;
		private var SIZE:int = 5;
		private var num:int = 80;
		private var list:SerialList = new SerialList();
		private var copy:Array;
		
		public function Main()
		{
			graphics.beginFill(0xF0F0F0);
			graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			graphics.endFill();
			
			data = new Array(num);
			for (var i:int = 0; i < data.length; i++)
			{
				var rect:Rect = new Rect(SIZE);
				rect.x = rect.value = i * SIZE;
				rect.y = i * SIZE;
				addChild(rect);
				data[i] = rect;
			}
			init();

			new PushButton(this, 185, 400, "execute", execute);
		}
		
		private function execute(event:Event):void
		{
			if (list.state == 2) return;
			list = new SerialList();
			
			init();
			sort();
			list.execute();
		}
		
		private function init():void
		{
			var random:Array = Random.shakedIntegers(num);
			for (var i:int = 0; i < data.length; i++)
			{
				data[i].x = random[i] * SIZE;
			}
		}
		
		private function sort():void
		{
			copy = new Array(num);
			for (var i:int = 0; i < data.length; i++)
			{
				copy[i] = data[i];
			}
			
			split(copy);
		}
		
		private function split(data:Array):Array
		{
			if (data.length == 1) return data;
			else
				return merge
						(
							split(data.slice(0, data.length / 2)),
							split(data.slice(data.length / 2, data.length))
						);
		}
		
		private function merge(a:Array, b:Array):Array
		{
			var i:int = 0;
			var j:int = 0;
			var dest:Array = [];
			
			while (i < a.length || j < b.length)
			{
				if (j >= b.length || (i < a.length && a[i].value < b[j].value))
				{
					list.addCommand(new Func(function(rect:Rect, tx:int):void { rect.x = tx * SIZE; }, [a[i], i + j]));
					list.addCommand(new Wait(20 / 1000.0));
					dest[i + j] = a[i];
					i++;
				}
				else
				{
					list.addCommand(new Func(function(rect:Rect, tx:int):void { rect.x = tx * SIZE; }, [b[j], i + j]));
					list.addCommand(new Wait(20 / 1000.0));
					dest[i + j] = b[j];
					j++;
				}
			}
			
			return dest;
		}
	}
}

import flash.display.Sprite;
import flash.filters.DropShadowFilter;

class Rect extends Sprite
{
	public var value:int;
	
	public function Rect(size:int)
	{
		graphics.beginFill(0x0);
		graphics.drawRect(0, 0, size, size);
		graphics.endFill();
		
		this.filters = [new DropShadowFilter(2)];
	}
}