Міністерство освіти та науки України
Національний університет “Львівська політехніка”
Інститут комп’ютерних наук та інформаційних технологій
Кафедра автоматизованих систем управління
Лабораторна робота № 3
з дисципліни “Комп’ютерна графіка”
Текст програми:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, Mask, ExtCtrls, Math, Grids;
type
TForm1 = class(TForm)
PaintBox1: TPaintBox;
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
RadioGroup1: TRadioGroup;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
TrackBar1: TTrackBar;
TrackBar2: TTrackBar;
Label1: TLabel;
Label2: TLabel;
procedure OsiXY();
procedure Ploshchuna();
procedure ParalPr();
procedure CosPr();
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure RadioButton1Click(Sender: TObject);
procedure RadioButton2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
h,w:integer;
dn,dnz,nz,d:integer;
x0,y0,z,x,y:integer;
a:double;
implementation
{$R *.dfm}
procedure TForm1.OsiXY();
var
n:integer;
i:integer;
begin
a:=pi/6;
PaintBox1.Repaint;
h:=PaintBox1.Height;
w:=PaintBox1.Width;
x0:=w div 2;
y0:=h div 2;
with Form1.PaintBox1.Canvas do begin
Pen.Width:=1;
TextOut(w-10,y0+10, 'Z');
TextOut(x0+10,0, 'Y');
TextOut(5,y0+round(tan(a)*x0)+5,'X');
//X
Pen.Color := clBlack;
MoveTo(x0,y0);
LineTo(w,y0);
//Y
Pen.Color := clBlack;
MoveTo(x0,0);
LineTo(x0,y0);
//Z
Pen.Color := clBlack;
MoveTo(x0,y0);
LineTo(0,y0+round(tan(a)*x0));
//Стрілочки Z
MoveTo(0,y0+round(tan(a)*x0));
LineTo(4,y0+round(tan(a)*x0)-10);
MoveTo(0,y0+round(tan(a)*x0));
LineTo(10,y0+round(tan(a)*x0)+1);
//Стрілочки X i Y
MoveTo(w,y0);
LineTo(w-10,y0-5);
MoveTo(w,y0);
LineTo(w-10,y0+5);
MoveTo(x0,0);
LineTo(x0-5,10);
MoveTo(x0,0);
LineTo(x0+5,10);
//Поділки Z
nz:=20;
dnz:=x0;
for i:=1 to x0 do begin
dnz:=dnz-nz;
z:=x0-dnz;
x:=x0-round(z*cos(a));
y:=y0+round(z*sin(a));
MoveTo(x-3,y-3);
LineTo(x+4,y+4);
end;
//Поділки X i Y
dn:=25;
n:=0;
for i := 1 to w do begin
n:=n+dn;
MoveTo(x0+n,y0-5);
LineTo(x0+n,y0+5);
MoveTo(x0-5,y0-n);
LineTo(x0+5,y0-n);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
OsiXY();
Ploshchuna();
if RadioButton1.Checked then ParalPr();
if RadioButton2.Checked then CosPr();
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Close();
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
PaintBox1.Repaint;
end;
procedure TForm1.RadioButton1Click(Sender: TObject);
begin
StringGrid1.ColCount:=5;
StringGrid1.Width:=329;
StringGrid1.Cells[0,1]:='Початкове';
StringGrid1.Cells[1,0]:='X';
StringGrid1.Cells[2,0]:='Y';
StringGrid1.Cells[3,0]:='Z';
StringGrid1.Cells[4,0]:='D';
StringGrid2.Cells[0,1]:='Спроект.';
StringGrid2.Cells[1,0]:='X';
StringGrid2.Cells[2,0]:='Y';
StringGrid2.Cells[3,0]:='Z';
StringGrid2.Cells[1,1]:='';
StringGrid2.Cells[2,1]:='';
StringGrid2.Cells[3,1]:='';
StringGrid1.Cells[1,1]:='0';
StringGrid1.Cells[2,1]:='5';
StringGrid1.Cells[3,1]:='5';
StringGrid1.Cells[4,1]:='3';
end;
procedure TForm1.RadioButton2Click(Sender: TObject);
begin
StringGrid1.ColCount:=4;
StringGrid1.Width:=264;
StringGrid1.Cells[0,1]:='Початкове';
StringGrid1.Cells[1,0]:='L';
StringGrid1.Cells[2,0]:='Alpha';
StringGrid1.Cells[3,0]:='Beta';
StringGrid2.Cells[0,1]:='Спроект.';
StringGrid2.Cells[1,0]:='X';
StringGrid2.Cells[2,0]:='Y';
StringGrid2.Cells[3,0]:='Z';
StringGrid2.Cells[1,1]:='';
StringGrid2.Cells[2,1]:='';
StringGrid2.Cells[3,1]:='';
StringGrid1.Cells[1,1]:='3';
StringGrid1.Cells[2,1]:='45';
StringGrid1.Cells[3,1]:='67';
end;
procedure TForm1.Ploshchuna();
var m1,m2:integer;
i,k:integer;
begin
m1:=TrackBar1.Position;
m2:=TrackBar2.Position;
if RadioButton1.Checked then
d:=StrToInt(StringGrid1.Cells[4,1])*dn;
if RadioButton2.Checked then
d:=0;
with Form1.PaintBox1.Canvas do begin
Pen.Color := clBlue;
Pen.Width:=2;
z:=+m1;
x:=x0-round(z*cos(a));
y:=y0+round(z*sin(a));
MoveTo(Round(x+d),Round(y));//101
z:=-m1;
x:=x0-round(z*cos(a));
y:=y0+round(z*sin(a));
LineTo(Round(x+d),Round(y));//100
z:=+m1;
x:=x0-round(z*cos(a));
y:=y0+round(z*sin(a))-m2;
MoveTo(Round(x+d),Round(y));//111
z:=-m1;
x:=x0-round(z*cos(a));
y:=y0+round(z*sin(a))-m2;
LineTo(Round(x+d),Round(y));//110
z:=+m1;
x:=x0-round(z*cos(a));
y:=y0+round(z*sin(a));
MoveTo(Round(x+d),Round(y));//101
z:=+m1;
x:=x0-round(z*cos(a));
y:=y0+round(z*sin(a))-m2;
LineTo(Round(x+d),Round(y));//111
z:=-m1;
x:=x0-round(z*cos(a));
y:=y0+round(z*sin(a));
MoveTo(Round(x+d),Round(y));//100
z:=-m1;
x:=x0-round(z*cos(a));
y:=y0+round(z*sin(a))-m2;
LineTo(Round(x+d),Round(y));//110
k:=0;
for i := 1 to Round(m1/5-1) do begin
Pen.Color := clYellow;
Pen.Width:=1;
k:=k+10;
z:=+m1-k;
x:=x0-round(z*cos(a));
y:=y0+round(z*sin(a));
MoveTo(Round(x+d),Round(y));//101
z:=+m1-k;
x:=x0-round(z*cos(a));
y:=y0+round(z*sin(a))-m2;
LineTo(Round(x+d),Round(y));//111
end;
k:=0;
for i := 1 to Round(m2/10-1) do begin
Pen.Color := clYellow;
Pen.Width:=1;
k:=k+10;
z:=+m1;
x:=x0-round(z*cos(a));
y:=y0+round(z*sin(a))-k;
MoveTo(Round(x+d),Round(y));//101
z:=-m1;
x:=x0-round(z*cos(a));
y:=y0+round(z*sin(a))-k;
LineTo(Round(x+d),Round(y));//100
end;
end;
end;
procedure TForm1.ParalPr();
var x1,y1,z1:integer;
x_sh,y_sh,z_sh:double;
begin
with Form1.PaintBox1.Canvas do
begin
Pen.Color := clBlack;
x1:=StrToInt(StringGrid1.Cells[1,1]);
y1:=StrToInt(StringGrid1.Cells[2,1]);
z1:=StrToInt(StringGrid1.Cells[3,1]);
//x y z
x:=Round(x1*nz);
z:=x0-round(x*cos(a));
y:=y0+round(x*sin(a));
Brush.Color := clBlack;
Ellipse (Round(z+dn*z1)-3,Round(y-dn*y1)-3,Round(z+dn*z1)+3,Round(y-dn*y1)+3);
MoveTo(Round(z+dn*z1),Round(y-dn*y1));
LineTo(x0,y0);
//x' y' z'
x_sh:=(x1*(d/dn))/z1;
y_sh:=(y1*(d/dn))/z1;
z_sh:=d/dn;
x:=Round(x_sh*nz);
z:=x0-round(x*cos(a));
y:=y0+round(x*sin(a));
Ellipse (Round((z+dn*z_sh)-3),Round((y-dn*y_sh)-3),Round((z+dn*z_sh)+3),Round(y-dn*y_sh)+3);
StringGrid2.Cells[1,1]:=FloatToStr(x_sh);
StringGrid2.Cells[2,1]:=FloatToStr(y_sh);
StringGrid2.Cells[3,1]:=FloatToStr(z_sh);
end;
end;
procedure TForm1.CosPr();
var alpha,beta:double;
x_sh,y_sh,z_sh,l:double;
begin
with Form1.PaintBox1.Canvas do
begin
l:=StrToFloat(StringGrid1.Cells[1,1]);
alpha:=StrToFloat(StringGrid1.Cells[2,1])*pi/180;
beta:=StrToFloat(StringGrid1.Cells[3,1])*pi/180;
Pen.Color := clBlack;
Brush.Color := clBlack;
Ellipse (Round(x0+l*dn)-3,y0+0-3,Round(x0+l*dn)+3,y0+0+3);
x_sh:=l*cotan(beta)*cos(alpha);
y_sh:=l*cotan(beta)*sin(alpha);
z_sh:=0;
x:=Round(x_sh*nz);
z:=x0-round(x*cos(a));
y:=y0+round(x*sin(a));
MoveTo(Round(x0+l*dn),y0);
LineTo(Round(z+dn*z_sh),Round(y-dn*y_sh));
MoveTo(Round(z+dn*z_sh),Round(y-dn*y_sh));
LineTo(Round(z+dn*z_sh),y);
MoveTo(Round(z+dn*z_sh),Round(y-dn*y_sh));
LineTo(x0,Round(y0-dn*y_sh));
Ellipse (Round((z+dn*z_sh)-3),Round((y-dn*y_sh)-3),Round((z+dn*z_sh)+3),Round(y-dn*y_sh)+3);
StringGrid2.Cells[1,1]:=FloatToStr(x_sh);
StringGrid2.Cells[2,1]:=FloatToStr(y_sh);
StringGrid2.Cells[3,1]:=FloatToStr(z_sh);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
RadioButton1.Checked:=True;
end;
end.
Приклад роботи програми:
Висновок: В даній роботі я ознайомився з елементарним математичним апаратом плоских геометричних проекцій.