码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-09-08 13:50:57      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

 1 package
 2 {
 3     import flash.display.MovieClip;
 4     import flash.display.Sprite;
 5     import flash.events.Event;
 6     import flash.geom.Point;
 7     import flash.geom.Vector3D;
 8     
 9     /**
10      * @author Frost.Yen
11      * @E-mail 871979853@qq.com
12      * @create 2015-9-8 上午11:03:11
13      *
14      */
15     [SWF(width="800",height="600")]
16     public class LineTetrahedron extends Sprite
17     {
18         private var _faceArr:Array = [];
19         private var _r:Number = 50;//顶点在各轴上的投影值
20         private var _focus:Number = 200;//焦距
21         private var _lineContainer:Sprite;
22         private var _container:Sprite;
23         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}];
24         public function LineTetrahedron()
25         {
26             initViews();
27             initEventListeners();
28         }
29         private function initViews():void
30         {
31             _container = new Sprite();
32             _lineContainer = new Sprite();
33             _container.x = _lineContainer.x = 400;
34             _container.y = _lineContainer.y = 300;
35             this.addChild(_container);
36             this.addChild(_lineContainer);
37             createFace();
38             
39         }
40         private function initEventListeners():void
41         {
42             this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
43         }
44         private function onEnterFrame(e:Event):void
45         {
46             for (var i:int=0; i<_faceArr.length; i++)
47             {
48                 _faceArr[i].transform.matrix3D.appendRotation((mouseX-_lineContainer.x)/50,Vector3D.Y_AXIS);
49                 _faceArr[i].transform.matrix3D.appendRotation((mouseY-_lineContainer.y)/50,Vector3D.X_AXIS);
50                 _faceArr[i].Z = (_faceArr[i] as MovieClip).transform.getRelativeMatrix3D(_container).position.z;
51                 _faceArr[i].X = (_faceArr[i] as MovieClip).transform.getRelativeMatrix3D(_container).position.x*(_focus/(_focus+_faceArr[i].Z));
52                 _faceArr[i].Y = (_faceArr[i] as MovieClip).transform.getRelativeMatrix3D(_container).position.y*(_focus/(_focus+_faceArr[i].Z));
53                 //利用焦距实现透视
54             }
55             _lineContainer.graphics.clear();
56             _lineContainer.graphics.lineStyle(3,0x00ffff);
57             _lineContainer.graphics.moveTo(_faceArr[0].X,_faceArr[0].Y);
58             _lineContainer.graphics.lineTo(_faceArr[1].X,_faceArr[1].Y);
59             _lineContainer.graphics.lineTo(_faceArr[2].X,_faceArr[2].Y);
60             _lineContainer.graphics.lineTo(_faceArr[0].X,_faceArr[0].Y);
61             _lineContainer.graphics.lineTo(_faceArr[3].X,_faceArr[3].Y);
62             _lineContainer.graphics.lineTo(_faceArr[1].X,_faceArr[1].Y);
63             _lineContainer.graphics.moveTo(_faceArr[3].X,_faceArr[3].Y);
64             _lineContainer.graphics.lineTo(_faceArr[2].X,_faceArr[2].Y);
65         }
66         private function createFace():void
67         {
68             for(var i:int = 0;i<4;i++){
69                 var mc:MovieClip = new MovieClip();
70                 _container.addChild(mc);
71                 _faceArr.push(mc);
72                 mc.x = _vertexArr[i].x;
73                 mc.y = _vertexArr[i].y;
74                 mc.z = _vertexArr[i].z;
75             }
76         }
77     }
78 }

 

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

标签:

原文地址:http://www.cnblogs.com/frost-yen/p/4791368.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!