Смекни!
smekni.com

Программирование математических объектов (стр. 5 из 6)

OpenDialog: TOpenDialog;

SaveDialog: TSaveDialog;

N10: TMenuItem;

Space: TButton;

Box1: TComboBox;

Label1: TLabel;

OK: TButton;

Cancel: TButton;

Label6: TLabel;

BCube: TBitBtn;

BSide: TBitBtn;

Box6: TComboBox;

Label7: TLabel;

procedure FormCreate(Sender: TObject);

procedure TimerTimer(Sender: TObject; LagCount: Integer);

procedure VidMouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

procedure EnterClick(Sender: TObject);

procedure N3Click(Sender: TObject);

procedure N4Click(Sender: TObject);

procedure N2Click(Sender: TObject);

procedure N9Click(Sender: TObject);

procedure N10Click(Sender: TObject);

procedure SpaceClick(Sender: TObject);

procedure FormKeyDown(Sender: TObject; var Key: Word;

Shift: TShiftState);

procedure FormKeyUp(Sender: TObject; var Key: Word;

Shift: TShiftState);

procedure CancelClick(Sender: TObject);

procedure OKClick(Sender: TObject);

procedure N8Click(Sender: TObject);

procedure BCubeClick(Sender: TObject);

procedure BSideClick(Sender: TObject);

procedure N6Click(Sender: TObject);

private

public

end;

var Lab: TLab;

implementation

{$R *.DFM}

const ScreenX:Word=640;

ScreenY:Word=480;

x1:integer=0;

y1:integer=0;

White=$FFFFFF;

View:Boolean=False;

Figure:Word=1;

Accept:Boolean=True;

sv:Word=0;

var c:char;S,SS,TMP:PPl;t:PTexture;

CS,CO,CC,CSc,CL:Word;Rot:TRot;

x0,y0,x2,y2:integer;r:Single;

Blue,Red,Yelow,M,N,SC:Word;

Keys:array[1..255]of Boolean;

Input:array[1..12]of procedure (x,y:integer;var E:Boolean);

Bol:Boolean;Option:array[1..5] of String;

o:array[1..MaxSide]of TPoint;

w:array[1..2]of TPoint;tmp_o:TPoint;

cx,cy:Word;oc:TPoint;ClicCub:array[0..MaxSide]of Boolean;

procedure LoadObject(Name:String;var Obj:PObj);

var f1:file of TPoint; a:TSides;

f2:file of Word;

S,Tmp:TPoint;i,j:word;Name2:String;

B:Boolean;

begin

For i:=1 to Length (Name)-4 do begin

Name2:=Name2+Name[i];

end;

Name:=Name2;

Assign(f2,Name+'.res');

Reset(f2);

Read(f2,Obj^.Count);

close(f2);

Assign(f1,Name+'.dat');

Reset(f1);

Read(f1,Obj^.o);

For i:=1 to Obj^.Count do

New(Obj^.Side[i],Create(Obj^.o.x,Obj^.o.y,Obj^.o.z,0,0,0,a));

For i:=1 to Obj^.Count do begin

inc(CS);

if(i mod 2<>0)Then

T:=ColorText(RGB(100+Random(155),100+Random(155),100+Random(155)),GMT)

else T:=T;

Obj^.Side[i]^.Texture:=T;

Obj^.Side[i]^.Mode:=Mode;

Obj^.Side[i]^.Alpha:=0;

Read(f1,S);

For j:=1 to 3 do

begin

Read(f1,S);

Obj^.Side[i]^.S[j]:=S;

SS^[i,j]:=S;

end;

end;

close(f1);

end;

procedure SaveObject(Name:String;var Obj:PObj);

var f1:file of TPoint;

f2:file of Word;B:Boolean;

S:TPoint;i,j:word;Name2:String;

begin

B:=False;

For i:=1 to Length(Name) do

if(Name[i]='.')Then B:=True;

if(B=True)Then begin

For i:=1 to Length(Name)-4 do begin

Name2:=Name2+Name[i];

end;

Name:=Name2;

end;

Assign(f2,Name+'.res');

Rewrite(f2);

Reset(f2);

Write(f2,Obj^.Count);

close(f2);

Assign(f1,Name+'.dat');

Rewrite(f1);

Reset(f1);

Write(f1,Obj^.o);

For i:=1 to Obj^.Count do begin

Obj^.Side[i]^.o:=tmp_o;

S:=Obj^.Side[i]^.o;

Write(f1,S);

For j:=1 to 3 do

begin

S:=Obj^.Side[i]^.S[j];

Write(f1,S);

end;

end;

close(f1);

end;

procedure CreateTMP(var Obj:PObj);

var i,j,k:Word;

begin

Obj^.o.x:=0;Obj^.o.y:=0;Obj^.o.z:=100;

For i:=1 to Obj^.Count do

begin

tmp_o:=Obj^.Side[i]^.o;

Obj^.Side[i]^.o:=o[i];

For j:=1 to 3 do

Obj^.Side[i]^.S[j]:=S^[i,j];

end;

end;

procedure ShowSide;

var i,j,k,cx,cy:Word;tmp:TPoint;

begin

Lab.Vid.Surface.Fill(0);

myForm(GetMaxX,GetMaxY);

For i:=1 to CS do begin

Line(Trunc(SS^[i,1].x),Trunc(SS^[i,1].y),Trunc(SS^[i,2].x),Trunc(SS^[i,2].y),White);

Line(Trunc(SS^[i,2].x),Trunc(SS^[i,2].y),Trunc(SS^[i,3].x),Trunc(SS^[i,3].y),White);

Line(Trunc(SS^[i,3].x),Trunc(SS^[i,3].y),Trunc(SS^[i,1].x),Trunc(SS^[i,1].y),White);

Line(Trunc(SS^[i,1].x),Trunc(SS^[i,1].z),Trunc(SS^[i,2].x),Trunc(SS^[i,2].z),White);

Line(Trunc(SS^[i,2].x),Trunc(SS^[i,2].z),Trunc(SS^[i,3].x),Trunc(SS^[i,3].z),White);

Line(Trunc(SS^[i,3].x),Trunc(SS^[i,3].z),Trunc(SS^[i,1].x),Trunc(SS^[i,1].z),White);

Line(Trunc(SS^[i,1].z*r),Trunc(SS^[i,1].y),Trunc(SS^[i,2].z*r),Trunc(SS^[i,2].y),White);

Line(Trunc(SS^[i,2].z*r),Trunc(SS^[i,2].y),Trunc(SS^[i,3].z*r),Trunc(SS^[i,3].y),White);

Line(Trunc(SS^[i,3].z*r),Trunc(SS^[i,3].y),Trunc(SS^[i,1].z*r),Trunc(SS^[i,1].y),White);

if(ClicCub[i]=True)Then begin

Line(Trunc(SS^[i,1].z),Trunc(SS^[i,1].y),Trunc(SS^[i,2].z),Trunc(SS^[i,1].y),White);

Line(Trunc(SS^[i,2].z),Trunc(SS^[i,1].y),Trunc(SS^[i,1].z),Trunc(SS^[i,3].y),White);

end;

end;

S[CS]:=SS[CS];

cx:=GMX div 2;

cy:=GMY div 2+1;

For i:=1 to 3 do

begin

S^[CS,i].x:=S^[CS,i].x-cx+dF;

S^[CS,i].y:=cy-S^[CS,i].y-dF;

S^[CS,i].z:=S^[CS,i].z-cx-dF;

end;

end;

procedure CreateSide(var Obj:PObj);

var cx,cy,i,j,k:Word;

begin

r:=1;

For i:=1 to Obj^.Count do

S[i]:=Obj^.Side[i]^.S;

TMP:=S;

Lab.Vid.Surface.Fill(0);

myForm(GetMaxX,GetMaxY);

cx:=GMX div 2;

cy:=GMY div 2+1;

For j:=1 to CS do

For i:=1 to 3 do

begin

TMP^[j,i].x:=TMP^[j,i].x-cx+dF;

TMP^[j,i].y:=cy-TMP^[j,i].y-dF;

TMP^[j,i].z:=TMP^[j,i].z-cx-dF;

end;

For i:=1 to CS do begin

