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

drawTriangles

Get Adobe Flash player
by cpu_t 17 Aug 2010
    Embed
/**
 * Copyright cpu_t ( http://wonderfl.net/user/cpu_t )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/d1Jh
 */

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    
    public class Main extends Sprite
    {
        private var _points:Vector.<Number>;
        private var _indeces:Vector.<int>;
        private var _vertices:Vector.<Number>;
        
        public function Main():void
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point
            
            _points = new Vector.<Number>();
            _indeces = new Vector.<int>();
            var n:int = 11;
            for (var i:int = 0; i < n*n; i++)
            {
                _points.push(i % n * stage.stageWidth / (n - 1), Math.floor(i / n) * stage.stageHeight / (n - 1));
                if (i % n < n - 1 && Math.floor(i / n) < n - 1)
                {
                    _indeces.push(i, i + 1, i + n);
                    _indeces.push(i + 1, i + n, i + n + 1);
                }
            }
            _vertices = _points.concat();
            
            addEventListener(Event.ENTER_FRAME, _onEnterFrame);
        }
        
        private function _onEnterFrame(e:Event):void
        {
            for (var i:int = 0; i < _vertices.length; i+=2)
            {
                var dx:Number = mouseX - _points[i];
                var dy:Number = mouseY - _points[i+1];
                var distance:Number = Math.sqrt(dx * dx + dy * dy);
                _vertices[i] = _points[i] - dx / distance * 50;
                _vertices[i+1] = _points[i+1] - dy / distance * 50;
            }
            
            graphics.clear();
            graphics.lineStyle(1, 0x808080);
            graphics.drawTriangles(_vertices, _indeces);
        }
        
    }
    
}