Fl036_Main.as (Main)

Fl036_Main.as
package {
    import flash.display.Sprite;
    import com.borealkiss.controls.ViewController;
    import com.borealkiss.controls.HexagonCloudController;

    [SWF(width="550",height="400",backgroundColor="0x0")]
    public class Fl036_Main extends Sprite{
        public function Fl036_Main(){
            var canvas:Sprite = new Sprite();
            this.addChild(canvas);
            var controller:ViewController = new HexagonCloudController(canvas);
            controller.draw();
        }
    }
}

com.borealkiss.controls.ViewController.as (Abstract)

ViewController.as
/**
 * @date 21/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.controls{
    import flash.display.DisplayObjectContainer;
    import flash.errors.IllegalOperationError;
    
    /**
     * Abstract class
     */ 
    public class ViewController{
        protected var _target:DisplayObjectContainer;
        
        /**
         * Constructor
         */     
        public function ViewController(target:DisplayObjectContainer){
            _target = target;
            
            if (Object(this) == ViewController){
                throw new IllegalOperationError(
                    "Abstract class must be inherited in a subclass");
            }
        }
        
        public function draw():void{
            //Must be overridden in a subclass.
        }
    }
}

com.borealkiss.controls.HexagonCloudController.as

HexagonCloudController.as
/**
 * @date 25/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.controls{
    import flash.display.DisplayObjectContainer;
    import com.borealkiss.controls.ViewController;
    import com.borealkiss.display.HexagonCloud;
    
    public class HexagonCloudController extends ViewController{
        protected const STAGE_WIDTH:Number = 550;
        protected const STAGE_HEIGHT:Number = 400;
        
        //How many clouds.
        protected const LIMIT:uint = 100;
        
        //Radius (or edge length) of the hexagon.
        private const RADIUS:Number = 5;
        private var GRID_X:Number = 3*RADIUS;
        private var GRID_Y:Number = Math.sqrt(3)*RADIUS;
        
        /**
         * Constructor
         */ 
        public function HexagonCloudController(target:DisplayObjectContainer){
            super(target);
        }
        
        override public function draw():void{
            for (var i:int=0; i<LIMIT; i++){
                var color:uint = 0xFFFFFF*Math.random();
                var cloud:HexagonCloud = new HexagonCloud(RADIUS,color,0.1,20,true,color);
                var randomX:Number = STAGE_WIDTH*Math.random();
                var randomY:Number = STAGE_HEIGHT*Math.random();
                var remainderX:Number = randomX % GRID_X;
                var x:Number = int(randomX / GRID_X) * GRID_X;
                var y:Number = int(randomY / GRID_Y) * GRID_Y;
                
                if ( remainderX % (GRID_X/2) < 1){
                    x += GRID_X/2;
                    y += GRID_Y/2;
                }
                
                cloud.x = x;
                cloud.y = y;
                _target.addChild(cloud);
            }
        }
    }
}

com.borealkiss.display.HexagonCloud.as

HexagonCloud.as
/**
 * @date 25/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display{
    import flash.display.Sprite;
    import com.borealkiss.display.shapes.Hexagon;
    
    /**
     * Draw a small group of the same colored hexagons.
     */ 
    public class HexagonCloud extends Sprite{
        protected var _radius:uint;
        protected var _stroke:uint;
        protected var _alpha:Number;
        //How many hexagons in a cloud.
        protected var _limit:uint;
        protected var _filling:Boolean;
        protected var _fillColor:uint;
        protected var _hexagons:Array;
        //Number of the edge of a hexagon.
        private const EDGES:int = 6;
        
        /**
         * Constructor
         */ 
        public function HexagonCloud(radius:Number,stroke:uint=0x0,alpha:Number=0.1,
                                limit:uint=5,filling:Boolean=false,fillColor:uint=0x0){
            
            _radius = radius;
            _stroke = stroke;
            _alpha = alpha;
            _limit = limit;
            _filling = filling;
            _fillColor = fillColor;
            this.init();
        }
        
        protected function init():void{
            _hexagons = new Array();
            
            //First hexagon must be set in the center.
            var firstHex:Hexagon = new Hexagon(_radius,_stroke,_alpha,_filling,_fillColor);
            this.x = this.y = firstHex.x = firstHex.y = 0;
            _hexagons.push(firstHex);
            
            var limit:int = _limit;
            while (limit > 0){
                var parentNum:int = int(Math.random()*(_hexagons.length));
                var parentHex:Hexagon = _hexagons[parentNum];
                
                //Direction of the center of a child hex from the parent center.
                var theta:Number = int(Math.random()*EDGES)*(Math.PI/3) + Math.PI/6;
                
                var childHex:Hexagon = new Hexagon(_radius,_stroke,_alpha,_filling,_fillColor);
                childHex.x = parentHex.x + Math.sqrt(3)*(_radius)*Math.cos(theta);
                childHex.y = parentHex.y + Math.sqrt(3)*(_radius)*Math.sin(theta);
                _hexagons.push(childHex);
                limit--;
            }
            this.draw();
        }
        
        protected function draw():void{
            for (var i:int=0; i<_hexagons.length; i++){
                this.addChild(_hexagons[i]);
            }
        }
    }
}

com.borealkiss.display.shapes.Hexagon.as

Hexagon.as
/**
 * @date 25/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display.shapes{
    import flash.display.Shape;
    import flash.display.Graphics;
    
    /**
     * Draws a hexagon.
     */ 
    public class Hexagon extends Shape{
        //Number of the edge of a hexagon.
        private const EDGES:int = 6;
        
        /**
         * Constructor
         */ 
        public function Hexagon(radius:Number,stroke:uint=0x0,alpha:Number=0.1,
                                filling:Boolean=false,fillColor:uint=0x0){
                                    
            var g:Graphics = this.graphics;
            g.lineStyle(0,stroke,alpha);
            
            if (filling){
                g.beginFill(fillColor,alpha);
            }
            
            for (var i:int=0; i<=EDGES; i++){
                var theta:Number = 2*Math.PI*i/EDGES;
                var x:Number = radius*Math.cos(theta);
                var y:Number = radius*Math.sin(theta);
                
                if (i == 0){
                    g.moveTo(x,y);
                }
                else {
                    g.lineTo(x,y);
                }
            }
        }
    }
}
Powered by boreal-kiss.com 2008.