Monthly Archives: June 2012

Gumowski/Mira Pseudo-Soundwave

Actionscript:
  1. [SWF(width = 600, height = 600)]
  2. var a:Number = 0.02;
  3. var b:Number = .9998;
  4.  
  5. var xn1:Number = 5;
  6. var yn1:Number = 0;
  7. var xn:Number, yn:Number;
  8.  
  9. var scale:Number = 10;
  10. var iterations:Number = 20000;
  11. var step:Number = stage.stageWidth / iterations;
  12.  
  13. function f(x:Number):Number{
  14.     var x2:Number = x * x;
  15.     return a * x + (2 * (1 - a) * x2) / (1 + x2);
  16. }
  17.  
  18. var canvas:BitmapData = Bitmap(addChild(new Bitmap(new BitmapData(600,600,false,0xEFEFEF)))).bitmapData;
  19.  
  20. var circle = new Sprite();
  21. with(circle.graphics) beginFill(0, 0.3), drawCircle(2,2,1);
  22.  
  23. var dot:BitmapData = new BitmapData(4,4,true, 0x00000000);
  24. dot.draw(circle);
  25.  
  26. var pnt:Point = new Point();
  27.  
  28. var txt:TextField = TextField(addChild(new TextField()));
  29. txt.text = "move mouse";
  30.                                                       
  31. addEventListener(Event.ENTER_FRAME, onLoop);
  32. function onLoop(evt:Event):void {
  33.    
  34.     canvas.fillRect(canvas.rect, 0xEFEFEF);
  35.  
  36.     a = mouseY / 1000;
  37.     xn1 = mouseX / 30;
  38.     yn1 = 0;
  39.     for (var i:int = 0; i<iterations; i++){
  40.           xn = xn1;
  41.           yn = yn1;
  42.          
  43.           xn1 = b * yn + f(xn);
  44.           yn1 =  -xn + f(xn1);
  45.           pnt.x = i * step;
  46.           pnt.y = 300 + yn1 * scale;
  47.           canvas.copyPixels(dot, dot.rect, pnt, null, null, true);
  48.        
  49.     }
  50. }

Try it out:

Gumowski Mira Pseudo-soundwave - wonderfl build flash online

Posted in BitmapData, Graphics, Math, graphics algorithms | Leave a comment

Closest Point on a Line

Actionscript:
  1. // from
  2. // http://www.gamedev.net/topic/444154-closest-point-on-a-line/
  3.  
  4. function getClosestPoint(A:*, B:*, P:*, segmentClamp:Boolean=true):Point {
  5.     var AP:Point = new Point(P.x - A.x, P.y - A.y),
  6.         AB:Point = new Point(B.x - A.x, B.y - A.y);
  7.     var ab2:Number=AB.x*AB.x+AB.y*AB.y;
  8.     var ap_ab:Number=AP.x*AB.x+AP.y*AB.y;
  9.     var t:Number=ap_ab/ab2;
  10.     if (segmentClamp) {
  11.         if (t<0.0) {
  12.             t=0.0;
  13.         } else if (t> 1.0) {
  14.             t=1.0;
  15.         }
  16.  
  17.     }
  18.     return new Point(A.x + AB.x * t, A.y + AB.y * t);
  19. }
  20.  
  21.  
  22. var point:Sprite = Sprite(addChild(new Sprite()));
  23. point.x = 50;
  24. point.y = 50;
  25. with(point.graphics) beginFill(0), drawCircle(0,0, 3);
  26.  
  27. var line:MovieClip = MovieClip(addChild(new MovieClip()));
  28. line.a = new Point(20, 100);
  29. line.b = new Point(300, 60);
  30. with(line.graphics) lineStyle(0), moveTo(line.a.x, line.a.y), lineTo(line.b.x, line.b.y);
  31.  
  32. var closestPoint:Point = getClosestPoint(line.a, line.b, point);
  33.  
  34. var closest:Sprite = Sprite(addChild(new Sprite()));
  35. closest.x = closestPoint.x;
  36. closest.y = closestPoint.y;
  37. with(closest.graphics) beginFill(0xFF0000), drawCircle(0,0, 3);

Posted in Math | Leave a comment