Monthly Archives: April 2009

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 | 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 | 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 | Tagged , | 1 Comment

Slider Navigation

Actionscript:
  1. var thumbNum:Number = 20;
  2. var spacing:Number = 10;
  3. var thumbs:MovieClip = new MovieClip();
  4. addChild(thumbs);
  5. for (var i:int = 0; i<thumbNum; i++){
  6.     var t:MovieClip = new MovieClip();
  7.     with(t.graphics) beginFill(0x666666), drawRect(0,0,100,50);
  8.     t.x = i * (t.width + spacing);
  9.     t.y = 5;
  10.     t.buttonMode = true;
  11.     thumbs.addChild(t);
  12. }
  13. var minX:Number = stage.stageWidth - thumbs.width - spacing;
  14. var destX:Number = thumbs.x = spacing;
  15. var velX:Number = 10;
  16. var stageThird:Number =  stage.stageWidth / 3;
  17. var right:Number = stageThird * 2;
  18. var left:Number  = stageThird;
  19. addEventListener(Event.ENTER_FRAME, onLoop);
  20. function onLoop(evt:Event):void {
  21.     if (mouseX> right){
  22.        destX -= velX;
  23.     }
  24.     if (mouseX <left){
  25.         destX += velX;
  26.     }
  27.     if (destX <minX){
  28.         destX = minX;
  29.     }
  30.     if (destX> spacing){
  31.         destX = spacing;
  32.     }
  33.     thumbs.x += (destX - thumbs.x) /4;
  34. }

This snippet shows a technique for a common type of navigation.

Posted in UI, motion | Tagged , | Leave a comment