Category Archives: misc

sinh & cosh

Actionscript:
  1. function sinh(x:Number):Number{
  2.     return (Math.pow(Math.E, x) - Math.pow(Math.E, -x)) * 0.5;
  3. }
  4.  
  5. function cosh(x:Number):Number{
  6.     return (Math.pow(Math.E, x) + Math.pow(Math.E, -x)) * 0.5;
  7. }

Needed sinh and cosh today. Easy enough to create with existing math functions. If you needed more speed you could inline these and replace Math.E with 2.71828183.

Got the math over at wikipedia (as usual).

Also posted in Math | Tagged , | Leave a comment

Supershapes / Superformula

Actionscript:
  1. // Superformula (equations from):
  2. // http://www.geniaal.be/downloads/AMJBOT.pdf
  3. // http://en.wikipedia.org/wiki/Superformula
  4. const TWO_PI:Number = Math.PI * 2;
  5. function superShape(a:Number, b:Number, m:Number, n1:Number, n2:Number, n3:Number, pnt:Point, scale:Number):void{
  6.     var r:Number = 0
  7.     var p:Number = 0;
  8.     var xp:Number = 0, yp:Number = 0;
  9.     while(p <= TWO_PI){
  10.         var ang:Number = m * p / 4;
  11.         with(Math){
  12.             r = pow(pow(abs(cos(ang) / a), n2) + pow(abs(sin(ang) / b), n3),-1/n1);
  13.             xp = r * cos(p);
  14.             yp = r * sin(p);
  15.         }
  16.         p += .01;
  17.         canvas.setPixel(pnt.x + xp *scale, pnt.y + yp * scale,  0xFFFFFF);
  18.      }
  19. }
  20. // test it out:
  21. var canvas:BitmapData = new BitmapData(700,600,false, 0x000000);
  22. addChild(new Bitmap(canvas, "auto", true));
  23.  
  24. superShape(1, 1, 5, 23, 23, 23, new Point(100,80), 30);
  25. superShape(1, 1, 5, 13, 13, 3, new Point(200,80), 30);
  26. superShape(1, 1, 8, 3, 13, 3, new Point(300,80), 30);
  27. superShape(10,8, 16, 30, 13, 3, new Point(450,80), 30);
  28. superShape(1,1, 1, .5, .5, .5, new Point(100,190), 100);
  29.  
  30. for (var i:int = 0; i <150; i++){
  31.   superShape(1,1, 2, 1+i/800, 4, 8-i * .1, new Point(550,350), 50);
  32. }
  33. for (i = 0; i <20; i++){
  34.   superShape(1.1,1.2, 6, 2 + i , 4, 9 - i, new Point(200,350), 50);
  35. }

The above snippet demos a function that will draw Supershapes using the Superformula...

From wikipedia:
The Superformula appeared in a work by Johan Gielis. It was obtained by generalizing the superellipse, named and popularized by Piet Hein...

Here is the result of the above code:


You can read more about the Superformula here in the original paper by Gielis.

wikipedia entry...

3d Supershapes by Paul Bourke

Also posted in Math, graphics algorithms, setPixel | Tagged , | Leave a comment

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

Also posted in arrays | 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...

Also posted in arrays | Tagged , | Leave a comment