分析:
正二十面体共有12个顶点、30条棱,其20个面都是正三角形。每条棱所对应的弧度值为1.1071487177940904弧度,这个弧度值可通过求Math.sqrt(5)/5的反余弦值求得。正二十面体的12个点按相对应的位置可以分为6对,把其中一对顶点置于Y轴上,此两点到坐标原点距离相等,则另外五对顶点皆呈倾斜状态,它们穿过坐标原点并环绕Y轴以72度递增旋转。根据这一点,可以先算好第一对倾斜顶点的坐标,然后让它绕Y轴以72度递增旋转4次得到另外四对顶点坐标,至此正二十面体的12个顶点坐标都已清楚,便可以创建模型了。
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Vector3D;
import flash.net.LocalConnection; /**
* @author Frost.Yen
* @E-mail 871979853@qq.com
* @create 2015-9-7 下午12:58:27
*
*/
[SWF(width="800",height="600")]
public class LineIcosahedron extends Sprite
{
private var _r:Number = 100;
private var _faceArr:Array = [];
private var _focus:Number = 400;
private var _container:Sprite;
private var _lineContainer:Sprite;
public function LineIcosahedron()
{
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
{
_lineContainer.graphics.clear(); _lineContainer.graphics.lineStyle(3,0x00ffff);
for(var i:int = 0;i<_faceArr.length;i++){
(_faceArr[i] as MovieClip).transform.matrix3D.appendRotation(-(mouseX-_container.x)/50,Vector3D.Y_AXIS);
(_faceArr[i] as MovieClip).transform.matrix3D.appendRotation((mouseY-_container.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));
} drawLine();
}
private function createFace():void
{
var angle:Number = 0.5*Math.PI - Math.acos(Math.sqrt(5)/5);
for(var i:int = 0;i<12;i++){
var mc:MovieClip = new MovieClip();
_container.addChild(mc);
_faceArr.push(mc);
if(i==0){
mc.x = 0;
mc.y = -_r;
mc.z = 0;
}else if(i==11){
mc.x = 0;
mc.y = _r;
mc.z = 0;
}else if(i<=5){
mc.x = _r*Math.cos(angle);
mc.y = -_r*Math.sin(angle);
mc.z = 0;
}else{
mc.x = -_r*Math.cos(angle);
mc.y = _r*Math.sin(angle);
mc.z = 0;
}
}
for(i = 1;i<11;i++){
(_faceArr[i] as MovieClip).transform.matrix3D.appendRotation(i*72,Vector3D.Y_AXIS);
}
}
private function drawLine():void
{
for(var i:int = 1;i<6;i++ ){
_lineContainer.graphics.moveTo(_faceArr[0].X,_faceArr[0].Y);
_lineContainer.graphics.lineTo(_faceArr[i].X,_faceArr[i].Y);
}
for(i=1;i<6;i++){
_lineContainer.graphics.lineTo(_faceArr[i].X,_faceArr[i].Y);
}
for(i = 6;i<11;i++){
_lineContainer.graphics.moveTo(_faceArr[11].X,_faceArr[11].Y);
_lineContainer.graphics.lineTo(_faceArr[i].X,_faceArr[i].Y);
}
for(i=6;i<11;i++){
_lineContainer.graphics.lineTo(_faceArr[i].X,_faceArr[i].Y);
} _lineContainer.graphics.moveTo(_faceArr[1].X,_faceArr[1].Y);
var id:int = 9;
for(i=0;i<9;i++){
_lineContainer.graphics.lineTo(_faceArr[id].X,_faceArr[id].Y);
if(i<3){
id = i+11-id;
}else{
id = i+6-id;
}
}
_lineContainer.graphics.lineTo(_faceArr[1].X,_faceArr[1].Y);
}
}
}
下面是源代码中drawLine()函数的原型
private function drawLine2():void
{
_lineContainer.graphics.moveTo(_faceArr[0].X,_faceArr[0].Y);
_lineContainer.graphics.lineTo(_faceArr[1].X,_faceArr[1].Y); _lineContainer.graphics.moveTo(_faceArr[0].X,_faceArr[0].Y);
_lineContainer.graphics.lineTo(_faceArr[2].X,_faceArr[2].Y); _lineContainer.graphics.moveTo(_faceArr[0].X,_faceArr[0].Y);
_lineContainer.graphics.lineTo(_faceArr[3].X,_faceArr[3].Y); _lineContainer.graphics.moveTo(_faceArr[0].X,_faceArr[0].Y);
_lineContainer.graphics.lineTo(_faceArr[4].X,_faceArr[4].Y); _lineContainer.graphics.moveTo(_faceArr[0].X,_faceArr[0].Y);
_lineContainer.graphics.lineTo(_faceArr[5].X,_faceArr[5].Y); _lineContainer.graphics.lineTo(_faceArr[1].X,_faceArr[1].Y);
_lineContainer.graphics.lineTo(_faceArr[2].X,_faceArr[2].Y);
_lineContainer.graphics.lineTo(_faceArr[3].X,_faceArr[3].Y);
_lineContainer.graphics.lineTo(_faceArr[4].X,_faceArr[4].Y);
_lineContainer.graphics.lineTo(_faceArr[5].X,_faceArr[5].Y); _lineContainer.graphics.moveTo(_faceArr[11].X,_faceArr[11].Y);
_lineContainer.graphics.lineTo(_faceArr[6].X,_faceArr[6].Y); _lineContainer.graphics.moveTo(_faceArr[11].X,_faceArr[11].Y);
_lineContainer.graphics.lineTo(_faceArr[7].X,_faceArr[7].Y); _lineContainer.graphics.moveTo(_faceArr[11].X,_faceArr[11].Y);
_lineContainer.graphics.lineTo(_faceArr[8].X,_faceArr[8].Y); _lineContainer.graphics.moveTo(_faceArr[11].X,_faceArr[11].Y);
_lineContainer.graphics.lineTo(_faceArr[9].X,_faceArr[9].Y); _lineContainer.graphics.moveTo(_faceArr[11].X,_faceArr[11].Y);
_lineContainer.graphics.lineTo(_faceArr[10].X,_faceArr[10].Y); _lineContainer.graphics.lineTo(_faceArr[6].X,_faceArr[6].Y);
_lineContainer.graphics.lineTo(_faceArr[7].X,_faceArr[7].Y);
_lineContainer.graphics.lineTo(_faceArr[8].X,_faceArr[8].Y);
_lineContainer.graphics.lineTo(_faceArr[9].X,_faceArr[9].Y);
_lineContainer.graphics.lineTo(_faceArr[10].X,_faceArr[10].Y); _lineContainer.graphics.moveTo(_faceArr[1].X,_faceArr[1].Y);
_lineContainer.graphics.lineTo(_faceArr[9].X,_faceArr[9].Y);
_lineContainer.graphics.lineTo(_faceArr[2].X,_faceArr[2].Y);
_lineContainer.graphics.lineTo(_faceArr[10].X,_faceArr[10].Y);
_lineContainer.graphics.lineTo(_faceArr[3].X,_faceArr[3].Y);
_lineContainer.graphics.lineTo(_faceArr[6].X,_faceArr[6].Y);
_lineContainer.graphics.lineTo(_faceArr[4].X,_faceArr[4].Y);
_lineContainer.graphics.lineTo(_faceArr[7].X,_faceArr[7].Y);
_lineContainer.graphics.lineTo(_faceArr[5].X,_faceArr[5].Y);
_lineContainer.graphics.lineTo(_faceArr[8].X,_faceArr[8].Y);
_lineContainer.graphics.lineTo(_faceArr[1].X,_faceArr[1].Y);
}