分析:
将八面体置于3D坐标系中,其中心的坐标位于原点(0,0,0),让八面体的六个顶点恰好位于3D坐标系的x轴、y轴和z轴上,则从八面体的中心到这六个顶点的距离是相等的。我们可以假设这个距离为r,则六个顶点的坐标分别为上(0,-r,0)、下(0,r,0)、四周:左(-r,0,0)、右(r,0,0)、前(0,0,-r)、后(0,0,r)。
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 [email protected] 12 * @create 2015-9-7 下午5:10:05 13 * 14 */ 15 [SWF(width="800",height="600")] 16 public class LineOctahedron extends Sprite 17 { 18 private var _faceArr:Array = []; 19 private var _r:Number = 100;//中心到顶点的距离 20 private var _focus:Number = 200;//焦距 21 private var _lineContainer:Sprite; 22 private var _container:Sprite; 23 private var _vertexArr:Array = [{x:0,y:_r,z:0},{x:-_r,y:0,z:0},{x:0,y:0,z:_r},{x:_r,y:0,z:0},{x:0,y:0,z:-_r},{x:0,y:-_r,z:0}]; 24 public function LineOctahedron() 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 for(i = 1;i<=4;i++){ 58 //从上面连到四周; 59 _lineContainer.graphics.moveTo(_faceArr[0].X,_faceArr[0].Y); 60 _lineContainer.graphics.lineTo(_faceArr[i].X,_faceArr[i].Y); 61 //从下面连到四周; 62 _lineContainer.graphics.moveTo(_faceArr[5].X,_faceArr[5].Y); 63 _lineContainer.graphics.lineTo(_faceArr[i].X,_faceArr[i].Y); 64 //四周顶点互连; 65 _lineContainer.graphics.moveTo(_faceArr[i].X,_faceArr[i].Y); 66 _lineContainer.graphics.lineTo(_faceArr[i!=4?i+1:1].X,_faceArr[i!=4?i+1:1].Y); 67 } 68 } 69 private function createFace():void 70 { 71 for(var i:int = 0;i<6;i++){ 72 var mc:MovieClip = new MovieClip(); 73 _container.addChild(mc); 74 _faceArr.push(mc); 75 mc.x = _vertexArr[i].x; 76 mc.y = _vertexArr[i].y; 77 mc.z = _vertexArr[i].z; 78 } 79 } 80 } 81 }
时间: 2024-12-18 09:26:11