Advertisement
spanvega

JOINING_EFLA

Feb 18th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.geom.*;
  4.     import flash.events.*;
  5.     import flash.display.*;
  6.     import flash.filters.*;
  7.     import com.bit101.components.*;
  8.  
  9.     /**  @author SPANVEGA // CHRISTIAN  **/
  10.  
  11.     // [ SWF (width = '465', height = '465', backgroundColor = '0x000000', frameRate = '60')]
  12.  
  13.     public class JOINING_EFLA extends Sprite
  14.     {
  15.         private var w : uint = 465, h : uint = 465;
  16.         private var b : EFLA = new EFLA (w, h, true, 0);
  17.         private var r : Rectangle = new Rectangle (0, 0, w, h);
  18.         private var f : BlurFilter = new BlurFilter (1.25, 1.25, 1);
  19.         private var m : Matrix = new Matrix (), p : Point = new Point ();
  20.         private var t : ColorTransform = new ColorTransform (1, 1, 1, 0.25);
  21.  
  22.         private var connected : Vector.<int> = new Vector.<int>(quantity, true);
  23.         private var quantity_slider : HUISlider, distance_slider : HUISlider;
  24.         private var pressed : Boolean = false, type : int;
  25.  
  26.         private var distance : uint, fade : Number, step : Number, ratio : Number;
  27.         private var quantity : uint = 75, hue : Number = 275;
  28.         private var d : Number, dx : Number, dy : Number;
  29.  
  30.         private var pool : POOL, pa : POINT, pb : POINT;
  31.         private var lines : Array = [], line : LINE;
  32.         private var shape : BitmapData, size : uint;
  33.  
  34.         private var i : int, j : int;
  35.  
  36.  
  37.         public function JOINING_EFLA () { addEventListener ('addedToStage', init); }
  38.  
  39.         private function init (e : Event) : void
  40.         {
  41.             removeEventListener ('addedToStage', init);
  42.  
  43.             stage.scaleMode = 'noScale';
  44.             stage.quality = 'low';
  45.  
  46.             graphics.beginFill (0, 1);
  47.             graphics.drawRect  (0, 0, w, h);
  48.  
  49.             //
  50.  
  51.             var c : uint = 0xFFFFFFFF;
  52.  
  53.             shape = new BitmapData (size = 5, size, true, 0);
  54.             shape.setVector (new Rectangle (0, 0, size, size), new <uint>
  55.             [0, 0, c, 0, 0, 0, c, c, c, 0, c, c, c, c, c, 0, c, c, c, 0, 0, 0, c, 0, 0]);
  56.  
  57.             addChild (new Bitmap (b));
  58.  
  59.             gui ();
  60.  
  61.             //
  62.  
  63.             pool = new POOL (0, 0, w, h);
  64.  
  65.             for (i = 0; i < quantity; i++)
  66.             {
  67.                 pool.add (point ());
  68.             }
  69.  
  70.             _distance ();
  71.  
  72.             //
  73.  
  74.             stage.addEventListener ('mouseDown', press_left);
  75.             stage.addEventListener ('mouseUp',   press_left);
  76.             stage.addEventListener ('rightMouseDown', press_right);
  77.             stage.addEventListener ('rightMouseUp',   press_right);
  78.  
  79.             render (); stage.addEventListener ('enterFrame', render);
  80.         }
  81.  
  82.         private function point () : POINT
  83.         {
  84.             pa = new POINT ();
  85.             pa.px = Math.random () * pool.width;
  86.             pa.py = Math.random () * pool.height;
  87.             pa.vx = -2.5 + Math.random () * 5;
  88.             pa.vy = -2.5 + Math.random () * 5;
  89.             return pa;
  90.         }
  91.  
  92.         private function render (e : Event = null) : void
  93.         {
  94.             if (pressed)
  95.             { check (); }
  96.  
  97.             pool.update ();
  98.  
  99.             lines.length = 0;
  100.  
  101.             b.lock ();
  102.             b.fillRect (r, 0);
  103.  
  104.             for (i = 0; i < quantity; i++)
  105.             {
  106.                 pa = pool.num (i);
  107.  
  108.                 for (j = 0; j < quantity; j++)
  109.                 {
  110.                     if (j == i) break;
  111.  
  112.                     pb = pool.num (j);
  113.  
  114.                     dx = pb.px - pa.px;
  115.                     dy = pb.py - pa.py;
  116.                     d  = Math.sqrt (dx * dx + dy * dy);
  117.  
  118.                     if (d < distance)
  119.                     {
  120.                         ratio = 0xFF;
  121.                         if (d > fade)
  122.                         {
  123.                             ratio = 0x40 + ((distance - d) / step * 0xBE);
  124.                         }
  125.  
  126.                         line = new LINE ();
  127.                         line.px = pa.px; line.py = pa.py;
  128.                         line.tx = pb.px; line.ty = pb.py;
  129.                         line.color = ratio << 24 | hsv2rgb ((d + hue) % 360);
  130.                         lines.push (line);
  131.  
  132.                         connected [i] = connected [j] = 1;
  133.  
  134.                         // SPRING
  135.  
  136.                         pa.vx += dx * 0.00025;
  137.                         pa.vy += dy * 0.00025;
  138.                         pb.vx -= dx * 0.00025;
  139.                         pb.vy -= dy * 0.00025;
  140.                     }
  141.                 }
  142.             }
  143.  
  144.             // Z-SORT BY COLOR LINES
  145.  
  146.             lines.sortOn ('color', Array.NUMERIC);
  147.  
  148.             // RENDER LINES
  149.  
  150.             j = lines.length;
  151.             for (i = 0; i < j; i++)
  152.             {
  153.                 line = lines [i];
  154.  
  155.                 b.line (line.px, line.py, line.tx, line.ty, line.color);
  156.             }
  157.  
  158.             b.applyFilter (b, r, p, f);
  159.  
  160.             // RENDER POINTS
  161.  
  162.             for (i = 0; i < quantity; i++)
  163.             {
  164.                 pa = pool.num (i);
  165.                 m.tx = ~~ (-size * 0.5 + pa.px);
  166.                 m.ty = ~~ (-size * 0.5 + pa.py);
  167.                 b.draw (shape, m, connected [i] == 0 ? t : null);
  168.  
  169.                 // RESET STATUS
  170.  
  171.                 connected [i] = 0;
  172.             }
  173.  
  174.             b.unlock ();
  175.         }
  176.  
  177.         private function check () : void
  178.         {
  179.             if (type == 0)
  180.             {
  181.                 if (pool.len < quantity_slider.maximum)
  182.                 {
  183.                     pa = point ();
  184.                     pa.px = stage.mouseX;
  185.                     pa.py = stage.mouseY;
  186.  
  187.                     pool.add (pa);
  188.                     quantity += 1;
  189.                 }
  190.             }
  191.  
  192.             if (type == 1)
  193.             {
  194.                 if (pool.len > quantity_slider.minimum)
  195.                 {
  196.                     pool.del (pool.len-1);
  197.                     quantity -= 1;
  198.                 }
  199.             }
  200.  
  201.             quantity_slider.value = quantity;
  202.  
  203.             connected = new Vector.<int>(quantity, true);
  204.         }
  205.  
  206.         private function press_left (e : MouseEvent) : void
  207.         {
  208.             type = 0;
  209.  
  210.             if (e.type == 'mouseDown' && stage.mouseY < h - 25)
  211.             {
  212.                 pressed = true;
  213.             }
  214.             if (e.type == 'mouseUp') { pressed = false; }
  215.         }
  216.  
  217.         private function press_right (e : MouseEvent) : void
  218.         {
  219.             type = 1;
  220.  
  221.             if (e.type == 'rightMouseDown' && stage.mouseY < h - 25)
  222.             {
  223.                 pressed = true;
  224.             }
  225.             if (e.type == 'rightMouseUp'){ pressed = false; }
  226.         }
  227.  
  228.         private function gui () : void
  229.         {
  230.             with (Style) { BACKGROUND = LABEL_TEXT = 0xFFFFFF; DROPSHADOW = BUTTON_FACE = 0x000000; }
  231.  
  232.             with (addChild (new Sprite ()))
  233.             { graphics.beginFill (0x404040, 0.25); graphics.drawRect (0, 440, 465, 25); }
  234.  
  235.             distance_slider = new HUISlider (this, 10, 445, 'DISTANCE', _distance);
  236.             with (distance_slider) { setSliderParams (0, 100, 75); width = 160; labelPrecision = 0; tick = 1; }
  237.  
  238.             quantity_slider = new HUISlider (this, 170, 445, 'QUANTITY', _quantity);
  239.             with (quantity_slider) { setSliderParams (0, 100, quantity); width = 160; labelPrecision = 0; tick = 1; }
  240.  
  241.             var h : HUISlider = new HUISlider (this, 327, 445, 'HUE', _hue);
  242.             h.setSliderParams (0, 360, hue); h.width = 160; h.labelPrecision = 0; h.tick = 1;
  243.         }
  244.  
  245.         //
  246.  
  247.         private function _distance (e : Event = null) : void
  248.         {
  249.             distance = distance_slider.value;
  250.  
  251.             fade = distance * 0.75;
  252.             step = distance - fade;
  253.         }
  254.  
  255.         private function _hue (e : Event) : void
  256.         {
  257.             hue = e.target.value;
  258.         }
  259.  
  260.         private function _quantity (e : Event) : void
  261.         {
  262.             var num : int;
  263.  
  264.             quantity = e.target.value;
  265.  
  266.             if (quantity > pool.len)
  267.             {
  268.                 num = quantity - pool.len;
  269.  
  270.                 for (i = 0; i < num; i++)
  271.                 {
  272.                     pool.add (point ());
  273.                 }
  274.             }
  275.             if (quantity < pool.len)
  276.             {
  277.                 num = pool.len - quantity;
  278.  
  279.                 for (i = 0; i < num; i++)
  280.                 {
  281.                     pool.del (pool.len-1);
  282.                 }
  283.             }
  284.  
  285.             connected = new Vector.<int>(quantity, true);
  286.         }
  287.  
  288.         private function hsv2rgb (h : Number = 0, s : Number = 1, v : Number = 1) : uint
  289.         {
  290.             var i : int = int (h / 60);
  291.             var f : Number = h / 60 - i;
  292.             var p : Number = v * (1 - s);
  293.             var q : Number = v * (1 - s * f);
  294.             var t : Number = v * (1 - s * (1 - f));
  295.  
  296.             switch (i)
  297.             {
  298.                 case 0: return v * 0xFF << 16 | t * 0xFF << 8 | p * 0xFF << 0;
  299.                 case 1: return q * 0xFF << 16 | v * 0xFF << 8 | p * 0xFF << 0;
  300.                 case 2: return p * 0xFF << 16 | v * 0xFF << 8 | t * 0xFF << 0;
  301.                 case 3: return p * 0xFF << 16 | q * 0xFF << 8 | v * 0xFF << 0;
  302.                 case 4: return t * 0xFF << 16 | p * 0xFF << 8 | v * 0xFF << 0;
  303.                 case 5: return v * 0xFF << 16 | p * 0xFF << 8 | q * 0xFF << 0;
  304.             }
  305.  
  306.             return 0;
  307.         }
  308.     }
  309. }
  310.  
  311. //
  312.  
  313. final class LINE
  314. {
  315.     public var px : Number, py : Number;
  316.     public var tx : Number, ty : Number;
  317.     public var color : uint;
  318. }
  319.  
  320. //
  321.  
  322. import flash.geom.Rectangle;
  323.  
  324. final class POOL extends Rectangle
  325. {
  326.     private var points : Vector.<POINT>;
  327.  
  328.     private var p : POINT;
  329.  
  330.  
  331.     public function POOL (x : Number, y : Number, width : Number, height : Number)
  332.     {
  333.         super (x, y, width, height);
  334.  
  335.         points = new <POINT> [];
  336.     }
  337.  
  338.     public function update () : void
  339.     {
  340.         var i : int = len;
  341.         while (--i > -1)
  342.         {
  343.             p = points [i];
  344.  
  345.             p.px += p.vx;
  346.             p.py += p.vy;
  347.  
  348.             if (p.px < x) { p.px = x; p.vx = -p.vx; }
  349.             if (p.py < y) { p.py = y; p.vy = -p.vy; }
  350.             if (p.px > x + width)  { p.px = width  + x; p.vx = -p.vx; }
  351.             if (p.py > y + height) { p.py = height + y; p.vy = -p.vy; }
  352.         }
  353.     }
  354.  
  355.     public function add (p : POINT) : void
  356.     {
  357.         points.push (p);
  358.     }
  359.  
  360.     public function del (index : uint) : void
  361.     {
  362.         points.splice (index, 1);
  363.     }
  364.  
  365.     public function mod (index : uint, p : POINT) : void
  366.     {
  367.         points [index] = p;
  368.     }
  369.  
  370.     public function num (index : uint) : POINT
  371.     {
  372.         return points [index];
  373.     }
  374.  
  375.     public function get len () : uint
  376.     {
  377.         return points.length;
  378.     }
  379. }
  380.  
  381. final class POINT
  382. {
  383.     public var px : Number, py : Number;
  384.     public var vx : Number, vy : Number;
  385. }
  386.  
  387. //
  388.  
  389. import flash.display.BitmapData;
  390.  
  391. final class EFLA extends BitmapData
  392. {
  393.     // Extremely Fast Line Algorithm http://www.edepot.com/algorithm.html
  394.  
  395.     public function EFLA (width : uint, height : uint, transparent : Boolean = true, color : uint = 0)
  396.     {
  397.         super (width, height, transparent, color);
  398.     }
  399.  
  400.     public function line (x0 : int, y0 : int, x1 : int, y1 : int, color : uint = 0) : void
  401.     {
  402.         var shortLen : int = y1 - y0;
  403.         var longLen  : int = x1 - x0;
  404.  
  405.         if ((shortLen ^ (shortLen >> 31)) - (shortLen >> 31) > (longLen ^ (longLen >> 31)) - (longLen >> 31))
  406.         {
  407.             shortLen = (longLen = (shortLen = longLen ^ shortLen) ^ longLen) ^ shortLen;
  408.  
  409.             var yLonger : Boolean = true;
  410.         }
  411.         else { yLonger = false; }
  412.  
  413.         var inc : int = longLen < 0 ? -1 : 1;
  414.  
  415.         var multDiff : Number = longLen == 0 ? shortLen : shortLen / longLen;
  416.  
  417.         if (yLonger)
  418.         {
  419.             for (var i : int = 0; i != longLen; i += inc)
  420.  
  421.             setPixel32 (x0 + i * multDiff, y0 + i, color);
  422.         }
  423.         else
  424.         {
  425.             for (i = 0; i != longLen; i += inc)
  426.  
  427.             setPixel32 (x0 + i, y0 + i * multDiff, color);
  428.         }
  429.     }
  430. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement