package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Vector3D; /**
* @author Frost.Yen
* @E-mail 871979853@qq.com
* @create 2015-9-1 下午2:32:43
*
*/
[SWF(width="800",height="600")]
public class Dodecahedron extends Sprite
{
private var _faceArr:Array = [];//存放每个面的数组
private var _radius:Number = 60;//五边形半径
private var _dis:Number;//中心点到五边形顶点的距离,可根据五边形半径计算
private var _angle:Number=Math.acos(-1/Math.sqrt(5))*180/Math.PI;//12面体相邻面之间的夹角
private var _container:Sprite;
public function Dodecahedron()
{
initViews();
initEventListeners();
}
private function initViews():void
{
_container = new Sprite();
_container.x = stage.stageWidth*0.5;
_container.y = stage.stageHeight*0.5; this.addChild(_container);
creatDodecahedron();
}
private function initEventListeners():void
{
this.addEventListener(Event.ENTER_FRAME,onMove);
}
private function onMove(e:Event):void
{
for(var i:int = 0;i<_faceArr.length;i++){
_faceArr[i].transform.matrix3D.appendRotation(-(mouseX-stage.stageWidth*0.5)/10,Vector3D.Y_AXIS);
_faceArr[i].transform.matrix3D.appendRotation((mouseY-stage.stageHeight*0.5)/10,Vector3D.X_AXIS);
}
_faceArr.sortOn("z",18);//如果为 options 参数指定值 8 或 Array.RETURNINDEXEDARRAY,则返回反映排序结果的数组并且不修改数组。
for(i=0;i<_faceArr.length;i++){
_container.setChildIndex(_faceArr[i],i);
} }
private function creatDodecahedron():void
{
var b:Number = Math.sqrt(_radius*_radius*2-2*_radius*_radius*Math.cos(360/6 * Math.PI/180));//五边形的边长
var h1:Number = Math.sqrt(_radius*_radius-b*b/4);//五边形中心点到任意边的高
//var h:Number = h1 * Math.tan((180-116.56505)*0.5*Math.PI / 180);//12面体中心点到五边形中心点的距离
_dis = h1 / Math.sin((180-116.56505)*0.5*Math.PI / 180);//12面体中心点到五边形顶点的距离 for(var i:int = 0;i<12;i++){
var pentagon:Sprite = drawPentagon(_radius,Math.random()*0xFFFFFF);
pentagon.z = -Math.sqrt(_dis*_dis-_radius*_radius);
_container.addChild(pentagon);
_faceArr.push(pentagon);
}
_faceArr[6].transform.matrix3D.appendRotation(180,Vector3D.Y_AXIS);
for(i=1;i<=5;i++){
//12面体相邻面的夹角arccos(-1/√5)*180/π=116.56505
//trace(Math.acos(-1/Math.sqrt(5))*180/Math.PI);
_faceArr[i].transform.matrix3D.appendRotation(_angle,Vector3D.Y_AXIS);
//绕z轴分布;
_faceArr[i].transform.matrix3D.appendRotation(36-(i-1)*72,Vector3D.Z_AXIS);
_faceArr[i + 6].transform.matrix3D.appendRotation(180+_angle,Vector3D.Y_AXIS);
_faceArr[i + 6].transform.matrix3D.appendRotation(36-(i-1)*72,Vector3D.Z_AXIS);
}
}
/**
* 绘制五边形
* @param r
* @param color
* @return
*/
private function drawPentagon(r:Number = 60,color:uint = 0xff00ff):Sprite
{
var pointArr:Array = [];
for(var i:int = 0;i<5;i++){
pointArr.push(new Point(r*Math.cos(i*Math.PI*2/5),r*Math.sin(i*Math.PI*2/5)));
}
var pentagon:Sprite = new Sprite();
pentagon.graphics.lineStyle(0,0);
pentagon.graphics.beginFill(color,0.9);
pentagon.graphics.moveTo(pointArr[0].x,pointArr[0].y);
for(i = 1;i<=5;i++){
pentagon.graphics.lineTo(pointArr[i%5].x,pointArr[i%5].y);
}
pentagon.graphics.endFill();
return pentagon;
}
}
}