3Draw

this is a prototype of my 3D Drawing application. It offers all x, y and z views in which to draw and a 3D view to see it. Use the mousewheel to adjust depth of drawing plane and click and drag the 3d view to rotate.

I’m trapped at work so I used this very effective online compiler and here is my source:

Linky


class Draw3D {
 var obj;
 var root;
 var dim_x;
 var dim_y;
 var dim_z;
 var dim_3d;
 var ghost;
 var fl;
 var rad;
 var x_txt;
 var y_txt;
 var z_txt;
 var txt_3d;
 function Draw3D(path) {
  fl = 300;
  rad = 200;
  root = path.createEmptyMovieClip("root", 5);
  obj = new Array();
  dim_x = path.createEmptyMovieClip("dim_x", 1);
  dim_x._x = 100;
  dim_x._y = 100;
  dim_x.z = 0;
  dim_z = path.createEmptyMovieClip("dim_z", 2);
  dim_z._x = 300;
  dim_z._y = 100;
  dim_z.z = 0;
  dim_y = path.createEmptyMovieClip("dim_y", 3);
  dim_y._x = 100;
  dim_y._y = 300;
  dim_y.z = 0;
  dim_3d = path.createEmptyMovieClip("dim_3d", 4);
  dim_3d._x = 300;
  dim_3d._y = 300;
  dim_3d.x_angle = 0;
  dim_3d.y_angle = 0;
  dim_3d.zcenter = 200;
  //create depth fields
  root.createTextField("x_txt", 6, 30, 5, 100, 20);
  root.x_txt.selectable = false;
  root.x_txt.variable = "dim_x.z";
  root.createTextField("y_txt", 7, 230, 5, 100, 20);
  root.y_txt.selectable = false;
  root.y_txt.variable = "dim_z.z";
  root.createTextField("z_txt", 8, 30, 205, 100, 20);
  root.z_txt.selectable = false;
  root. z_txt.variable = "dim_y.z";
  root.createTextField("txt_3d", 9, 205, 205, 100, 20);
  root.txt_3d.selectable = false;
  root.txt_3d.variable = "dim_3d.zcenter";
  ghost = path.createEmptyMovieClip("ghost", 10);
  //mouse events
  var ref = this;
  var mouseListener = new Object();
  mouseListener.dim_x = dim_x;
  mouseListener.dim_z = dim_z;
  mouseListener.dim_y = dim_y;
  mouseListener.dim_3d = dim_3d;
  mouseListener.ref = this;
  mouseListener.onMouseWheel = function(delta) {
   if(this.dim_x._xmouse > -100 && this.dim_x._ymouse > -100 && this.dim_x._xmouse < 100 && this.dim_x._ymouse < 100) {
    this.dim_x.z -= delta*5;
   } else if(this.dim_y._xmouse > -100 && this.dim_y._ymouse > -100 && this.dim_y._xmouse < 100 && this.dim_y._ymouse < 100) {
    this.dim_y.z -= delta*5;
   } else if(this.dim_z._xmouse > -100 && this.dim_z._ymouse > -100 && this.dim_z._xmouse < 100 && this.dim_z._ymouse < 100) {
    this.dim_z.z -= delta*5;
   } else if(this.dim_3d._xmouse > -100 && this.dim_3d._ymouse > -100 && this.dim_3d._xmouse < 100 && this.dim_3d._ymouse < 100) {
    this.dim_3d.zcenter -= delta*5;
   }
   ref.updateViews();
  }
  Mouse.addListener(mouseListener);
  dim_x.onPress = function () {
   ref.onPressHandler(this);
  }
  ;
  dim_x.onRelease = dim_x.onReleaseOutside = function () {
   ref.onReleaseHandler(this);
  }
  ;
  dim_y.onPress = function () {
   ref.onPressHandler(this);
  }
  ;
  dim_y.onRelease = dim_y.onReleaseOutside = function () {
   ref.onReleaseHandler(this);
  }
  ;
  dim_z.onPress = function () {
   ref.onPressHandler(this);
  }
  ;
  dim_z.onRelease = dim_z.onReleaseOutside = function () {
   ref.onReleaseHandler(this);
  }
  ;
  dim_3d.onPress = function () {
   ref.press3D(this);
  }
  ;
  root.lineStyle(2, 0, 10);
  root.moveTo(200, 0);
  root.lineTo(200, 400);
  root.moveTo(0, 200);
  root.lineTo(400, 200);
  root.lineStyle(0, 0, 100);
  drawLetter(root, 5, 5, "x");
  drawLetter(root, 205, 5, "y");
  drawLetter(root, 5, 205, "z");
  //create axis lines
  var node1;
  var node2;
  var line;
  node1 = new Array(-100, 0, 0);
  node2 = new Array(100, 0, 0);
  line = new Array(node1, node2, 0x000000, 20);
  obj.push(line);
  //
  node1 = new Array(0, -100, 0);
  node2 = new Array(0, 100, 0);
  line = new Array(node1, node2, 0x000000, 20);
  obj.push(line);
  //
  node1 = new Array(0, 0, -100);
  node2 = new Array(0, 0, 100);
  line = new Array(node1, node2, 0x000000, 20);
  obj.push(line);
  updateViews();
 }
 // End of the function
 function updateViews() {
  dim_x.clear();
  dim_y.clear();
  dim_z.clear();
  dim_3d.clear();
  drawRects();
  for (var line in obj) {
   var node1 = obj[line][0];
   var node2 = obj[line][1];
   var scale;
   dim_x.lineStyle(0, obj[line][2], obj[line][3]);
   dim_y.lineStyle(0, obj[line][2], obj[line][3]);
   dim_z.lineStyle(0, obj[line][2], obj[line][3]);
   dim_3d.lineStyle(0, obj[line][2], obj[line][3]);
   scale = fl/(fl+dim_x.z-node1[0]);
   dim_x.moveTo(Math.min(100, Math.max(-100, node1[1]*scale)), Math.min(100, Math.max(-100, node1[2]*scale)));
   dim_x.lineTo(Math.min(100, Math.max(-100, node2[1]*scale)), Math.min(100, Math.max(-100, node2[2]*scale)));
   scale = fl/(fl+dim_y.z-node1[1]);
   dim_y.moveTo(Math.min(100, Math.max(-100, node1[2]*scale)), Math.min(100, Math.max(-100, node1[0]*scale)));
   dim_y.lineTo(Math.min(100, Math.max(-100, node2[2]*scale)), Math.min(100, Math.max(-100, node2[0]*scale)));
   scale = fl/(fl+dim_z.z-node1[2]);
   dim_z.moveTo(Math.min(100, Math.max(-100, node1[1]*scale)), Math.min(100, Math.max(-100, node1[0]*scale)));
   dim_z.lineTo(Math.min(100, Math.max(-100, node2[1]*scale)), Math.min(100, Math.max(-100, node2[0]*scale)));
   draw3DLine(dim_3d, node1, node2, dim_3d.x_angle, dim_3d.y_angle, 0, dim_3d.zcenter);
  }
  // end of for...in
 }
 // End of the function
 function press3D(target) {
  target.oldx = target._xmouse;
  target.oldy = target._ymouse;
  var ref = this;
  target.onMouseMove = function () {
   ref.on3DMove(this);
  }
  ;
  target.onRelease = target.onReleaseOutside = function () {
   delete this.onMouseMove;
  }
  ;
 }
 // End of the function
 function on3DMove(target) {
  var _loc3;
  var _loc2;
  _loc2 = target.oldx - target._xmouse;
  _loc3 = target.oldy - target._ymouse;
  target.x_angle = target.x_angle - _loc3 / 100;
  target.y_angle = target.y_angle + _loc2 / 100;
  target.oldx = target._xmouse;
  target.oldy = target._ymouse;
  updateViews();
 }
 // End of the function
 function onPressHandler(target) {
  target.drawing = true;
  target.sx = target._xmouse;
  target.sy = target._ymouse;
  ghost.clip = target;
  ghost.onMouseMove = function () {
   if (this.clip.drawing != false) {
    this.clear();
    this.lineStyle(0, 0, 35);
    this.moveTo(this.clip.sx + this.clip._x, this.clip.sy + this.clip._y);
    this.lineTo(this.clip._xmouse + this.clip._x, this.clip._ymouse + this.clip._y);
   } else {
    this.clear();
    delete this.onMouseMove;
   }
   // end else if
  }
  ;
 }
 // End of the function
 function onReleaseHandler(target) {
  if (target.drawing) {
   target.drawing = false;
   if (target._xmouse > -100 && target._ymouse > -100 && target._xmouse < 100 && target._ymouse < 100) {
    var _loc4 = new Array();
    var _loc3 = new Array();
    var _loc2 = new Array();
    if (target._name == "dim_x") {
     _loc4.push(target.z, target.sx, target.sy);
     _loc3.push(target.z, target._xmouse, target._ymouse);
    } else if (target._name == "dim_y") {
     _loc4.push(target.sy, target.z, target.sx);
     _loc3.push(target._ymouse, target.z, target._xmouse);
    } else if (target._name == "dim_z") {
     _loc4.push(target.sy, target.sx, target.z);
     _loc3.push(target._ymouse, target._xmouse, target.z);
    }
    // end else if
    _loc2.push(_loc4);
    _loc2.push(_loc3);
    _loc2.push(0x000000, 100);
    obj.push(_loc2);
    updateViews();
   }
   // end if
  }
  // end if
 }
 // End of the function
 function drawRects() {
  drawRect(dim_x, -100, -100, 100, 100, 16777215);
  drawRect(dim_y, -100, -100, 100, 100, 16777215);
  drawRect(dim_z, -100, -100, 100, 100, 16777215);
  drawRect(dim_3d, -100, -100, 100, 100, 16777215);
 }
 // End of the function
 function draw3DLine(target, node1, node2, x_angle, y_angle, ycenter, zcenter) {
  var _loc17;
  var _loc15;
  var _loc22;
  var _loc3;
  var _loc2;
  var _loc13;
  var _loc4;
  var _loc16;
  var _loc14;
  var _loc1;
  var _loc8 = Math.sin(x_angle);
  var _loc10 = Math.cos(x_angle);
  var _loc6 = Math.sin(y_angle);
  var _loc9 = Math.cos(y_angle);
  var _loc5 = 0;
  var _loc7 = 1;
  _loc3 = _loc10 * node1[1] - _loc8 * node1[2];
  _loc2 = _loc8 * node1[1] + _loc10 * node1[2];
  _loc13 = _loc9 * _loc2 - _loc6 * node1[0];
  _loc4 = _loc6 * _loc2 + _loc9 * node1[0];
  _loc16 = _loc7 * _loc4 - _loc5 * _loc3;
  _loc14 = _loc5 * _loc4 + _loc7 * _loc3;
  _loc1 = fl / (fl + _loc13 + zcenter);
  _loc17 = _loc16 * _loc1;
  _loc15 = (_loc14+ycenter) * _loc1;
  target.moveTo(Math.min(100, Math.max(-100, _loc17)), Math.min(100, Math.max(-100, _loc15)));
  _loc3 = _loc10 * node2[1] - _loc8 * node2[2];
  _loc2 = _loc8 * node2[1] + _loc10 * node2[2];
  _loc13 = _loc9 * _loc2 - _loc6 * node2[0];
  _loc4 = _loc6 * _loc2 + _loc9 * node2[0];
  _loc16 = _loc7 * _loc4 - _loc5 * _loc3;
  _loc14 = _loc5 * _loc4 + _loc7 * _loc3;
  _loc1 = fl / (fl + _loc13 + zcenter);
  _loc17 = _loc16 * _loc1;
  _loc15 = (_loc14+ycenter) * _loc1;
  target.lineTo(Math.min(100, Math.max(-100, _loc17)), Math.min(100, Math.max(-100, _loc15)));
 }
 // End of the function
 function drawRect(target, x1, y1, x2, y2, c) {
  target.lineStyle(undefined);
  target.beginFill(c);
  target.moveTo(x1, y1);
  target.lineTo(x2, y1);
  target.lineTo(x2, y2);
  target.lineTo(x1, y2);
  target.lineTo(x1, y1);
  target.endFill();
  target.lineStyle(0, 0, 100);
 }
 // End of the function
 function drawLetter(target, x, y, let) {
  if (let == "x") {
   root.moveTo(x, y);
   root.lineTo(x + 20, y + 20);
   root.moveTo(x + 20, y);
   root.lineTo(x, y + 20);
  } else if (let == "y") {
   root.moveTo(x, y);
   root.lineTo(x + 10, y + 10);
   root.moveTo(x + 20, y);
   root.lineTo(x, y + 20);
  } else if (let == "z") {
   root.moveTo(x, y);
   root.lineTo(x + 20, y);
   root.lineTo(x, y + 20);
   root.lineTo(x + 20, y + 20);
  }
  // end else if
 }
 // End of the function
 static function main() {
  var d = new Draw3D(_root);
 }
}