Line(Trunc(TMP^[i,1].x),Trunc(TMP^[i,1].y),Trunc(TMP^[i,2].x),Trunc(TMP^[i,2].y),White);

Line(Trunc(TMP^[i,2].x),Trunc(TMP^[i,2].y),Trunc(TMP^[i,3].x),Trunc(TMP^[i,3].y),White);

Line(Trunc(TMP^[i,3].x),Trunc(TMP^[i,3].y),Trunc(TMP^[i,1].x),Trunc(TMP^[i,1].y),White);

Line(Trunc(TMP^[i,1].x),Trunc(TMP^[i,1].z),Trunc(TMP^[i,2].x),Trunc(TMP^[i,2].z),White);

Line(Trunc(TMP^[i,2].x),Trunc(TMP^[i,2].z),Trunc(TMP^[i,3].x),Trunc(TMP^[i,3].z),White);

Line(Trunc(TMP^[i,3].x),Trunc(TMP^[i,3].z),Trunc(TMP^[i,1].x),Trunc(TMP^[i,1].z),White);

Line(Trunc(TMP^[i,1].z*r),Trunc(TMP^[i,1].y),Trunc(TMP^[i,2].z*r),Trunc(TMP^[i,2].y),White);

Line(Trunc(TMP^[i,2].z*r),Trunc(TMP^[i,2].y),Trunc(TMP^[i,3].z*r),Trunc(TMP^[i,3].y),White);

Line(Trunc(TMP^[i,3].z*r),Trunc(TMP^[i,3].y),Trunc(TMP^[i,1].z*r),Trunc(TMP^[i,1].y),White);

end;

Flip(Lab.Vid);

end;

procedure LoadSide(var Obj:PObj);

var i,j,k:Word;TMP:PPL;

begin

r:=1;

For i:=1 to Obj^.Count do

S[i]:=Obj^.Side[i]^.S;

SS^:=S^;

Lab.Vid.Surface.Fill(0);

myForm(GetMaxX,GetMaxY);

For j:=1 to CS do

For i:=1 to 3 do

begin

SS^[j,i].x:=SS^[j,i].x+cx-dF;

SS^[j,i].y:=cy-SS^[j,i].y-dF;

SS^[j,i].z:=SS^[j,i].z+cx+dF;

end;

For i:=1 to CS do begin

Line(Trunc(SS^[i,1].x),Trunc(SS^[i,1].y),Trunc(SS^[i,2].x),Trunc(SS^[i,2].y),White);

Line(Trunc(SS^[i,2].x),Trunc(SS^[i,2].y),Trunc(SS^[i,3].x),Trunc(SS^[i,3].y),White);

Line(Trunc(SS^[i,3].x),Trunc(SS^[i,3].y),Trunc(SS^[i,1].x),Trunc(SS^[i,1].y),White);

Line(Trunc(SS^[i,1].x),Trunc(SS^[i,1].z),Trunc(SS^[i,2].x),Trunc(SS^[i,2].z),White);

Line(Trunc(SS^[i,2].x),Trunc(SS^[i,2].z),Trunc(SS^[i,3].x),Trunc(SS^[i,3].z),White);

Line(Trunc(SS^[i,3].x),Trunc(SS^[i,3].z),Trunc(SS^[i,1].x),Trunc(SS^[i,1].z),White);

Line(Trunc(SS^[i,1].z*r),Trunc(SS^[i,1].y),Trunc(SS^[i,2].z*r),Trunc(SS^[i,2].y),White);

Line(Trunc(SS^[i,2].z*r),Trunc(SS^[i,2].y),Trunc(SS^[i,3].z*r),Trunc(SS^[i,3].y),White);

Line(Trunc(SS^[i,3].z*r),Trunc(SS^[i,3].y),Trunc(SS^[i,1].z*r),Trunc(SS^[i,1].y),White);

end;

Flip(Lab.Vid);

end;

procedure InPut1(x,y:integer;var E:Boolean);

var i,j:integer;B:Boolean;

begin

if(sv=0)Then begin

o[CS].x:=0;

o[CS].y:=0;

o[CS].z:=0;

end;

if(M<sv)Then begin

