/* ========================================================================== */ /* Integer-partition demo: */ /* Copyright (c) 2009 Laurens Rodriguez Oscanoa */ /* http://www.opensource.org/licenses/mit-license.php */ /* -------------------------------------------------------------------------- */ package { import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFormat; // Compiled with Flex 3.2.0. [SWF(backgroundColor="#FFFFFF", frameRate="30", width="640", height="330")] public class partitions extends Sprite { private const N:int = 8; private var count:int = 0; public function partitions() { // Draw all the integer partitions of [N] f(new Array(), N); } private function f(pa:Array, m:int):void { // Determine value of [z] // ---------------------- // Because the array [pa] represents a partition in ascending order // the maximum term of [pa] would be the last array element. // In case the array is empty, [z] would be 1. var z:int = (pa.length > 0)? pa[pa.length - 1] : 1; // Recursive process // ----------------- for (var k:int = z; k <= m; ++k) { // Create new temporal array [pt] adding [k] as last element to [pa]. // In AS3 we first need to create a copy of array [pa]. var pt:Array = pa.concat(); pt.push(k); if (m == k) { // If (m - k) == 0, draw partition [pt], addChild(Graphics.node(25 + (28 * count), 50, 12, String(count + 1), 0xFFFFFF, 0xCCBB00, 0x887700)); addChild(Graphics.strip(25 + (28 * count), 80, 28, 11, pt, Graphics.warmColor(), 0x998800)); ++count; } else { // in other case continue recursion. f(pt, m - k); } } } } } import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFormat; class Graphics { // Return a "warm" color. public static function warmColor():Number { var b:int = 192 + (16 * Math.random()); var r:int = 240 + (16 * Math.random()); var g:int = 224 + (16 * Math.random()); return (r << 16 | g << 8 | b); } // Return a "strip", that is a list of nodes joined by a line. public static function strip(x:int, y:int, offset:int, radius:Number, list:Array, colorBody:Number, colorBorder:Number, vertical:Boolean = true, colorText:Number = 0, borderSize:Number = 0.5, font:String = "Verdana"):Sprite { var strip:Sprite = new Sprite(); strip.graphics.lineStyle(3 * borderSize, colorBorder); strip.graphics.beginFill(colorBody); strip.graphics.moveTo(x, y); if (vertical) { strip.graphics.lineTo(x, y + (offset * (list.length - 1))); } else { strip.graphics.lineTo(x + (offset * (list.length - 1)), y); } strip.graphics.endFill(); var node:Sprite; for (var k:int = 0; k < list.length; ++k) { if (vertical) { node = Graphics.node(x, y + (offset * k), radius, list[k], colorBody, colorBorder, colorText, borderSize, font); } else { node = Graphics.node(x + (offset * k), y, radius, list[k], colorBody, colorBorder, colorText, borderSize, font); } strip.addChild(node); } return strip; } // Return a "node", that is a circle with a label. public static function node(x:int, y:int, radius:Number, caption:String, colorBody:Number, colorBorder:Number, colorText:Number = 0, borderSize:Number = 0.5, font:String = "Verdana"):Sprite { var format:TextFormat = new TextFormat(); format.font = font; format.color = colorText; format.size = 12; format.bold = true; var label:TextField = new TextField(); label.selectable = false; label.defaultTextFormat = format; label.text = caption; label.x = -2*label.textWidth/3; // need to fix this ad-hoc solution label.y = -2*label.textHeight/3; // for centering text. var node:Sprite = new Sprite(); node.graphics.lineStyle(borderSize, colorBorder); node.graphics.beginFill(colorBody); node.graphics.drawCircle(0, 0, radius); node.graphics.endFill(); node.addChild(label); node.x = x; node.y = y; return node; } }