Lissajous 3D code on 2009-1-18
package
{
import flash.text.TextField;
import flash.events.Event;
import flash.display.IGraphicsData;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.GraphicsSolidFill;
import flash.display.GraphicsStroke;
import flash.display.GraphicsPath;
import flash.display.GraphicsPathCommand;
import flash.display.LineScaleMode;
import flash.display.CapsStyle;
import flash.display.JointStyle;
import flash.geom.Matrix3D;
import flash.geom.Vector3D;
import flash.geom.Utils3D;
import flash.geom.PerspectiveProjection;
[SWF(width="465", height="465", backgroundColor="0xffffff", frameRate="24")]
public class Lissajous3D extends MovieClip
{
private var screen:Sprite;
private var output:TextField;
private var vertices:Vector.<Number>;
private var path:Vector.<Number>;
private var path2d:Vector.<Number>;
private var commands:Vector.<int>;
private var mat:Matrix3D;
private var perspective:PerspectiveProjection;
private var point3D:Vector3D;
public function Lissajous3D()
{
super();
addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}
public function init():void {
screen = new Sprite();
screen.x = stage.stageWidth * 0.5;
screen.y = stage.stageHeight * 0.5;
screen.z = 100;
addChild(screen);
perspective = new PerspectiveProjection();
perspective.fieldOfView = 60;
vertices = new Vector.<Number>();
commands = new Vector.<int>();
var r:Number = 20;
var th:Number = 2 * Math.PI / 200;
var _x:Number;
var _y:Number;
var _z:Number;
for (var i:int = 0; i < 200; i++) {
_x = r * Math.cos(i * th);
_y = r * Math.sin(i * 2 * th);
_z = r * Math.sin(i * 4 * th);
vertices.push(_x, _y, _z);
commands.push(GraphicsPathCommand.LINE_TO);
}
vertices.push(vertices[0], vertices[1], vertices[2]);
commands.push(GraphicsPathCommand.LINE_TO);
commands[0] = GraphicsPathCommand.MOVE_TO;
mat = new Matrix3D();
mat.identity();
path = new Vector.<Number>();
path2d = new Vector.<Number>();
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(e:Event):void
{
mat.prependRotation(1, Vector3D.Z_AXIS);
mat.prependRotation(1, Vector3D.Y_AXIS);
mat.prependRotation(1, Vector3D.X_AXIS);
var m:Matrix3D = mat.clone();
m.appendTranslation(0, 0, 50);
m.transformVectors(vertices, path);
Utils3D.projectVectors(perspective.toMatrix3D(), path, path2d, new Vector.<Number>());
screen.graphics.clear();
var gp:GraphicsPath = new GraphicsPath(commands, path2d );
var fill:GraphicsSolidFill = new GraphicsSolidFill(0x00ff00);
var stroke:GraphicsStroke
= new GraphicsStroke(5, false, LineScaleMode.NONE,
CapsStyle.ROUND, JointStyle.ROUND, 3, fill);
screen.graphics.drawGraphicsData(Vector.<IGraphicsData>([stroke, gp]));
}
private function addedToStage(e:Event):void
{
removeEventListener(e.type, arguments.callee);
initOutput();
init();
}
private function initOutput():void {
var margin:Number = 10;
addChild(output = new TextField());
output.selectable = false;
output.multiline = true;
output.width = stage.stageWidth - margin * 2;
output.height = stage.stageHeight - margin * 2;
}
public function print(str:String):void {
output.appendText(str);
}
public function println(str:String):void {
output.appendText(str);
output.appendText("\n");
}
}
}