Gumdrop Torus

Actionscript:
  1. var matrix:Matrix3D = new Matrix3D();
  2.  
  3. var verts:Vector.<Number> = new Vector.<Number>();
  4. var pVerts:Vector.<Number> = new Vector.<Number>();
  5. var uvts:Vector.<Number> = new Vector.<Number>();
  6.  
  7. for (var i:Number = -2; i<2; i+=.04){
  8.     for (var j:Number = -2; j<2; j+=.04){
  9.         for (var k:Number = -2; k<2; k+=.04){
  10. // equation from: http://local.wasp.uwa.edu.au/~pbourke/geometry/gumdrop/
  11.             var yz:Number = j * j + k * k;
  12.              var s:Number = 4 * (Math.pow(i,4) + Math.pow(yz,2))
  13.              + 17 * i * i *(yz) - 20 * (yz + i * i) + 17;
  14.             if (s <0 && s> -0.5){
  15.                 verts.push(i * 60);
  16.                 verts.push(j * 60);
  17.                 verts.push(k * 60);
  18.                 pVerts.push(0), pVerts.push(0);
  19.                 uvts.push(0), uvts.push(0), uvts.push(0);
  20.              }
  21.         }
  22.     }
  23. }
  24.  
  25. var tVerts:Vector.<Number> = new Vector.<Number>();
  26. matrix.appendRotation(90, Vector3D.X_AXIS);
  27. matrix.appendRotation(45, Vector3D.Y_AXIS);
  28. matrix.appendScale(1.7, 1.7, 1.7);
  29. matrix.transformVectors(verts, tVerts);
  30.  
  31. var p:Point = new Point();
  32. var brush:BitmapData=new BitmapData(3,3,true,0x41FFFFFF);
  33. var canvas:BitmapData = new BitmapData(500,500,false, 0x000000);
  34. addChild(new Bitmap(canvas));
  35. var dx:Number=0;
  36. var dy:Number=0;
  37. addEventListener(Event.ENTER_FRAME, onLoop);
  38. function onLoop(evt:Event):void {
  39.     dx += (mouseX - dx)/4;
  40.     dy += (mouseY - dy)/4;
  41.     matrix.identity();
  42.     matrix.appendRotation(dy,Vector3D.X_AXIS);
  43.     matrix.appendRotation(dx,Vector3D.Y_AXIS);
  44.     matrix.appendTranslation(250, 250, 0);
  45.     Utils3D.projectVectors(matrix, tVerts, pVerts, uvts);
  46.     canvas.lock();
  47.     canvas.fillRect(canvas.rect, 0x000000);
  48.     var inc:int = 0;
  49.     for (var i:int = 0; i<pVerts.length; i+=2){
  50.      
  51.         p.x = pVerts[i];
  52.         p.y = pVerts[i+1];
  53.         canvas.copyPixels(brush, brush.rect, p, null, null, true);
  54.     }
  55.     canvas.unlock();
  56. }

Felt like revisiting implicit surface plotting today...

Have a look at the swf over at wonderfl.net

This entry was posted in 3D, BitmapData, Math and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*