Tag Archives: actionscript

Fracture a Number

Actionscript:
  1. var target:Number = 1024;
  2. var slices:Array = [target];
  3. var leng:int = 11;
  4.  
  5. for (var i:int = 0; i<leng-1; i++){
  6.      var index:int = int(Math.random()*slices.length);
  7.      var val:Number = slices[index];
  8.      var rand:Number = Math.random() * val/2;
  9.      slices[index] = val - rand;
  10.      slices.push(rand);
  11. }
  12.  
  13. trace(slices);
  14.  
  15. // test that they all add up
  16. var sum:Number = 0;
  17. for (i = 0; i<slices.length; i++){
  18.     sum += slices[i];
  19. }
  20. trace("test that they all add up: ", sum);

The above snippet creates an array of a specified length whose elements all add up to the variable target. Here is some example output:


165.31133050055192,322.23456030456015,
257.47582363389245,26.9984893942173,1.96283924962002,
5.466277873168191,21.362282634705164,62.68168197512457,
76.63028224500404,36.27274381401516,12.558309228795265,35.04537914634583
test that they all add up: 1024

Posted in arrays, misc | Also tagged | Leave a comment

Random Walk to Target

Actionscript:
  1. var target:Number = 360;
  2. var steps:Array = new Array();
  3. for (var step:Number = 0; step <target; step += int(Math.random() * 36 + 36)){
  4.     steps.push(Math.min(target,step));
  5. }
  6. steps.push(target);
  7. trace(steps);
  8. /* outputs something similar to:
  9. 0,46,99,144,189,259,330,360
  10. */

This is something I've had to do a few times recently.... it randomly steps a number toward a given target...

Posted in arrays, misc | Also tagged | Leave a comment

Tooth Curve (modified Bicuspid)

Actionscript:
  1. var canvas:BitmapData=new BitmapData(400,400,false,0xCCCCCC);
  2. addChild(new Bitmap(canvas));
  3.  
  4. var a:Number=-1.21;
  5. var r:Rectangle=new Rectangle(0,0,3,5);
  6. var halfWidth:Number=canvas.width/2;
  7. var halfHeight:Number=canvas.height/2;
  8.  
  9. render();
  10.  
  11. function render():void{
  12.     for (var x:Number = -2; x<=2; x+=.05) {
  13.         for (var y:Number = -2; y<=2; y+=.05) {
  14.    
  15.             // equation from : http://en.wikipedia.org/wiki/Bicuspid_curve
  16.             //(x^2 - a^2) * (x - a)^2 + (y^2 - a^2) * (y^2 - a^2) = 0
  17.    
  18.             // unpoptimized:
  19.             // var e:Number = (x*x - a*a) * (x-a)*(x-a) + (y*y-a*a) * (y*y-a*a);  
  20.             // optimized:
  21.             var x_a:Number=x-a;
  22.             // factoring: x^2 - a^2 = (x + a) * (x - a)
  23.             var y2_a2:Number =  (y + a) * (y - a);
  24.             var e:Number = (x + a) * x_a * x_a * x_a +  y2_a2 * y2_a2;
  25.    
  26.             // tolerance beetween .7 & -.1;
  27.             if (e<.7&&e>-.1) {
  28.                 r.x=halfWidth+y*50;
  29.                 r.y=halfHeight-x*100;
  30.                 canvas.fillRect(r, 0x000000);
  31.             }
  32.         }
  33.     }
  34. }

I've been looking for a single math equation to draw a tooth for sometime.... today I stumbled upon this... and realized it could probably be changed to look more like a tooth.

This snippet will draw this:

I'm using a graphing method here that allows me to use the Cartesian form of the equation and not the parameteric... I'll explain a bit more about this in a later post.

Posted in BitmapData, Math | Also tagged | 1 Comment

BitmapData Snapshot Grid

Actionscript:
  1. [SWF(width=600,height=650)]
  2. var canvas:BitmapData=Bitmap(addChild(new Bitmap(new BitmapData(600,300,false,0xCCCCCC),"auto",true))).bitmapData;
  3. var stills:BitmapData=Bitmap(addChild(new Bitmap(new BitmapData(600,380,false,0xAAAAAA),"auto",true))).bitmapData;
  4. getChildAt(1).y=300;
  5.  
  6. var c:Shape = new Shape();
  7. var m:Matrix = new Matrix();
  8. m.createGradientBox(40, 40, 0, 0, 0);
  9. c.graphics.beginGradientFill(GradientType.RADIAL, [0xCC0000, 0xCC0000],  [1, 0], [0, 255], m);
  10. c.graphics.drawCircle(20,20,20);
  11.  
  12. addEventListener(Event.ENTER_FRAME, onLoop);
  13. function onLoop(evt:Event):void {
  14.     c.x=mouseX-c.width/2;
  15.     c.y=mouseY-c.height/2;
  16.     canvas.draw(c, c.transform.matrix);
  17. }
  18.  
  19. var spacing:Number = 10;
  20. var cols:Number = 4;
  21. var max:Number = cols * cols;
  22. var size:Number = 1/(canvas.width/((canvas.width / cols) - spacing));
  23. var st:Matrix = new Matrix();
  24. st.scale(size, size);
  25. var w:Number = canvas.width * st.d  + spacing;
  26. var h:Number = canvas.height * st.d + spacing;
  27. var timer:Timer=new Timer(500);
  28. timer.start();
  29. timer.addEventListener(TimerEvent.TIMER, onCapture);
  30. function onCapture(evt:TimerEvent):void {
  31.     var inc:int = timer.currentCount - 1;
  32.     st.tx = (inc% cols) * w+ spacing / 2;
  33.     st.ty = int(inc / cols) * h + spacing;
  34.     stills.draw(canvas, st);
  35.     if (timer.currentCount==max) {
  36.         timer.reset();
  37.         timer.start();
  38.     }
  39. }

Take snapshots of a given BitmapData and arrange them in a grid. I wrote this snippet quickly in response to a question so it could probably use a little clean up...

You'll need to move your mouse over the large canvas bitmap (drawing to it) to see anything...

Posted in BitmapData | Also tagged | Leave a comment