Actionscript:
-
var matrix:Matrix3D = new Matrix3D();
-
var verts:Vector.<Number> = new Vector.<Number>();
-
var pVerts:Vector.<Number> = new Vector.<Number>();
-
var uvts:Vector.<Number> = new Vector.<Number>();
-
var sqrt2:Number = Math.sqrt(2)
-
var pi:Number = Math.PI;
-
var pi23:Number= 2 * Math.PI / 3;
-
var step:Number= pi / 50;
-
for (var u:Number = -pi; u<pi; u+=step) {
-
for (var v:Number = -pi; v<pi; v+=step) {
-
// from here http://local.wasp.uwa.edu.au/~pbourke/geometry/hexatorus/
-
var px = Math.sin(u) / Math.abs(sqrt2+ Math.cos(v))
-
var py = Math.sin(u+pi23) / Math.abs(sqrt2 +Math.cos(v + pi23))
-
var pz = Math.cos(u-pi23) / Math.abs(sqrt2 +Math.cos(v - pi23))
-
verts.push(px * 50);
-
verts.push(py * 50);
-
verts.push(pz * 50);
-
}
-
}
-
var brush:BitmapData=new BitmapData(3,2,true,0x41FFFFFF);
-
var canvas:BitmapData=new BitmapData(400,400,false,0x000000);
-
addChild(new Bitmap(canvas));
-
var dx:Number=0;
-
var dy:Number=0;
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
dx += (mouseX - dx)/4;
-
dy += (mouseY - dy)/4;
-
matrix.identity();
-
matrix.appendRotation(dy,Vector3D.X_AXIS);
-
matrix.appendRotation(dx,Vector3D.Y_AXIS);
-
matrix.appendTranslation(200, 200, 0);
-
Utils3D.projectVectors(matrix, verts, pVerts, uvts);
-
canvas.lock();
-
canvas.fillRect(canvas.rect, 0x000000);
-
var p = new Point();
-
for (var i:int = 0; i<pVerts.length; i+=2) {
-
p.x = pVerts[i];
-
p.y = pVerts[i+1];
-
canvas.copyPixels(brush, brush.rect, p, null, null, true);
-
}
-
canvas.unlock();
-
}
More 3D shapes inspired by (taken from) Paul Bourke's website. I figured I should probably add a parametric 3D surface to this set of posts - so I chose to plot the Triaxial Hexatorus. It's important to note that U and V go from -PI to PI.
I googled Triaxial Hexatorus and stumbled upon a great papervision demo that creates meshes using parametric equations - it also morphs between shapes.
I also noticed that I don't need to populate the uvts or pVerts Vectors with zeros - seems to work fine without doing that.