Category Archives: Math

Plot of sinh()

Actionscript:
  1. x = stage.stageWidth / 2;
  2. y = stage.stageHeight / 2;
  3. scaleX = scaleY = 3;
  4.  
  5. var t:Number =-5;
  6. var xp:Number = 0;
  7. var yp:Number = 0;
  8.  
  9. addEventListener(Event.ENTER_FRAME, onLoop);
  10. function onLoop(evt:Event):void {
  11.      xp = t;
  12.      yp = sinh(t);
  13.      
  14.     graphics.lineStyle(0,0);
  15.     if (t == -5){
  16.         graphics.moveTo(xp, yp);
  17.     }else{
  18.         graphics.lineTo(xp, yp);
  19.     }
  20.      if (t> 5){
  21.          removeEventListener(Event.ENTER_FRAME, onLoop);
  22.      }
  23.      t+=.2;
  24. }
  25. function sinh(x:Number):Number{
  26.     return (Math.pow(Math.E, x) - Math.pow(Math.E, -x)) * 0.5;
  27. }

Just a quick plot to test the sinh() function from a few days ago...

Posted in Math | Tagged , | Leave a comment

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 misc | Tagged , | Leave a comment

Babylonian Method for Square Root

Actionscript:
  1. var xn:Number = 1;
  2. var xn1:Number = 0
  3. var square:Number =39;
  4.  
  5. trace("find the sqrt of ", square);
  6. trace("Math.sqrt: ", Math.sqrt(square))
  7.  
  8. // no starting approximation, just try up to 35 iterations
  9. for(var i:int = 0; i<35; i++){
  10.     xn1 = .5 * (xn  + square / xn);
  11.     if (xn1== xn){
  12.         trace("other sqrt: ", xn1);
  13.         break;
  14.     }
  15.     xn = xn1;
  16. }
  17. /*outputs
  18. find the sqrt of  39
  19. Math.sqrt:  6.244997998398398
  20. other sqrt:  6.244997998398398
  21. */

I was reading about calculating square roots - not for speed optimization purposes, just to see some of the different ways to go about it. The above snippet uses the Babylonian method to find the square root of a number. I left out the first step of guessing at the square root... so this snippet is by no means efficient...

Posted in Math | Tagged , | 2 Comments

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 graphics algorithms, misc, setPixel | Tagged , | Leave a comment