PutPixel(x,y,Yelow);

Flip(Lab.Vid);

if(x<cx)and(y<cy)Then

begin

o[CS].x:=x-cx+dF;

o[CS].y:=cy-y-dF;

end

else if(x>cx)and(y<cy)Then

begin

o[CS].z:=x-cx-dF;

o[CS].y:=cy-y-dF;

end

else if(x<cx)and(y>cy)Then

begin

o[CS].x:=x-cx+dF;

o[CS].z:=x-cx-dF;

end

else begin

ShowMessage('Эта четверть не является плоскостью проекций!');

E:=True;

end;

inc(M);

end

else

if (M<6+sv)Then begin

if(N>3)Then N:=0;

if(x>cx)and(y>cy)Then

else

if(N=0)Then begin

x0:=x;y0:=y;x1:=0;y1:=0;

N:=1;

end;

if(x<cx)and(y<cy)Then

begin

SS^[CS,N].x:=x;

SS^[CS,N].y:=y;

r:=1;

end

else if(x>cx)and(y<cy)Then

begin

SS^[CS,N].z:=x;

SS^[CS,N].y:=y;

r:=1;

end

else if(x<cx)and(y>cy)Then

begin

SS^[CS,N].x:=x;

SS^[CS,N].z:=y;

r:=GetMaxX/GetMaxY;

end

else begin

ShowMessage('Эта четверть не является плоскостью проекций!');

E:=True;

end;

if(E=False)Then begin

x2:=X;y2:=Y;

if(x1<>0)and(y1<>0)Then begin

Line(x0,y0,x2,y2,Red);

Line(x1,y1,x2,y2,Red);

end

else putpixel(x2,y2,Red);

Flip(Lab.Vid);

x1:=x2;y1:=y2;

For i:=1 to CS-1 do

For j:=1 to 3 do

begin

if(SS^[CS,N].x+o[CS].x>=SS^[i,j].x+o[i].x-2)and(SS^[CS,N].x+o[CS].x<=SS^[i,j].x+o[i].x+2)

and(SS^[CS,N].y+o[CS].y>=SS^[i,j].y+o[i].y-2)and(SS^[CS,N].y+o[CS].y<=SS^[i,j].y+o[i].y+2)

and(SS^[CS,N].z+o[CS].z>=SS^[i,j].z+o[i].z-2)and(SS^[CS,N].z+o[CS].z<=SS^[i,j].z+o[i].z+2)Then Accept:=True;

end;

inc(N);

inc(M);

end;

end;

if(M=6+sv)and(Accept=True)Then begin

M:=0;N:=0;

ClicCub[CS]:=False;

ShowSide;

Flip(Lab.Vid);

New(Scene^.Camera[CC]^.Obj[CO]^.Side[CS],Create(0,0,100,o[CS].x,o[CS].y,o[CS].z,S^[CS]));

if(CS mod 2<>0)Then

T:=ColorText(RGB(100+Random(155),100+Random(155),100+Random(155)),GMT)

else T:=T;

Scene^.Camera[CC]^.Obj[CO]^.Side[CS]^.Texture:=T;

Scene^.Camera[CC]^.Obj[CO]^.Side[CS]^.Mode:=Mode;

Scene^.Camera[CC]^.Obj[CO]^.Side[CS]^.Alpha:=0;

Accept:=False;

inc(CS);

end

else if(M=6+sv)and(Accept=False)Then begin

SS[CS]:=SS[CS-1];

SS[CS]:=SS[CS-1];

SS[CS]:=SS[CS-1];

M:=0;N:=0;

ShowSide;

Flip(Lab.Vid);

end;

end;

procedure InPut2(x,y:integer;var E:Boolean);

var i,j:integer;o:TPoint;Party:Single;tmp:Single;

begin

if(N<sv)Then begin

if(x<cx)and(y<cy)Then

begin

oc.x:=x-cx+dF;

oc.y:=cy-y-dF;

end

else if(x>cx)and(y<cy)Then

begin

oc.z:=x-cx-dF;

oc.y:=cy-y-dF;

end

else if(x<cx)and(y>cy)Then

begin

oc.x:=x-cx+dF;

