Meget nemt... Her er lidt begynderprogrammering, så du bedre forstår hvordan man bruger Cosinus og Sinus:
unit Unit1;
interface
uses
Windows,
Messages,
SysUtils,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
ExtCtrls,
StdCtrls,
vektorer, Menus;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
About1: TMenuItem;
Exit1: TMenuItem;
Resize1: TMenuItem;
N1: TMenuItem;
N3: TMenuItem;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure About1Click(Sender: TObject);
procedure Resize(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
type
coords = array[0..23] of double;
Const
lys : vektor = (x:0;y:0;z:1);
per = 6;
center = 180;
xrot = 5;
yrot = 10;
zrot = 4;
lock : coords =
(-1,-1,-1, -1,-1, 1, -1, 1,-1, -1, 1, 1,
1,-1,-1, 1,-1, 1, 1, 1,-1, 1, 1, 1);
index : array[0..23] of byte =
(0,1,3,2,
0,4,5,1,
0,2,6,4,
1,5,7,3,
2,3,7,6,
5,4,6,7);
var
buffer : Tbitmap;
clean : Tbitmap;
v : double;
cube : coords;
size : byte;
procedure rotate;
var x,y,z : double;
i : byte;
begin
if v>4*pi then v:=0 else v:=v+0.005;
cube:=lock;
for i:=0 to 7 do
begin
y:=cos(xrot*v)*cube[i*3+1]-sin(xrot*v)*cube[i*3+2];
z:=sin(xrot*v)*cube[i*3+1]+cos(xrot*v)*cube[i*3+2];
cube[i*3+1]:=y;
cube[i*3+2]:=z;
x:=cos(yrot*v)*cube[i*3]-sin(yrot*v)*cube[i*3+2];
z:=sin(yrot*v)*cube[i*3]+cos(yrot*v)*cube[i*3+2];
cube[i*3]:=x;
cube[i*3+2]:=z;
x:=cos(zrot*v)*cube[i*3]-sin(zrot*v)*cube[i*3+1];
y:=sin(zrot*v)*cube[i*3]+cos(zrot*v)*cube[i*3+1];
cube[i*3]:=x;
cube[i*3+1]:=y;
end;
end;
procedure line(x1,y1,x2,y2:word);
begin
with buffer.canvas do
begin
Brush.color:=clBlack;
MoveTo(x1,y1);
LineTo(x2,y2);
end;
end;
procedure pol(x1,y1,x2,y2,x3,y3,x4,y4,color:word);
begin
buffer.canvas.brush.color:=color;
buffer.canvas.Polygon([point(x1,y1),point(x2,y2),point(x3,y3),point(x4,y4)]);
end;
procedure draw;
var i, col : byte;
x1,y1,x2,y2,x3,y3,x4,y4 : word;
v1,v2, n : vektor;
begin
buffer.canvas.draw(0,0,clean);
for i:=0 to 5 do
begin
v1.x:=cube[index[i*4+2]*3+0]-cube[index[i*4+0]*3+0];
v1.y:=cube[index[i*4+2]*3+1]-cube[index[i*4+0]*3+1];
v1.z:=cube[index[i*4+2]*3+2]-cube[index[i*4+0]*3+2];
v2.x:=cube[index[i*4+1]*3+0]-cube[index[i*4+0]*3+0];
v2.y:=cube[index[i*4+1]*3+1]-cube[index[i*4+0]*3+1];
v2.z:=cube[index[i*4+1]*3+2]-cube[index[i*4+0]*3+2];
kryds(v1,v2,n);
if cos_vinkel(n,lys)>0.15 then
begin
x1:=round(size*((cube[index[i*4+0]*3+0]*per)/(cube[index[i*4+0]*3+2]+per)))+center;
y1:=round(size*((cube[index[i*4+0]*3+1]*per)/(cube[index[i*4+0]*3+2]+per)))+center-10;
x2:=round(size*((cube[index[i*4+1]*3+0]*per)/(cube[index[i*4+1]*3+2]+per)))+center;
y2:=round(size*((cube[index[i*4+1]*3+1]*per)/(cube[index[i*4+1]*3+2]+per)))+center-10;
x3:=round(size*((cube[index[i*4+2]*3+0]*per)/(cube[index[i*4+2]*3+2]+per)))+center;
y3:=round(size*((cube[index[i*4+2]*3+1]*per)/(cube[index[i*4+2]*3+2]+per)))+center-10;
x4:=round(size*((cube[index[i*4+3]*3+0]*per)/(cube[index[i*4+3]*3+2]+per)))+center;
y4:=round(size*((cube[index[i*4+3]*3+1]*per)/(cube[index[i*4+3]*3+2]+per)))+center-10;
col:=round(cos_vinkel(n,lys)*255);
pol(x1,y1,x2,y2,x3,y3,x4,y4,col);
end;
end;
form1.canvas.draw(0,0,buffer);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Buffer:=Tbitmap.Create;
Buffer.width:=380;
buffer.height:=390;
clean:=Tbitmap.Create;
clean.width:=380;
clean.height:=390;
form1.width:=380;
form1.height:=390;
clean.canvas.Brush.color:=clBlack;
clean.canvas.polygon([point(0,0),point(0,389),point(379,389),point(379,0)]);
size:=50;
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.About1Click(Sender: TObject);
begin
ShowMessage(\'af Valdemar Rørbech\'+#13+
\'valdemar@roerbech.dk\'+#13+
\'web:
http://prog.adr.dk/\');
end;
procedure TForm1.Resize(Sender: TObject);
var tmp : integer;
begin
tmp:=strtoint(inputbox(\'3D-terning\',\'Angiv ny størrelse (max. 80)\',inttostr(size)));
if tmp<81 then
if tmp>-1 then
size:=tmp
else showmessage(\'Ost!\');
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Rotate;
Draw;
end;
end.
Programmet laver ikke andet end at rotere et object i 3D, men derfor kan du vel godt bruge til din lommeregner, ik´?
CyberLSN