flash on 2015-2-16

by mutantleg
♥0 | Line 124 | Modified 2015-02-16 02:20:55 | MIT License
play

ActionScript3 source code

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

package {
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
 
          graphics.lineStyle(2, 0);
           var i:int; var a:xVert;
           for (i = 0; i < 32; i++)
           {
             a = new xVert();
             a.cx = 32+ Math.random() * 400;
             a.cy = 32+Math.random() * 400;
             vecVert.push(a);
             
             graphics.drawCircle(a.cx,a.cy, 8);   
           }//nexti
           
           var num:int;
           num = vecVert.length;
           for (i = 0; i < num; i++)
           {
             a = vecVert[i];
             connVert(a);
           }   
           
           vecCol = new Vector.<uint>(256,false);
           for (i = 0; i < 256; i++)
           { vecCol[i] = ( Math.random()* 0xFFffFFff ); }
           
           
           floodGroup();
            
           stage.addEventListener(Event.ENTER_FRAME, onEnter); 
        }//ctor
        
        public var vecVert:Vector.<xVert> = Vector.<xVert>([]); 

        public var vecCol:Vector.<uint>;
        
        public function connVert(v:xVert):void
        {
           var i:int; var a:xVert;           var num:int;
           num = vecVert.length;
           v.num = 0;
           for (i = 0; i < num; i++)
           {
             a = vecVert[i];
             if (a == v) { continue; }    
             if (getMag(a.cx - v.cx, a.cy - v.cy) > 60) { continue; }
             //v.vecVert.push(a);          
             v.vecVert[v.num] = a; v.num += 1;
             if (v.num >= v.vecVert.length) { continue; }  
             graphics.moveTo(a.cx, a.cy);
             graphics.lineTo(v.cx, v.cy);
           }
            
        }//conn
        
        public var ctest:int = 1;
        public var vecTemp:Vector.<xVert> = new Vector.<xVert>(2048,false); 
        public function floodGroup():void
        {
          var vec:Vector.<xVert>;  var it:int;
          var vr:Vector.<xVert>; var num:int; var i:int;
          var a:xVert; var b:xVert;
          var group:int; var k:int; var nk:int;
          var c:uint;  
          
          group = 1;
          ctest += 1;
          vec = vecTemp;
          
          nk = vecVert.length;          
          
          for (k = 0; k< nk;k++)
          {
           a = vecVert[k];
              if (a.test == ctest) { continue; }
              vec[0] = a;
              it = 1; 
              
              group += 1; if (group >= 128) { return; }
              //c = Math.random()*0xFFffFF;
              c = vecCol[group];
              
              while (it > 0)
              {
               it -= 1;
               a = vec[it];
               a.group = group;
               
               graphics.beginFill(c, 1);
                graphics.drawCircle(a.cx,a.cy, 8);
               graphics.endFill();
         
                  vr = a.vecVert;  num = a.num; // vr.length;
                  for (i = 0; i < num; i++)
                  {
                    b = vr[i];
                    if (b.test == ctest) { continue; } 
                     b.test = ctest;
                     vec[it] = b;    
                    it += 1; 
        
                    if (it >= 2048) {it=2048; return;}        
                  }//nexti
              }//wend 
          }//nextk
        }//flood
               
        public function getMag(ax:Number, ay:Number):Number
        { return Math.sqrt(ax*ax + ay*ay); }
        
        public var gt:int = 0;
        public function onEnter(e:Event):void
        {
            
            //less trippy on lower framerate
            gt += 1;
            if (gt % 8 != 0) { return; }
            
            graphics.clear();
            graphics.lineStyle(2, 0);

           //var c:uint;
           var i:int; var a:xVert;           var num:int;
           num = vecVert.length;
           for (i = 0; i < num; i++)
           {
             a = vecVert[i];
    
            a.cx += (Math.random()-0.5) * 16;
            a.cy += (Math.random()-0.5) * 16;

            if (a.cx < 0) {a.cx += 465;}
            if (a.cy < 0) {a.cy += 465;}
            if (a.cx > 465) {a.cx -= 465;}
            if (a.cy > 465) {a.cy -= 465;}
    
             graphics.drawCircle(a.cx,a.cy, 8);   
             connVert(a);
           }//nexti

           floodGroup();
            
        }//onenter
        

        
    }//classend
}

internal class xVert
{
   public var cx:Number = 0;
   public var cy:Number = 0;
    
   public var vecVert:Vector.<xVert> = new Vector.<xVert>(16, false); 
   
   public var num:int = 0;
   
   public var group:int = 0;
   
   public var test:int = 0;
   
}//xvert