Міністерство освіти та науки України
Національний університет “Львівська політехніка”
Інститут комп’ютерних наук та інформаційних технологій
Кафедра автоматизованих систем управління
Лабораторна робота №1
з дисципліни “ Комп`ютерна графіка ”
Тема: «Робота в декартовій
системі координат»
Програмна реалізація:
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;
Button1: TButton;
Button2: TButton;
StringGrid1: TStringGrid;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
StringGrid2: TStringGrid;
Label1: TLabel;
Button3: TButton;
Label2: TLabel;
TrackBar1: TTrackBar;
ComboBox1: TComboBox;
procedure Button2Click(Sender: TObject);
procedure OsiXY();
procedure Tochka();
procedure Line();
procedure Trukytnuk();
procedure Zminu();
procedure Button1Click(Sender: TObject);
procedure RadioButton1Click(Sender: TObject);
procedure RadioButton2Click(Sender: TObject);
procedure RadioButton3Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
h,w:integer;//висота і ширина вікна
dn:integer;
implementation
{$R *.dfm}
procedure TForm1.OsiXY();
var
n:integer;
i:integer;
begin
dn:=TrackBar1.Position;
PaintBox1.Repaint;
h:=PaintBox1.Height;
w:=PaintBox1.Width;
with Form1.PaintBox1.Canvas do begin
TextOut(w-10,(h div 2)+10, 'X');
TextOut((w div 2)+10,0, 'Y');
MoveTo(0,(h div 2));
LineTo(w,(h div 2));
MoveTo((w div 2),0);
LineTo((w div 2),h);
MoveTo(w,(h div 2));
LineTo(w-10,(h div 2)-5);
MoveTo(w,(h div 2));
LineTo(w-10,(h div 2)+5);
MoveTo((w div 2),0);
LineTo((w div 2)-5,10);
MoveTo((w div 2),0);
LineTo((w div 2)+5,10);
n:=0;
for i := 1 to w do begin
n:=n+dn;
MoveTo((w div 2)+n,(h div 2)-5);
LineTo((w div 2)+n,(h div 2)+5);
MoveTo((w div 2)-n,(h div 2)-5);
LineTo((w div 2)-n,(h div 2)+5);
MoveTo((w div 2)-5,(h div 2)+n);
LineTo((w div 2)+5,(h div 2)+n);
MoveTo((w div 2)-5,(h div 2)-n);
LineTo((w div 2)+5,(h div 2)-n);
end;
end;
end;
procedure TForm1.Tochka();
var x1,y1,x0,y0:integer;//введена точка
x1p,y1p:integer;//перетворені значенння
a11,a12,a21,a22:integer;//елементи матриці
begin
x1:=strtoint(StringGrid1.Cells[1,1]);
y1:=strtoint(StringGrid1.Cells[2,1]);
a11:=strtoint(StringGrid2.Cells[1,1]);
a12:=strtoint(StringGrid2.Cells[2,1]);
a21:=strtoint(StringGrid2.Cells[1,2]);
a22:=strtoint(StringGrid2.Cells[2,2]);
x1p:=x1*a11+y1*a21;
y1p:=x1*a12+y1*a22;
x0:=(w div 2)+x1p*dn;
y0:=(h div 2)-y1p*dn;
with Form1.PaintBox1.Canvas do
begin
Brush.Color := clBlack;
Ellipse (x0-3,y0-3,x0+3,y0+3);
end;
end;
procedure TForm1.Line();
var x1,y1,x2,y2:integer;
x11,y11,x22,y22:integer;
x2p,y2p,x1p,y1p:integer;
a11,a12,a21,a22:integer;
begin
x1:=strtoint(StringGrid1.Cells[1,1]);
y1:=strtoint(StringGrid1.Cells[2,1]);
x2:=strtoint(StringGrid1.Cells[1,2]);
y2:=strtoint(StringGrid1.Cells[2,2]);
a11:=strtoint(StringGrid2.Cells[1,1]);
a12:=strtoint(StringGrid2.Cells[2,1]);
a21:=strtoint(StringGrid2.Cells[1,2]);
a22:=strtoint(StringGrid2.Cells[2,2]);
x1p:=x1*a11+y1*a21;
y1p:=x1*a12+y1*a22;
x2p:=x2*a11+y2*a21;
y2p:=x2*a12+y2*a22;
x11:=(w div 2)+x1p*dn;
y11:=(h div 2)-y1p*dn;
x22:=(w div 2)+x2p*dn;
y22:=(h div 2)-y2p*dn;
with Form1.PaintBox1.Canvas do
begin
Brush.Color := clBlack;
MoveTo(x11,y11);
LineTo(x22,y22);
end;
end;
procedure TForm1.Trukytnuk();
var x1,y1,x2,y2,x3,y3:integer;
x11,y11,x22,y22,x33,y33:integer;
a11,a12,a21,a22,a31,a32:integer;
x1p,y1p,x2p,y2p,x3p,y3p:integer;
begin
x1:=strtoint(StringGrid1.Cells[1,1]);
y1:=strtoint(StringGrid1.Cells[2,1]);
x2:=strtoint(StringGrid1.Cells[1,2]);
y2:=strtoint(StringGrid1.Cells[2,2]);
x3:=strtoint(StringGrid1.Cells[1,3]);
y3:=strtoint(StringGrid1.Cells[2,3]);
Zminu();
a11:=strtoint(StringGrid2.Cells[1,1]);
a12:=strtoint(StringGrid2.Cells[2,1]);
a21:=strtoint(StringGrid2.Cells[1,2]);
a22:=strtoint(StringGrid2.Cells[2,2]);
a31:=strtoint(StringGrid2.Cells[1,3]);
a32:=strtoint(StringGrid2.Cells[2,3]);
x1p:=x1*a11+y1*a21+a31;
y1p:=x1*a12+y1*a22+a32;
x2p:=x2*a11+y2*a21+a31;
y2p:=x2*a12+y2*a22+a32;
x3p:=x3*a11+y3*a21+a31;
y3p:=x3*a12+y3*a22+a32;
x11:=(w div 2)+x1p*dn;
y11:=(h div 2)-y1p*dn;
x22:=(w div 2)+x2p*dn;
y22:=(h div 2)-y2p*dn;
x33:=(w div 2)+x3p*dn;
y33:=(h div 2)-y3p*dn;
with Form1.PaintBox1.Canvas do
begin
Brush.Color := clBlack;
MoveTo(x11,y11);
LineTo(x22,y22);
MoveTo(x22,y22);
LineTo(x33,y33);
MoveTo(x33,y33);
LineTo(x11,y11);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
OsiXY();
if RadioButton1.Checked then
Tochka();
if RadioButton2.Checked then
Line();
if RadioButton3.Checked then
Trukytnuk();
end;
procedure TForm1.RadioButton1Click(Sender: TObject);
begin
StringGrid1.Cells[0,1]:='Точка';
StringGrid1.Cells[2,0]:='Y';
StringGrid1.Cells[1,0]:='X';
StringGrid1.Cells[2,0]:='Y';
StringGrid1.ColCount:=3;
StringGrid1.RowCount:=2;
StringGrid1.Cells[1,1]:='6';
StringGrid1.Cells[2,1]:='6';
StringGrid2.RowCount:=3;
StringGrid2.Cells[1,1]:='1';
StringGrid2.Cells[2,1]:='0';
StringGrid2.Cells[1,2]:='0';
StringGrid2.Cells[2,2]:='1';
Button1.Enabled:=true;
Combobox1.Visible:=False;
end;
procedure TForm1.RadioButton2Click(Sender: TObject);
begin
StringGrid1.Cells[0,1]:='Точка 1';
StringGrid1.Cells[0,2]:='Точка 2';
StringGrid1.Cells[1,0]:='X';
StringGrid1.Cells[2,0]:='Y';
StringGrid1.ColCount:=3;
StringGrid1.RowCount:=3;
StringGrid1.Cells[1,1]:='2';
StringGrid1.Cells[2,1]:='2';
StringGrid1.Cells[1,2]:='6';
StringGrid1.Cells[2,2]:='6';
StringGrid2.RowCount:=3;
StringGrid2.Cells[1,1]:='1';
StringGrid2.Cells[2,1]:='0';
StringGrid2.Cells[1,2]:='0';
StringGrid2.Cells[2,2]:='1';
Button1.Enabled:=true;
Combobox1.Visible:=False;
end;
procedure TForm1.RadioButton3Click(Sender: TObject);
begin
StringGrid1.Cells[0,1]:='Точка 1';
StringGrid1.Cells[0,2]:='Точка 2';
StringGrid1.Cells[0,3]:='Точка 3';
StringGrid1.Cells[1,0]:='X';
StringGrid1.Cells[2,0]:='Y';
StringGrid1.ColCount:=3;
StringGrid1.RowCount:=4;
StringGrid1.Cells[1,1]:='3';
StringGrid1.Cells[2,1]:='1';
StringGrid1.Cells[1,2]:='6';
StringGrid1.Cells[2,2]:='1';
StringGrid1.Cells[1,3]:='6';
StringGrid1.Cells[2,3]:='4';
StringGrid2.RowCount:=4;
StringGrid2.Cells[0,3]:='Зсув';
StringGrid2.Cells[1,3]:='0';
StringGrid2.Cells[2,3]:='0';
Button1.Enabled:=true;
Combobox1.Visible:=True;
StringGrid2.Cells[1,1]:='1';
StringGrid2.Cells[2,1]:='0';
StringGrid2.Cells[1,2]:='0';
StringGrid2.Cells[2,2]:='1';
StringGrid2.Cells[1,3]:='0';
StringGrid2.Cells[2,3]:='0';
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Close();
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
PaintBox1.Repaint;
end;
procedure TForm1.Zminu();
begin
if (ComboBox1.ItemIndex=-1) or (ComboBox1.ItemIndex=0) then begin
StringGrid2.Cells[1,1]:='1';
StringGrid2.Cells[2,1]:='0';
StringGrid2.Cells[1,2]:='0';
StringGrid2.Cells[2,2]:='1';
StringGrid2.Cells[1,3]:='0';
StringGrid2.Cells[2,3]:='0';
end
else case ComboBox1.ItemIndex of
1:begin//90
StringGrid2.Cells[1,1]:='0';
StringGrid2.Cells[2,1]:='1';
StringGrid2.Cells[1,2]:='-1';
StringGrid2.Cells[2,2]:='0';
end;
2:begin//180
StringGrid2.Cells[1,1]:='-1';
StringGrid2.Cells[2,1]:='0';
StringGrid2.Cells[1,2]:='0';
StringGrid2.Cells[2,2]:='-1';
end;
3:begin//270
StringGrid2.Cells[1,1]:='0';
StringGrid2.Cells[2,1]:='-1';
StringGrid2.Cells[1,2]:='1';
StringGrid2.Cells[2,2]:='0';
end;
4:begin//Відображення y=x
StringGrid2.Cells[1,1]:='0';
StringGrid2.Cells[2,1]:='1';
StringGrid2.Cells[1,2]:='1';
StringGrid2.Cells[2,2]:='0';
end;
5:begin//Відображення y=0
StringGrid2.Cells[1,1]:='1';
StringGrid2.Cells[2,1]:='0';
StringGrid2.Cells[1,2]:='0';
StringGrid2.Cells[2,2]:='-1';
end;
end;
end;
end.
Висновок: На даній лабораторній роботі я ознайомився з основами комп`ютерної графіки.