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...

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

5 Comments

  1. Posted April 21, 2009 at 8:09 pm | Permalink

    Hi Zevon,

    I get 3 errors when checking this one out?

    I’m on a different computer using CS3. I’ll check it out on my computer tomorrow in CS4 to see if it works then.

    Best
    Gayle

  2. Posted April 22, 2009 at 5:07 am | Permalink

    A lot of really nice snippets, very good inspiration!

    One little thing though, could you perhaps use a syntax-highlighter without the linenumbers?

    Its quite cumbersome to paste a script and then have to remove all the numbers - at least thats what happens for me on XP when pasting to Flash IDE.

    Cheers
    Mads

  3. Posted April 22, 2009 at 8:49 am | Permalink

    @gayle - it will only work in CS4… since it used Vector and flash 10 3D stuff.

    @Mads - just click on the “Plain Text” button above the code and you’ll be able to copy and paste without line nums… I will however be switching to a new code highlighter that will not require the “Plain Text” button :) … sometime in the next few days…

  4. Gayle
    Posted April 22, 2009 at 1:22 pm | Permalink

    Thanks Zevon!

    I am eating my words! I figured, regarding the the 3D stuff.

    Really is a beautiful piece. Love these Action Snippets. It’s a goodie a day!

    Gayle

  5. Posted April 23, 2009 at 12:49 am | Permalink

    Ah okay, hadn’t even noticed that that was a clickable button, just looked like a header - thanks :-)

One Trackback

  1. [...] kan dreje sig om alt fra 3D eksperimenter til hvordan man tegner bestemt tandhjul eller hvordan man kan finde kvadratroden, hvis man nu bare [...]

Post a Comment

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

*
*