[ActionScript 3.0] AS3 绘制正四面体(线条)

 package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Vector3D; /**
* @author Frost.Yen
* @E-mail 871979853@qq.com
* @create 2015-9-8 上午11:03:11
*
*/
[SWF(width="800",height="600")]
public class LineTetrahedron extends Sprite
{
private var _faceArr:Array = [];
private var _r:Number = 50;//顶点在各轴上的投影值
private var _focus:Number = 200;//焦距
private var _lineContainer:Sprite;
private var _container:Sprite;
private var _vertexArr:Array = [{x:-_r,y:_r,z:-_r},{x:_r,y:_r,z:_r},{x:-_r,y:-_r,z:_r},{x:_r,y:-_r,z:-_r}];
public function LineTetrahedron()
{
initViews();
initEventListeners();
}
private function initViews():void
{
_container = new Sprite();
_lineContainer = new Sprite();
_container.x = _lineContainer.x = 400;
_container.y = _lineContainer.y = 300;
this.addChild(_container);
this.addChild(_lineContainer);
createFace(); }
private function initEventListeners():void
{
this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
private function onEnterFrame(e:Event):void
{
for (var i:int=0; i<_faceArr.length; i++)
{
_faceArr[i].transform.matrix3D.appendRotation((mouseX-_lineContainer.x)/50,Vector3D.Y_AXIS);
_faceArr[i].transform.matrix3D.appendRotation((mouseY-_lineContainer.y)/50,Vector3D.X_AXIS);
_faceArr[i].Z = (_faceArr[i] as MovieClip).transform.getRelativeMatrix3D(_container).position.z;
_faceArr[i].X = (_faceArr[i] as MovieClip).transform.getRelativeMatrix3D(_container).position.x*(_focus/(_focus+_faceArr[i].Z));
_faceArr[i].Y = (_faceArr[i] as MovieClip).transform.getRelativeMatrix3D(_container).position.y*(_focus/(_focus+_faceArr[i].Z));
//利用焦距实现透视
}
_lineContainer.graphics.clear();
_lineContainer.graphics.lineStyle(3,0x00ffff);
_lineContainer.graphics.moveTo(_faceArr[0].X,_faceArr[0].Y);
_lineContainer.graphics.lineTo(_faceArr[1].X,_faceArr[1].Y);
_lineContainer.graphics.lineTo(_faceArr[2].X,_faceArr[2].Y);
_lineContainer.graphics.lineTo(_faceArr[0].X,_faceArr[0].Y);
_lineContainer.graphics.lineTo(_faceArr[3].X,_faceArr[3].Y);
_lineContainer.graphics.lineTo(_faceArr[1].X,_faceArr[1].Y);
_lineContainer.graphics.moveTo(_faceArr[3].X,_faceArr[3].Y);
_lineContainer.graphics.lineTo(_faceArr[2].X,_faceArr[2].Y);
}
private function createFace():void
{
for(var i:int = 0;i<4;i++){
var mc:MovieClip = new MovieClip();
_container.addChild(mc);
_faceArr.push(mc);
mc.x = _vertexArr[i].x;
mc.y = _vertexArr[i].y;
mc.z = _vertexArr[i].z;
}
}
}
}
上一篇:【BZOJ1006】【HNOI2008】神奇的国度(弦图染色)


下一篇:mysqldump参数详细说明(转)