oc.z:=x-cx-dF;

end

else begin

ShowMessage('Эта четверть не является плоскостью проекций!');

E:=True;

end;

if(E=False)Then begin

PutPixel(x,y,Yelow);

Flip(Lab.Vid);

inc(N);

end;

end

else

if(N<2+sv)and(N>sv-1)Then begin

if(N=sv)Then begin

x0:=x;y0:=y;

end;

x1:=x;y1:=y;

Line(x0,y0,x1,y1,Red);

Flip(Lab.Vid);

x0:=x;y0:=y;

inc(N);

if(x>cx)and(y>cy)Then begin

ShowMessage('Эта четверть не является плоскостью проекций!');

E:=True;

end

else begin

w[N-sv].x:=x;

w[N-sv].y:=y;

end;

end;

if(N=2+sv)Then begin

if(abs(w[N-sv].x-w[N-sv-1].x)/2<=abs(w[N-sv].y-w[N-sv-1].y)/2)Then Party:=abs(w[N-sv].x-w[N-sv-1].x)

else Party:=abs(w[N-sv].y-w[N-sv-1].y);

if(w[N-sv].x<cx)and(w[N-sv].y<cy)Then begin

o.x:=(w[N-sv].x+w[N-sv-1].x)/2;

o.y:=(w[N-sv].y+w[N-sv-1].y)/2;

o.z:=cx+Party/2+dF;

end

else if(w[N-sv].x>cx)and(w[N-sv-1].y<cy)Then begin

o.x:=cx-Party/2-dF;

o.y:=(w[N-sv].y+w[N-sv-1].y)/2;

o.z:=(w[N-sv].x+w[N-sv-1].x)/2;

end

else if(w[N-sv].x<cx)and(w[N-sv].y>cy)Then begin

o.x:=(w[N-sv].x+w[N-sv-1].x)/2;

o.y:=cy-Party/2-dF;

o.z:=(w[N-sv].y+w[N-sv-1].y)/2;

end;

r:=1;

SS^[CS,1].x:=o.x-Party/2;SS^[CS,1].y:=o.y+Party/2;SS^[CS,1].z:=o.z+Party/2;

SS^[CS,2].x:=o.x+Party/2;SS^[CS,2].y:=o.y+Party/2;SS^[CS,2].z:=o.z+Party/2;

SS^[CS,3].x:=o.x-Party/2;SS^[CS,3].y:=o.y-Party/2;SS^[CS,3].z:=o.z-Party/2;

SS^[CS+1,1].x:=o.x+Party/2;SS^[CS+1,1].y:=o.y-Party/2;SS^[CS+1,1].z:=o.z-Party/2;

SS^[CS+1,2].x:=o.x+Party/2;SS^[CS+1,2].y:=o.y+Party/2;SS^[CS+1,2].z:=o.z+Party/2;

SS^[CS+1,3].x:=o.x+Party/2;SS^[CS+1,3].y:=o.y+Party/2;SS^[CS+1,3].z:=o.z-Party/2;

SS^[CS+2,1].x:=o.x-Party/2;SS^[CS+2,1].y:=o.y+Party/2;SS^[CS+2,1].z:=o.z+Party/2;

SS^[CS+2,2].x:=o.x-Party/2;SS^[CS+2,2].y:=o.y-Party/2;SS^[CS+2,2].z:=o.z-Party/2;

SS^[CS+2,3]:=SS^[CS+1,1];

o.x:=o.x-cx+dF;

o.y:=cy-o.y-dF;

o.z:=o.z-cx-dF;

Scene^.Camera[CC]^.Obj[CO]^.Done;

Scene^.Camera[CC]^.Done;

Scene^.Done;

New(Cube,Create(0,0,100,o.x,o.y,o.z,Party,T,Mode,0));

Scene^.Camera[CC]^.ADD(CO,CO+1);

For i:=1 to Scene^.Camera[CC]^.Obj[CO]^.Count do

begin

For j:=1 to 3 do

S[i,j]:=Scene^.Camera[CC]^.Obj[CO]^.Side[i]^.S[j];

if(i mod 2<>0)Then

T:=ColorText(RGB(100+Random(155),100+Random(155),100+Random(155)),GMT)

