Advertisement
Guest User

Untitled

a guest
Feb 12th, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package;
  2.  
  3. import flash.display.BitmapData;
  4. import flash.events.TimerEvent;
  5. import flash.geom.Point;
  6. import flash.Lib;
  7. import flash.utils.Timer;
  8.  
  9. class Main {
  10.     var sw:Int;
  11.     var sh:Int;
  12.    
  13.     var particles:Array<Particle>;
  14.    
  15.     var comx:Float;
  16.     var comy:Float;
  17.    
  18.     var qs:Array<Point>;
  19.    
  20.     var angles:Array<Float>;
  21.    
  22.     function new(stage:flash.display.Stage) {
  23.         sw = stage.stageWidth;
  24.         sh = stage.stageHeight;
  25.  
  26.         particles = [];
  27.        
  28.         // initialise particles in a square
  29.  
  30.         var len = 60;
  31.         var x = sw/2;
  32.         var y = 200;
  33.         particles.push(new Particle(x, y));
  34.         particles.push(new Particle(x+len, y));
  35.         particles.push(new Particle(x+len, y+len));
  36.         particles.push(new Particle(x, y+len));
  37.        
  38.         // initial center of mass
  39.        
  40.         comx = comy = 0;
  41.         for (p in particles) {
  42.             comx += p.x;
  43.             comy += p.y;
  44.         }
  45.         comx /= particles.length;
  46.         comy /= particles.length;
  47.        
  48.         // displacement vectors
  49.        
  50.         qs = [];
  51.         for (p in particles) {
  52.             qs.push(new Point(p.x-comx, p.y-comy));
  53.         }
  54.        
  55.         // initial angle displacements from center of mass
  56.        
  57.         angles = [];
  58.         for (i in 0...particles.length) {
  59.             angles[i] = Math.atan2(particles[i].y-comy, particles[i].x-comx);
  60.         }
  61.        
  62.        
  63.  
  64.         var timer:Timer = new Timer(1000/60);
  65.         timer.addEventListener(TimerEvent.TIMER, step);
  66.         timer.start();
  67.     }
  68.  
  69.     function step(e:TimerEvent):Void {
  70.         var p = particles[0];
  71.         p.x -= (p.x-Lib.current.mouseX);
  72.         p.y -= (p.y-Lib.current.mouseY);
  73.  
  74.         // new center of mass
  75.        
  76.         comx = comy = 0;
  77.         for (p in particles) {
  78.             comx += p.x;
  79.             comy += p.y;
  80.         }
  81.         comx /= particles.length;
  82.         comy /= particles.length;
  83.  
  84.         // average angle
  85.        
  86.         var angleDelta = 0.;
  87.    
  88.         for (i in 0...particles.length) {
  89.             var px = particles[i].x-comx;
  90.             var py = particles[i].y-comy;
  91.            
  92.             var angle = Math.atan2(py, px);
  93.            
  94.             var deltaTemp = angle-angles[i];
  95.             if (deltaTemp < -Math.PI) deltaTemp += 2*Math.PI;
  96.             if (deltaTemp > Math.PI) deltaTemp -= 2*Math.PI;
  97.             angleDelta += deltaTemp;
  98.         }
  99.        
  100.         angleDelta /= particles.length;
  101.        
  102.  
  103.         // rotate particles around initial displacement by the angle
  104.        
  105.         var cos = Math.cos(angleDelta);
  106.         var sin = Math.sin(angleDelta);
  107.        
  108.         for (i in 0...particles.length) {
  109.             var p = particles[i];
  110.            
  111.             var q = qs[i];
  112.            
  113.             var gx = cos*q.x-sin*q.y;
  114.             var gy = sin*q.x+cos*q.y;
  115.            
  116.             gx += comx;
  117.             gy += comy;
  118.            
  119.             p.x = gx;
  120.             p.y = gy;
  121.            
  122.             // p.x += (gx-p.x)*.1;
  123.             // p.y += (gy-p.y)*.1;
  124.         }
  125.        
  126.        
  127.         // render
  128.        
  129.         Lib.current.graphics.clear();
  130.         Lib.current.graphics.lineStyle(0);
  131.        
  132.         for (p in particles) {
  133.             Lib.current.graphics.beginFill(0);
  134.             Lib.current.graphics.drawCircle(p.x, p.y, 2);
  135.             Lib.current.graphics.endFill();
  136.         }
  137.        
  138.         Lib.current.graphics.lineStyle(0, 0, .5);
  139.         Lib.current.graphics.moveTo(particles[0].x, particles[0].y);
  140.         Lib.current.graphics.lineTo(comx, comy);
  141.        
  142.         Lib.current.graphics.lineStyle(0, 0, 0);
  143.         Lib.current.graphics.beginFill(0xFF0000);
  144.         Lib.current.graphics.drawCircle(comx, comy, 3);
  145.         Lib.current.graphics.endFill();
  146.     }
  147.  
  148.     static function main() {
  149.         new Main(Lib.current.stage);
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement