package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.*;
public class FlashTest extends Sprite {
public var Panels:Array;
public var Rotation:Array;
public var t:TextField;
public var ox:Number;
public var oy:Number;
public var vx:Number;
public var vy:Number;
public var drag:Boolean;
public function FlashTest() {
t = new TextField();
addChild(t);
t.x = -stage.stageWidth/2;
t.y = -stage.stageHeight/2;
t.height = 400;
Panels = new Array(
[[-20,0,0],[0,-20,0],[0,0,20],[0x0000ff,1]],
[[0,0,20],[0,-20,0],[20,0,0],[0x0000ff,1]],
[[20,0,0],[0,-20,0],[0,0,-20],[0x0000ff,1]],
[[0,0,-20],[0,-20,0],[-20,0,0],[0x0000ff,1]],
[[-20,0,0],[0,0,20],[0,40,0],[0xff0000,1]],
[[0,0,20],[20,0,0],[0,40,0],[0xff0000,1]],
[[20,0,0],[0,0,-20],[0,40,0],[0xff0000,1]],
[[0,0,-20],[-20,0,0],[0,40,0],[0xff0000,1]]);
Rotation = new Array(0,0,0);
x = stage.stageWidth/2;
y = stage.stageHeight/2;
ox = 0;
oy = 0;
vx = 0;
vy = 0;
drag = false;
rotatePanels(Panels);
stage.addEventListener(Event.ENTER_FRAME,main);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mousedown);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseup);
stage.addEventListener(MouseEvent.MOUSE_MOVE,mousemove);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keydown);
}
public function main(e:Event):void{
}
public function mousedown(e:MouseEvent):void{
drag = true;
}
public function mouseup(e:MouseEvent):void{
drag = false;
}
public function mousemove(e:Event):void{
var X:Number = mouseX-ox;
var Y:Number = mouseY-oy;
ox = mouseX;
oy = mouseY;
if(drag == true){
graphics.clear();
t.text = "";
Rotation = new Array(-Y*Math.PI/64,X*Math.PI/64,0);
rotatePanels(Panels);
}
}
public function keydown(e:KeyboardEvent):void{
var c:Number = e.keyCode;
if(c == 37 || c == 38 || c == 39 || c == 40){
t.text = "";
graphics.clear();
if(e.keyCode == 37){
Rotation = new Array(0,Math.PI/32,0);
}else if(c == 39){
Rotation = new Array(0,-Math.PI/32,0);
}else if(c == 38){
Rotation = new Array(-Math.PI/32,0,0);
}else{
Rotation = new Array(Math.PI/32,0,0);
}
rotatePanels(Panels);
}
}
public function rotatePanels(P:Array):void{
for(var i:int=0;i<P.length;i++){
var A:Array = P[i];
for(var j:int=0;j<A.length-1;j++){
var X:Number = A[j][0];//0
var Y:Number = A[j][1];//-10
var Z:Number = A[j][2];//0
var d:Number = Math.sqrt(Math.pow(Y,2)+Math.pow(Z,2));//0
var a:Number = Math.atan(Y/Z)+Rotation[0];
if(Z < 0){
a += Math.PI;
}
if(d){
Z = Math.cos(a)*d;
Y = Math.sin(a)*d;
}
d = Math.sqrt(Math.pow(X,2)+Math.pow(Z,2));
a = Math.atan(Z/X)+Rotation[1];
if(X < 0){
a += Math.PI;
}
if(d){
X = Math.cos(a)*d;
Z = Math.sin(a)*d;
}
d = Math.sqrt(Math.pow(X,2)+Math.pow(Y,2));
a = Math.atan(Y/X)+Rotation[2];
if(X < 0){
a += Math.PI;
}
if(d){
X = Math.cos(a)*d;
Y = Math.sin(a)*d;
}
A[j][0] = X;
A[j][1] = Y;
A[j][2] = Z;
}
//if(((A[1][1]-A[0][1])/(A[1][0]-A[0][0]) - (A[2][1]-A[0][1])/(A[2][0]-A[0][0]) < 0) != (A[0][0] <= A[1][0] == A[0][0] > A[2][0])){
if((A[1][0]-A[0][0])*(A[2][1]-A[0][1])<(A[2][0]-A[0][0])*(A[1][1]-A[0][1])){
drawPanel(A);
}
}
}
public function drawPanel(A:Array):void{
graphics.lineStyle(1,0x000000,1);
var L:Number = A.length-1;
graphics.beginFill(A[L][0],1);
graphics.moveTo(A[L-1][0],A[L-1][1]);
for(var i:int=0;i<L;i++){
graphics.lineTo(A[i][0],A[i][1]);
}
graphics.endFill();
}
}
}