else T:=T;

Scene^.Camera[CC]^.Obj[CO]^.Side[i]^.Texture:=T;

Scene^.Camera[CC]^.Obj[CO]^.Side[i]^.Mode:=Mode;

end;

For i:=CS to CS+12 do

ClicCub[i]:=True;

CS:=CS+12;

N:=0;

Scene^.Camera[CC]^.Obj[CO]^.Done;

Scene^.Camera[CC]^.Done;

Scene^.Done;

ShowSide;

Flip(Lab.Vid);

end;

end;

procedure Data;

var i,j:Word;

begin

New(S);

New(SS);

GetMaxX:=Lab.Vid.Width-1;

GetMaxY:=Lab.Vid.Height-1;

M:=0;N:=0;Rot:=YRot;Bol:=False;

GMX:=Lab.Vid.Width;

GMY:=Lab.Vid.Height;

CSc:=1;CC:=1;CL:=1;CO:=1;CS:=1;

cx:=GMX div 2;

cy:=GMY div 2+1;

Blue:=RGB(255,0,0);

Red:=RGB(200,200,200);

Yelow:=RGB(0,255,0);

New(Scene,Create);

New(Scene^.Camera[CC],Create(0,0,0,0,0,0));

New(Light[CL],Create(-100,-100,0));

New(Scene^.Camera[CC]^.Obj[CO],Create(0,0,0,0,0,0));

Accept:=True;View:=False;

end;

procedure TLab.FormCreate(Sender: TObject);

var i,j:Word;

begin

Tables;

@InPut[1]:=@InPut1;

@InPut[2]:=@InPut2;

New(t);

For i:=0 to GMT do

New(t^[i]);

For i:=0 to GMT do

For j:=0 to GMT do

t^[i]^[j]:=RGB(255,0,0);

Data;

end;

procedure TLab.TimerTimer(Sender: TObject; LagCount: Integer);

var i:Word;

begin

if(Keys[VK_Escape])Then halt;

if(Bol=False)and(Keys[VK_F9])and(CountSide>0)Then begin

if(CountSide=0)Then begin

ShowMessage('Для просмотра нужна хотя бы одна грань');

exit;

end;

Bol:=True;View:=True;

Scene^.Camera[CC]^.Obj[CO]^.Done;

Scene^.Camera[CC]^.Done;

Scene^.Done;

CreateTMP(Scene^.Camera[CC]^.Obj[CO]);

For i:=1 to Scene^.Camera[CC]^.Obj[CO]^.Count do

Scene^.Camera[CC]^.Obj[CO]^.Side[i]^.o:=tmp_o;

Vid.Surface.Fill(0);

Scene^.Camera[CC]^.Obj[CO]^.Draw;

Flip(Vid);

end

else if(Bol)Then

if(Keys[ord('Y')])Then

begin

Rot:=YRot;

Vid.Surface.Fill(0);

Scene^.Camera[CC]^.Obj[CO]^.Rotate(CosA,SinA,Angle,Rot);

Scene^.Camera[CC]^.Obj[CO]^.Draw;

Flip(Vid);

end

else if(Keys[Ord('X')])Then

begin

Rot:=XRot;

Vid.Surface.Fill(0);

Scene^.Camera[CC]^.Obj[CO]^.Rotate(CosA,SinA,Angle,Rot);

Scene^.Camera[CC]^.Obj[CO]^.Draw;

Flip(Vid);

end

else if(Keys[Ord('Z')])Then

begin

Rot:=ZRot;

Vid.Surface.Fill(0);

Scene^.Camera[CC]^.Obj[CO]^.Rotate(CosA,SinA,Angle,Rot);

Scene^.Camera[CC]^.Obj[CO]^.Draw;

Flip(Vid);

end

else if(Keys[VK_Space])Then

begin

if(Space.Visible=False)Then begin

Space.Visible:=True;

Enter.Visible:=True;

BSide.Visible:=True;

BCube.Visible:=True;

Menu.Items[0].Visible:=True;

Menu.Items[1].Visible:=True;

Menu.Items[2].Visible:=True;

Width:=455;

Height:=465;