Tag Archives: actionscript

Paste an Arbitrary Piece of Code

Actionscript:
  1. ...
  2. // loop through until we find the root note
  3.     // grab the third and the fifth and exit the loop
  4.     for (var i:int = 0; i<leng; i++){
  5.         if (cMajor[i] == note){
  6.             third = cMajor[(i + 2) % leng];
  7.             fifth = cMajor[(i + 4) % leng];
  8.             break;
  9.         }
  10.     }
  11.    
  12.     // we may need a double sharp on the middle note
  13.     var sharpFlatDouble:String = sharpFlat;
  14.    
  15.     // check if this is a sharp, check if it is A or D
  16.     // if it is add the symbol for double sharp
  17.     if (sharpFlat == "#"){
  18.         if (note == "D" || note == "A"){
  19.             sharpFlatDouble = "x";
  20.         }
  21.     }
  22. ...

If your working on some code... just randomly copy a piece of it and paste it in the comments... This code is from a program that generates any major scale (it's still not finished). Feel free to post code chunks in any language...

[EDIT] the code doesn't need to work on its own... you can just randomly copy from something your working on...

Posted in misc | Also tagged , | 15 Comments

QuickBox2D Mini-Poly Editor

Actionscript:
  1. [SWF(width = 800, height = 600, frameRate = 60)]
  2. import com.actionsnippet.qbox.*;
  3. stage.frameRate = 60;
  4.  
  5. var sim:QuickBox2D = new QuickBox2D(this);
  6.  
  7. sim.createStageWalls();
  8.  
  9. sim.start();
  10.  
  11. var output:TextField = new TextField();
  12. output.text = "Click anywhere to add points to a polygon. Hit any key to test.\n\n";
  13. output.x = output.y = 50;
  14. with(output) width = 300, height = 400, border = true, selectable = true, wordWrap = true, multiline = true;
  15. addChild(output);
  16.  
  17. function display(str:*):void{
  18.     output.appendText(str.toString() + "\n");
  19. }
  20.                                
  21. var points:Array = [];
  22. var poly:Shape = new Shape();
  23. addChild(poly);
  24.  
  25. stage.addEventListener(MouseEvent.CLICK, onClick);
  26. stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
  27.  
  28. function onClick(evt:MouseEvent):void {
  29.     if (points.length == 0){
  30.         poly.graphics.beginFill(0xCCCCCC);
  31.         poly.graphics.lineStyle(1, 0xFF0000);
  32.         poly.graphics.moveTo(mouseX, mouseY);
  33.     }else{
  34.         poly.graphics.lineTo(mouseX, mouseY);
  35.     }
  36.     poly.graphics.drawCircle(mouseX, mouseY, 2);
  37.      
  38.     points.push(mouseX / 30.0, mouseY / 30.0);
  39. }
  40.  
  41. function onKeyPressed(evt:KeyboardEvent):void {
  42.      // average all points
  43.      var avgX:Number=0
  44.      var avgY:Number = 0;
  45.      
  46.      for (var i:int = 0; i<points.length; i+=2){
  47.          avgX += points[i];
  48.          avgY += points[i + 1];
  49.      }
  50.    
  51.      avgX /= points.length/2;
  52.      avgY /=  points.length/2;
  53.      avgX = avgX;
  54.      avgY = avgY;
  55.      
  56.      // subtract averages and fix decimal place
  57.       for (i = 0; i<points.length; i+=2){
  58.           var yp:int = i + 1;
  59.           points[i] -= avgX;
  60.           points[yp] -= avgY;
  61.           points[i] = Number(points[i].toFixed(2));
  62.           points[yp] = Number(points[yp].toFixed(2));
  63.      }
  64.      
  65.      display("points array:");
  66.      display(points);
  67.      
  68.      try{
  69.          var p:QuickObject = sim.addPoly({x:avgX, y:avgY, points:points});
  70.          p.userData.graphics.beginFill(0xFF0000);
  71.          p.userData.graphics.drawCircle(0,0,5);
  72.      }catch(e:*){
  73.         display("Invalid polygon data!");
  74.      }
  75.      
  76.      poly.graphics.clear();
  77.      points = [];
  78. }

This snippet shows the basic concepts needed to go about creating a polygon editor for QuickBox2D. I have an unreleased editor that I use for my QuickBox2D projects, at some point I may release it... but for now I figured I'd post this extremely simplified version for people to expand on.


Have a look at the swf here...

Posted in Box2D, QuickBox2D | Also tagged , , , | 26 Comments

Float as a Fraction

Actionscript:
  1. // print some floats as fractions
  2. printFraction(0.5);
  3. printFraction(0.75);
  4. printFraction(0.48);
  5.  
  6. // try something a little more complex
  7. var float:Number = 0.98765432;
  8. trace("\na more complex example:");
  9. printFraction(float);
  10. var frac:Array = asFraction(float);
  11. trace("double check it:");
  12. trace(frac[0] + "/" + frac[1] +" = " + frac[0] / frac[1]);
  13.  
  14. /* outputs:
  15. 0.5 = 1/2
  16. 0.75 = 3/4
  17. 0.48 = 12/25
  18.  
  19. a more complex example:
  20. 0.98765432 = 12345679/12500000
  21. double check it:
  22. 12345679/12500000 = 0.98765432
  23. */
  24.  
  25.  
  26. function printFraction(n:Number):void{
  27.     var frac:Array = asFraction(n);
  28.     trace(n + " = " + frac[0] + "/" + frac[1]);
  29. }
  30.  
  31. // takes any value less than one and returns an array
  32. // with the numerator at index 0 and the denominator at index 1
  33. function asFraction(num:Number):Array{
  34.     var decimalPlaces:int = num.toString().split(".")[1].length;
  35.     var denom:Number = Math.pow(10, decimalPlaces);
  36.     return reduceFraction(num * denom, denom);
  37. }
  38.  
  39. // divide the numerator and denominator by the GCF
  40. function reduceFraction(numerator:int, denominator:Number):Array{
  41.     // divide by the greatest common factor
  42.     var divisor:int = gcf(numerator, denominator);
  43.     if (divisor){
  44.         numerator /= divisor;
  45.         denominator /= divisor;
  46.     }
  47.     return [numerator, denominator];
  48. }
  49.                    
  50. // get the greatest common factor of two integers
  51. function gcf(a:int, b:int):int{
  52.     var remainder:int;
  53.     var factor:Number = 0;
  54.     var maxIter:int = 100;
  55.     var i:int = 0;
  56.     while (1){
  57.         if (b> a){
  58.            var swap:int = a;
  59.            a = b;
  60.            b = swap;
  61.         }
  62.         remainder = a % b;
  63.         a = b;
  64.         b = remainder
  65.         if (remainder == 0){
  66.             factor = a;
  67.             break;
  68.         }else if (remainder==1){
  69.             break;
  70.         }else if (i> maxIter){
  71.             trace("failed to calculate gcf");
  72.             break;
  73.         }
  74.         i++;
  75.     }
  76.     return factor;
  77. }

This snippet contains a few functions for calculating fractions based on float values. Writing this brought back some memories from grade school math.

Posted in Math, misc | Also tagged , | 4 Comments

Greatest Common Factor (divisor)

Actionscript:
  1. trace(gcf(75,145));
  2.  
  3. // outputs 5
  4.  
  5. function gcf(a:int, b:int):int{
  6.     var remainder:int;
  7.     var factor:Number = 0;
  8.     while (1){
  9.         if (b> a){
  10.            var swap:int = a;
  11.            a = b;
  12.            b = swap;
  13.         }
  14.         remainder = a % b;
  15.         a = b;
  16.         b = remainder
  17.         if (remainder == 0){
  18.             factor = a;
  19.             break;
  20.         }else if (remainder==1){
  21.          
  22.             break;
  23.         }
  24.     }
  25.     return factor;
  26. }

I was messing around with Egyptian Fractions and found myself in need of some interesting functions. The first function I realized I would be needing was for a greatest common factor (GCF or GCD).

The above snippet is a quick implementation of Euclid's algorithm. It will return 0 if no GCF is found...

I wrote a few helper functions while working with Egyptian fractions... will post them over the next few days.

Posted in Math | Also tagged , | 4 Comments