1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
program Pcube3D;
{$FRAME_WIDTH 550}
{$FRAME_HEIGHT 400}
{$FRAME_RATE 12}
{$BACKGROUND $FFFFFF}
uses
Flash8;
Type
TVector3D = class
x,y,z:double;
Procedure rotateXY(ca, sa, cb, sb:double);
Constructor Create(xv,yv,zv:double);
end;
TFace=class(Movieclip)
p: array[1..8] of TVector3D;
mid1234,mid3456,mid5678,mid7812,mid1357,mid2468: TVector3D;
constructor Create (numero:integer;parent:Movieclip);
end;
TAnim3D = Class(Movieclip)
face:array[1..6] of TFace;
procedure Refresh;
procedure onEnterFrame;override;
constructor Create (parent:Movieclip);
end;
const cube:array[1..8,1..3] of double = ((-100,-100,-100),
(-100,-100, 100),
(-100, 100,-100),
(-100, 100, 100),
( 100, 100,-100),
( 100, 100, 100),
( 100,-100,-100),
( 100,-100, 100));
Constructor TFace.Create(numero:integer;parent:Movieclip);
var i:integer;
begin
inherited Create(Parent, 'face' + IntToStr(numero),numero);
for i:=1 to 8 do p[i]:=TVector3D.Create(cube[i,1],cube[i,2],cube[i,3]);
mid1234 := TVector3D.Create(-100, 0, 0);
mid3456 := TVector3D.Create( 0, 100, 0);
mid5678 := TVector3D.Create( 100, 0, 0);
mid7812 := TVector3D.Create( 0,-100, 0);
mid1357 := TVector3D.Create( 0, 0,-100);
mid2468 := TVector3D.Create( 0, 0, 100);
end;
Procedure TVector3D.rotateXY(ca, sa, cb, sb:double);
var rz:double;
Begin
rz:= y*sa+z*ca;
y := y*ca-z*sa;
z := x*(-sb)+rz*cb;
x := x*cb+rz*sb;
end;
//Procedure de rafraichissement
Procedure TAnim3D.refresh;
Var cosa,cosb,sina,sinb:double;
i:integer;
begin
cosa := cos((_ymouse/20)*Math.PI/180);
sina := sin((_ymouse/20)*Math.PI/180);
cosb := cos((-_xmouse/20)*Math.PI/180);
sinb := sin((-_xmouse/20)*Math.PI/180);
for i:=1 to 6 do with Face[i] do
begin
P[1].rotateXY(cosa, sina, cosb, sinb);
P[2].rotateXY(cosa, sina, cosb, sinb);
P[3].rotateXY(cosa, sina, cosb, sinb);
P[4].rotateXY(cosa, sina, cosb, sinb);
P[5].rotateXY(cosa, sina, cosb, sinb);
P[6].rotateXY(cosa, sina, cosb, sinb);
P[7].rotateXY(cosa, sina, cosb, sinb);
P[8].rotateXY(cosa, sina, cosb, sinb);
mid1234.rotateXY(cosa, sina, cosb, sinb);
mid3456.rotateXY(cosa, sina, cosb, sinb);
mid5678.rotateXY(cosa, sina, cosb, sinb);
mid7812.rotateXY(cosa, sina, cosb, sinb);
mid1357.rotateXY(cosa, sina, cosb, sinb);
mid2468.rotateXY(cosa, sina, cosb, sinb);
end;
with face[1] do
begin
clear();
lineStyle(4,0);
beginFill($00ff00);
moveTo(P[1].x, P[1].y);
lineTo(P[2].x, P[2].y);
lineTo(P[4].x, P[4].y);
lineTo(P[3].x, P[3].y);
lineTo(P[1].x, P[1].y);
endFill();
end;
with face[2] do
begin
clear();
lineStyle(4,0);
beginFill($0000ff);
moveTo(P[3].x, P[3].y);
lineTo(P[4].x, P[4].y);
lineTo(P[6].x, P[6].y);
lineTo(P[5].x, P[5].y);
lineTo(P[3].x, P[3].y);
endFill();
end;
with face[3] do
begin
clear();
lineStyle(4,0);
beginFill($ff0000);
moveTo(P[5].x, P[5].y);
lineTo(P[6].x, P[6].y);
lineTo(P[8].x, P[8].y);
lineTo(P[7].x, P[7].y);
lineTo(P[5].x, P[5].y);
endFill();
end;
with face[4] do
begin
clear();
lineStyle(4,0);
beginFill($ff00ff);
moveTo(P[7].x, P[7].y);
lineTo(P[8].x, P[8].y);
lineTo(P[2].x, P[2].y);
lineTo(P[1].x, P[1].y);
lineTo(P[7].x, P[7].y);
endFill();
end;
with face[5] do
begin
clear();
lineStyle(4,0);
beginFill($ffff00);
moveTo(P[1].x, P[1].y);
lineTo(P[3].x, P[3].y);
lineTo(P[5].x, P[5].y);
lineTo(P[7].x, P[7].y);
lineTo(P[1].x, P[1].y);
endFill();
end;
with face[6] do
begin
clear();
lineStyle(4,0);
beginFill($00ffff);
moveTo(P[2].x, P[2].y);
lineTo(P[4].x, P[4].y);
lineTo(P[6].x, P[6].y);
lineTo(P[8].x, P[8].y);
lineTo(P[2].x, P[2].y);
endFill();
end;
with face[1] do swapDepths(100000-mid1234.z*100);
with face[2] do swapDepths(100000-mid3456.z*100);
with face[3] do swapDepths(100000-mid5678.z*100);
with face[4] do swapDepths(100000-mid7812.z*100);
with face[5] do swapDepths(100000-mid1357.z*100);
with face[6] do swapDepths(100000-mid2468.z*100);
end;
Procedure TAnim3D.onEnterFrame;
begin
refresh;
end;
Constructor TVector3D.Create(xv,yv,zv:double);
begin
x:=xv;
y:=yv;
z:=zv;
end;
Constructor TAnim3D.Create(parent:Movieclip);
var i:integer;
Begin
inherited Create(Parent,'anim3D',0);
For i:=1 to 6 do face[i]:=TFace.Create(i,self);
_x:=300;
_y:=200;
end;
begin
TAnim3D.Create(_Root);
end. |
Partager