Category Archives: Math

Gradient Tooth

Actionscript:
  1. var canvas:BitmapData=new BitmapData(400,400,false,0x000000);
  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+=.01) {
  13.         for (var y:Number = -2; y<=2; y+=.02) {
  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.                 r.x=halfWidth+y*50;
  27.                 r.y=halfHeight-x*100;
  28.                 var col:Number = e * 50;
  29.                 if (col <10){
  30.                     col = Math.abs(col) + 70;
  31.                     canvas.fillRect(r, col <<16 | col <<8 | col );
  32.                 }
  33.         }
  34.     }
  35. }

This is a variation on a post from a little while back.... it plots a modified Bicuspid that resembles a tooth:

Also posted in pixel manipulation, setPixel | Tagged , | Leave a comment

Swirl Gradient

Actionscript:
  1. var canvas:BitmapData=new BitmapData(200,200,false,0x000000);
  2. addChild(new Bitmap(canvas));
  3. scaleX=scaleY=2;
  4. var pixNum:int=canvas.width*canvas.height;
  5.  
  6. var xp:Vector.<int> = new Vector.<int>();
  7. var yp:Vector.<int> = new Vector.<int>();
  8. var radius:Vector.<Number> = new Vector.<Number>();
  9. var theta:Vector.<Number> = new Vector.<Number>();
  10. for (var i:int = 0; i<pixNum; i++) {
  11.     xp.push(i % 200);
  12.     yp.push(int(i / 200));
  13.     var dx:Number=100-xp[i];
  14.     var dy:Number=100-yp[i];
  15.     theta.push(Math.atan2(dy, dx));
  16.     radius.push(Math.sqrt(dx * dx + dy * dy)/20);
  17. }
  18.  
  19. addEventListener(Event.ENTER_FRAME, onLoop);
  20. function onLoop(evt:Event):void {
  21.     canvas.lock();
  22.     var n:Number = mouseX / 100;
  23.     for (var i:int = 0; i<pixNum; i++) {
  24.         var swirl:Number = 1+Math.sin(6*Math.cos(radius[i]) -n*theta[i]);
  25.         canvas.setPixel(xp[i], yp[i],  Math.abs(255 - swirl * 255));
  26.     }
  27.     canvas.unlock();
  28. }

This snippet creates a swirl gradient. While this snippet is not highly optimized, it does implement a basic optimization technique ... I cache some repetitive calculations in Vectors and then use them on the main loop.

I got the function for a swirl gradient from mathworld...

Also posted in BitmapData, pixel manipulation, setPixel | Tagged , | Leave a comment

3D Hypocycloid Shape

Actionscript:
  1. var matrix:Matrix3D = new Matrix3D();
  2. var verts:Vector.<Number> = new Vector.<Number>();
  3. var pVerts:Vector.<Number> = new Vector.<Number>();
  4. var uvts:Vector.<Number> = new Vector.<Number>();
  5. const TWO_PI:Number=Math.PI * 2;
  6. var step:Number=.05;
  7.  
  8. var brush:BitmapData = new BitmapData(3, 2, true, 0x41FFFFFF);
  9. var n:Number=8;
  10. var xp:Number=0,yp:Number=0,a:Number=12,t:Number=0;
  11. for (var i:Number = 0; i<TWO_PI; i+=step) {
  12.     for (var j:Number = 0; j<TWO_PI; j+=step) {
  13.         // unoptimized for readability
  14.     var cosi:Number = a/n * ((n - 1) * Math.cos(i) + Math.cos(Math.abs((n - 1) * i)));
  15.     var sini:Number = a/n * ((n - 1) * Math.sin(i) - Math.sin(Math.abs((n - 1) * i)));
  16.     var cosj:Number = a/n * ((n - 1) * Math.cos(j) + Math.cos(Math.abs((n - 1) * j)));
  17.     var sinj:Number = a/n * ((n - 1) * Math.sin(j) - Math.sin(Math.abs((n - 1) * j)));
  18.         verts.push(cosi * cosj);
  19.         verts.push(sini * cosj);
  20.         verts.push(a * sinj);
  21.         pVerts.push(0),pVerts.push(0);
  22.         uvts.push(0),uvts.push(0),uvts.push(0);
  23.     }
  24. }
  25. var canvas:BitmapData=new BitmapData(400,400,false,0x000000);
  26. addChild(new Bitmap(canvas));
  27. var dx:Number=0;
  28. var dy:Number=0;
  29. addEventListener(Event.ENTER_FRAME, onLoop);
  30. function onLoop(evt:Event):void {
  31.     dx += (mouseX - dx)/4;
  32.     dy += (mouseY - dy)/4;
  33.     matrix.identity();
  34.     matrix.appendRotation(dy,Vector3D.X_AXIS);
  35.     matrix.appendRotation(dx,Vector3D.Y_AXIS);
  36.     matrix.appendTranslation(200, 200, 0);
  37.     Utils3D.projectVectors(matrix, verts, pVerts, uvts);
  38.     canvas.lock();
  39.     canvas.fillRect(canvas.rect, 0x000000);
  40.     var p = new Point();
  41.     for (var i:int = 0; i<pVerts.length; i+=2) {
  42.         p.x = pVerts[i];
  43.         p.y = pVerts[i + 1];
  44.         canvas.copyPixels(brush, brush.rect, p, null, null, true);
  45.     }
  46.     canvas.unlock();
  47. }

Taking the Hypocycloid stuff from yesterday into 3D...

Also posted in 3D, BitmapData | Tagged , | 6 Comments

Hypocycloid Spiral

Actionscript:
  1. x = stage.stageWidth / 2;
  2. y = stage.stageHeight / 2;
  3. // change n to alter number of spikes (cuspes)
  4. var n:Number = 8;
  5. var xp:Number = 0, yp:Number = 0, a:Number = 10, t:Number = 0;
  6.  
  7. graphics.lineStyle(0, 0x000000);
  8. addEventListener(Event.ENTER_FRAME, onLoop);
  9. function onLoop(evt:Event):void {
  10.     for (var i:int = 0; i<10; i++){
  11.       // unoptimized for simplicity and similarity to original equations from here:
  12.           // http://mathworld.wolfram.com/Hypocycloid.html
  13.       xp = a/n * ((n - 1) * Math.cos(t) + Math.cos(Math.abs((n - 1) * t)));
  14.       yp = a/n * ((n - 1) * Math.sin(t) - Math.sin(Math.abs((n - 1) * t)))  ;
  15.      
  16.       a *= 1.002;
  17.        if (t == 0){
  18.            graphics.moveTo(xp, yp);
  19.        }else{
  20.            graphics.lineTo(xp, yp);  
  21.        }
  22.        t += .1;
  23.     }
  24. }

This code draws a Hypocycloid with a radius that increments - this results in a spiral formation.

I got the math from mathworld... here.

Posted in Math | Tagged , | 2 Comments