Unity/C# кодер
Регистрация: 03.10.2005
Адрес: Россия, Рязань
Сообщений: 7,568
Написано 3,006 полезных сообщений (для 5,323 пользователей)
|

program MY_3D;
const
Steps=5;
numpoints=8;
numlines=12;
var
points :array[1..numpoints,1..5] of integer;
lines :array[1..numlines, 1..2] of integer;
distance,i : integer;
FPS_LastCount,FPS_Count,MS,MSL : integer;
cx, cy : integer;
rx, ry, rz : real;
Srx,Crx,Sry,Cry,Srz,Crz : real;
ox,tx,ty,tz : real;
Step,Direction,Faza:Integer;
Function ISgn(Val:Integer):Integer;
Begin
if val=0 then ISgn:=1 else
ISgn:=Val/Abs(Val);
End;
Procedure UpdateCube;
var P1,P2:Integer;
Begin
if Step>Steps then
Begin
Direction:=-Direction;
if Direction>0 then Faza:=Faza+1;
If Faza>4 then Faza:=1;
Step:=0;
end
else
Step:=Step+1;
If Faza=1 then Begin P1:=1;P2:=7; End;
If Faza=2 then Begin P1:=2;P2:=8; End;
If Faza=3 then Begin P1:=3;P2:=5; End;
If Faza=4 then Begin P1:=4;P2:=6; End;
points[P1,1]:=points[P1,1]+Direction*ISgn(points[P1,1]);
points[P1,2]:=points[P1,2]+Direction*ISgn(points[P1,2]);
points[P1,3]:=points[P1,3]+Direction*ISgn(points[P1,3]);
points[P2,1]:=points[P2,1]+Direction*ISgn(points[P2,1]);
points[P2,2]:=points[P2,2]+Direction*ISgn(points[P2,2]);
points[P2,3]:=points[P2,3]+Direction*ISgn(points[P2,3]);
end;
Procedure Rotate3D;
begin
Srx:=Sin(rx); Crx:=Cos(rx);
Sry:=Sin(ry); Cry:=Cos(ry);
Srz:=Sin(rz); Crz:=Cos(rz);
for i:=1 to numpoints do
begin
//X rotation
ty := points[i,2] * Crx - points[i,3] * Srx;
tz := points[i,2] * Srx + points[i,3] * Crx;
//Y rotation
tx := points[i,1] * Cry - tz * Sry;
tz := points[i,1] * Sry + tz * Cry;
//Z rotation
ox := tx;
tx := tx * Crz - ty * Srz;
ty := ox * Srz + ty * Crz;
//Calculate new x and y location with perspective
points[i,4] := Trunc(512 * tx / (distance - tz))+cx; //x
points[i,5] := Trunc(512 * ty / (distance - tz))+cy; //y
end;
end;
begin
Direction:=1;
Faza:=1;
distance:=100;
cx:=GetWidth/2;
cy:=GetHeight/2;
points[1,1]:=5; points[1,2]:=-5; points[1,3]:=-5;
points[2,1]:=5; points[2,2]:=-5; points[2,3]:=5;
points[3,1]:=5; points[3,2]:=5; points[3,3]:=5;
points[4,1]:=5; points[4,2]:=5; points[4,3]:=-5;
points[5,1]:=-5; points[5,2]:=-5; points[5,3]:=-5;
points[6,1]:=-5; points[6,2]:=-5; points[6,3]:=5;
points[7,1]:=-5; points[7,2]:=5; points[7,3]:=5;
points[8,1]:=-5; points[8,2]:=5; points[8,3]:=-5;
lines[1,1]:=1; lines[1,2]:=2;
lines[2,1]:=2; lines[2,2]:=3;
lines[3,1]:=3; lines[3,2]:=4;
lines[4,1]:=4; lines[4,2]:=1;
lines[5,1]:=2; lines[5,2]:=6;
lines[6,1]:=3; lines[6,2]:=7;
lines[7,1]:=4; lines[7,2]:=8;
lines[8,1]:=1; lines[8,2]:=5;
lines[9,1]:=5; lines[9,2]:=6;
lines[10,1]:=6; lines[10,2]:=7;
lines[11,1]:=7; lines[11,2]:=8;
lines[12,1]:=8; lines[12,2]:=5;
while true do
begin
SetColor(255, 255, 255);
FillRect(0, 0, GetWidth, GetHeight);
rx:=rx+ToRadians(1);
ry:=2*rx;
rz:=3*rx;
UpdateCube;
Rotate3D;
SetColor(255, 0, 0);
//Draw3D;
for i:=1 to numlines do
begin
DrawLine(points[lines[i,1],4], points[lines[i,1],5], points[lines[i,2],4], points[lines[i,2],5]);
end;
MS:=GetSecond(GetCurrentTime);
If MS<>MSL Then
begin
FPS_LastCount:=FPS_Count;
FPS_Count:=0;
MSL:=MS;
end
Else
FPS_Count:=FPS_Count+1;
SetColor(0, 0, 255);
DrawText('FPS:'+IntegerToString(FPS_LastCount), 0, 0);
Repaint;
if GetKeyPressed = KE_KEY1 then distance:=distance-10;
if GetKeyPressed = KE_KEY3 then distance:=distance+10;
if GetKeyPressed = KE_KEY2 then cy:=cy-2;
if GetKeyPressed = KE_KEY4 then cx:=cx-2;
if GetKeyPressed = KE_KEY6 then cx:=cx+2;
if GetKeyPressed = KE_KEY8 then cy:=cy+2;
Delay(30);
end;
end.
|