空間にラインを引く。
package
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.Event;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.utils.Timer;
public class main2 extends MovieClip
{
public var screenZ:Number = 100;
protected var _camera:Object;
protected var _objectArray:Array = new Array();
public var shapes:Array = new Array();
protected var timer:Timer;
public function main2()
{
this.init();
}
protected function init():void
{
var tex:TextField = new TextField();
tex.text = "drag and draw Line";
this.addChild(tex);
this.stage.align = StageAlign.TOP_LEFT;
this.stage.scaleMode = StageScaleMode.NO_SCALE;
this.timer = new Timer(50);
this.timer.addEventListener(TimerEvent.TIMER,this.addPoint);
var obj:Object = new Object();
obj.x = 100;
obj.y = 100;
obj.z = 100;
this.setObject(obj);
obj = new Object();
obj.x = -100;
obj.y = -100;
obj.z = -100;
this.setObject(obj);
obj = new Object();
obj.x = -400;
obj.y = -400;
obj.z = -400;
this.setObject(obj);
this._camera = new Object();
this._camera.screenZ = this.screenZ;
this._camera.x = 0;
this._camera.y = 0;
this._camera.z = -2000;
this.stage.addEventListener(MouseEvent.MOUSE_DOWN,this.mouseDownEventHandler);
this.addEventListener(Event.ENTER_FRAME,this.render);
}
protected function render(e:Event):void
{
this.rotAxisY(Math.PI*2/100);
var ary:Array = this.screenView();
this.graphics.clear();
this.graphics.lineStyle(5,0x000000,1);
this.graphics.moveTo(ary[0].x + this.stage.stageWidth/2,ary[0].y + this.stage.stageHeight/2);
for(var i:uint=1;i<ary.length;i++)
{
trace(ary[i].x);
if(ary[i].z > this._camera.z)
{
this.graphics.lineTo(ary[i].x + this.stage.stageWidth/2,ary[i].y + this.stage.stageHeight/2);
}
}
}
protected function mouseDownEventHandler(e:MouseEvent):void
{
this.stage.addEventListener(MouseEvent.MOUSE_UP,this.mouseUpEventHandler);
this.timer.start();
}
protected function mouseUpEventHandler(e:MouseEvent):void
{
this.stage.addEventListener(MouseEvent.MOUSE_DOWN,this.mouseDownEventHandler);
this.timer.stop();
}
protected function addPoint(e:TimerEvent):void
{
var dx:Number = this.stage.mouseX -this.stage.stageWidth/2;
var dy:Number = this.stage.mouseY - this.stage.stageHeight/2;
var obj:Object = new Object();
obj.x = dx * ((-this._camera.z)/this._camera.screenZ);
obj.y = dy * ((-this._camera.z)/this._camera.screenZ);
obj.z = 100;
this..setObject(obj);
}
public function setObject(obj:Object,nam:String = ""):*
{
if(nam != "")
{
this._objectArray[nam] = obj;
return nam;
}
else
{
var num:uint = this._objectArray.push(obj);
return num-1;
}
return 0;
}
//スクリーン座標に変換
public function screenView():Array
{
var viewAry:Array = new Array();
for(var prop:Object in this._objectArray)
{
var obj:Object = new Object();
obj.x = (this._objectArray[prop].x - this._camera.x)*(this._camera.screenZ/(this._objectArray[prop].z - this._camera.z));
obj.y = (this._objectArray[prop].y - this._camera.y)*(this._camera.screenZ/(this._objectArray[prop].z - this._camera.z));
obj.z = this._camera.screenZ/(this._objectArray[prop].z - this._camera.z);
obj.object = this._objectArray[prop];
viewAry[prop] = obj;
}
return viewAry;
}
// X軸回転
// @param theta = ラジアン
public function rotAxisX(theta:Number = 0):void
{
var _dx:Number;
var _dy:Number;
var _dz:Number;
var rot:Number = theta;
for(var prop:Object in this._objectArray)
{
_dy = this._objectArray[prop].y*Math.cos(rot) + this._objectArray[prop].z*Math.sin(rot);
_dz = -this._objectArray[prop].y*Math.sin(rot) + this._objectArray[prop].z*Math.cos(rot);
this._objectArray[prop].y = _dy;
this._objectArray[prop].z = _dz;
}
}
// Y軸回転
// @param theta = ラジアン
public function rotAxisY(theta:Number = 0):void
{
var _dx:Number;
var _dy:Number;
var _dz:Number;
var rot:Number = theta;
for(var prop:Object in this._objectArray)
{
_dx = this._objectArray[prop].x*Math.cos(rot) - this._objectArray[prop].z*Math.sin(rot);
_dz = this._objectArray[prop].x*Math.sin(rot) + this._objectArray[prop].z*Math.cos(rot);
this._objectArray[prop].x = _dx;
this._objectArray[prop].z = _dz;
}
}
// Z軸回転
//@param theta = ラジアン
public function rotAxisZ(theta:Number = 0):void
{
var _dx:Number;
var _dy:Number;
var _dz:Number;
var rot:Number = theta;
for(var prop:Object in this._objectArray)
{
_dx = this._objectArray[prop].x*Math.cos(rot) + this._objectArray[prop].y*Math.sin(rot);
_dy = -this._objectArray[prop].x*Math.sin(rot) + this._objectArray[prop].y*Math.cos(rot);
this._objectArray[prop].x = _dx;
this._objectArray[prop].y = _dy;
}
}
}
}