Quick Pixel Sphere

Actionscript:
  1. var pointNum:int = 20000;
  2. var radius:int = 150;
  3.  
  4. var canvas:BitmapData = new BitmapData(400,400,false, 0x000000);
  5. addChild(new Bitmap(canvas));
  6. var verts:Vector.<Number>  = new Vector.<Number>();
  7. var pVerts:Vector.<Number> = new Vector.<Number>();
  8. var uv:Vector.<Number> = new Vector.<Number>();
  9.  
  10. for (var i:int = 0; i<pointNum; i+=3){
  11.     var xp:Number = Math.random() * 400 - 200;
  12.     var yp:Number = Math.random() * 400 - 200;
  13.     var zp:Number = Math.random() * 400 - 200;
  14.     var dist:Number = Math.sqrt(xp * xp + yp * yp + zp * zp);
  15.     // normalize and scale x,y,z
  16.     verts[i] = xp / dist * radius;
  17.     verts[i+1] = yp / dist * radius;
  18.     verts[i+2] = zp / dist * radius;
  19. }
  20.  
  21. var m:Matrix3D = new Matrix3D();
  22. var dx:Number = 0, dy:Number = 0;
  23. addEventListener(Event.ENTER_FRAME, onLoop);
  24. function onLoop(evt:Event):void {
  25.        m.identity();
  26.        dx += (mouseX - dx) / 4;
  27.        dy += (mouseY - dy) / 4;
  28.        m.appendRotation(dx, Vector3D.X_AXIS);
  29.        m.appendRotation(dy, Vector3D.Y_AXIS);
  30.        m.appendTranslation(200,200,0);
  31.        Utils3D.projectVectors(m, verts, pVerts, uv);
  32.        canvas.fillRect(canvas.rect, 0x000000);
  33.        for (var i:int = 0; i<pVerts.length; i+=2){
  34.          canvas.setPixel(pVerts[i], pVerts[i + 1], 0xFFFFFF);
  35.        }
  36. }

This snippet shows a quick way to randomly place a bunch of xyz coordinates on the surface of a sphere. I saw this trick in an OpenGL book a few years back - dug around my books but couldn't find it... If I find it I'll update this post.

The trick is achieved by normalizing the vector defined by each 3D coordinate...


Have a look at the swf...

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

4 Comments

  1. Posted July 26, 2009 at 8:52 pm | Permalink

    Hi Zevan

    it’s not related but i found this fun reading for me (via @NeuroProd) http://www.cs.brown.edu/courses/cs123/lectures.htm

  2. Posted July 27, 2009 at 6:33 am | Permalink

    that looks like its a very good link… thanks katopz

  3. Posted July 30, 2009 at 2:16 pm | Permalink

    Hi, awesome demos. I particularly like the spring one and how it can mess with your depth perception. This random point generator is slightly biased. In fact you can see a rough outline of a cube superimposed on the sphere. You can avoid this by throwing away all points which were originally greater than 200 units from the origin.

  4. Posted July 30, 2009 at 3:41 pm | Permalink

    Hey andre, glad you like the demos… there are a few other ways than throwing away points… but I actually like that aspect of this demo…. you could even use the same technique as the sphere of squares post from a few days back…. you could also use a little trig which is basically the same thing as using Matrix3D….

    Anyway… I was wondering if someone would notice that… you have a good eye, I just think it’s cool that normalizing the vectors creates a sphere… first time I saw that trick was in some OpenGL book that I think I lost (because I can’t seem to find it).

Post a Comment

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

*
*