Anda di halaman 1dari 73

Wayan Firdaus Mahmudy

Program Diploma III


Manajemen Informatika & Teknik Komputer
Fakultas Matematika dan Ilmu Pengetahuan Alam
Universitas Brawijaya Malang

Grafika Komputer - Sept 2004 1


Bab 1. Fungsi Primitif dan Dasar-Dasar Operasi Grafik
pada Delphi 1
Bab 2. Kurva 9
Bab 3. Interpolasi 19
Bab 4. Fractal Garis 26
Bab 5. Fractal Bidang Kompleks 33
Bab 6. Obyek 2D 43
Bab 7. Obyek 3D 52
Bab 8. Dasar-Dasar Pengolahan Citra Digital 60

Grafika Komputer - Sept 2004 2


Bab Fungsi Primitif dan Dasar-Dasar
1 Operasi Grafik pada Delphi

Tujuan
• Mengambar bentuk-bentuk garis, persegi dan elips menggunakan fungsi
standar Delphi pada komponen PaintBox.
• Membuat fungsi primitif untuk menggambar garis dan elips.

Latihan 1.1
Pada latihan ini dikenalkan fungsi-fungsi standar Delphi untuk penggambaran
garis, persegi dan lingkaran.
Rancangan tampilan dibuat sebagai berikut:

PaintBox1

Isi event sebagai berikut:


Unit1.PAS
01 …
02 type
03 TForm1 = class(TForm)
04 PaintBox1: TPaintBox;
05 ButtonGaris: TButton;
06 ButtonHapus: TButton;
07 ButtonLingkaran: TButton;
Grafika Komputer - Sept 2004 1
08 ButtonKotak: TButton;
09 ButtonKeluar: TButton;
10 procedure ButtonKeluarClick(Sender: TObject);
11 procedure ButtonHapusClick(Sender: TObject);
12 procedure ButtonGarisClick(Sender: TObject);
13 procedure ButtonKotakClick(Sender: TObject);
14 procedure ButtonLingkaranClick(Sender:TObject);
15 end;
16 …
17 procedure TForm1.ButtonKeluarClick(Sender:TObject);
18 begin
19 Close;
20 end;
21
22 procedure TForm1.ButtonHapusClick(Sender: TObject);
23 begin
24 PaintBox1.Refresh;
25 end;
26
27 procedure TForm1.ButtonGarisClick(Sender: TObject);
28 var i:integer;
29 begin
30 PaintBox1.Canvas.Pen.Color := clRed;
31 for i:=1 to 40 do
32 begin
33 PaintBox1.Canvas.MoveTo (0,0);
34 PaintBox1.Canvas.LineTo (PaintBox1.Width,10*i);
35 end;
36 end;
37
38 procedure TForm1.ButtonKotakClick(Sender: TObject);
39 var i:integer; x1,x2,y1,y2:integer;
40 begin
41 PaintBox1.Canvas.Pen.Color := clRed;
42 x1 := 0;
43 x2 := PaintBox1.Width-1;
44 y1 := 0;
45 y2 := PaintBox1.Height-1;
46 for i:=1 to 8 do
47 begin
48 PaintBox1.Canvas.Rectangle (x1,y1,x2,y2);
49 Inc (x1,15); Inc (y1,10);
50 Dec (x2,15); Dec (y2,10);
51 end;

Grafika Komputer - Sept 2004 2


52 end;
53
54 procedure TForm1.ButtonLingkaranClick(Sender:
55 TObject);
56 var i:integer; x1,x2,y1,y2:integer;
57 begin
58 PaintBox1.Canvas.Pen.Color := clBlue;
59 x1 := 0;
60 x2 := PaintBox1.Width-1;
61 y1 := 0;
62 y2 := PaintBox1.Height-1;
63 for i:=1 to 8 do
64 begin
65 PaintBox1.Canvas.Ellipse (x1,y1,x2,y2);
66 Inc (x1,15); Inc (y1,10);
67 Dec (x2,15); Dec (y2,10);
68 end;
69 end;
Jika program dijalankan dan ButtonGaris diklik akan dihasilkan tampilan
sebagai berikut:

Tampilan pada PaintBox untuk button lainnya adalah sebagai berikut:

Grafika Komputer - Sept 2004 3


Setelah program di atas dijalankan cobalah untuk mengganti event untuk
ButtonLingkaran dengan mengubah cara penggambaran lingkaran dimulai
dari lingkaran terkecil ke lingkaran terbesar.
01 procedure TForm1.ButtonLingkaranClick(Sender:
02 TObject);
03 var i:integer; x1,x2,y1,y2:integer;
04 begin
05 PaintBox1.Canvas.Pen.Color := clBlue;
06 PaintBox1.Canvas.Brush.Style := bsClear;
07 x1 := PaintBox1.Width div 2 - 5 ;
08 x2 := x1 + 5;
09 y1 := PaintBox1.Height div 2 - 5;
10 y2 := y1 + 5;
11 for i:=1 to 8 do
12 begin
13 PaintBox1.Canvas.Ellipse (x1,y1,x2,y2);
14 Dec (x1,15); Dec (y1,10);
15 Inc (x2,15); Inc (y2,10);
16 end;

Grafika Komputer - Sept 2004 4


17 end;
Jika pada bagian modifikasi baris 6 yang berisi
PaintBox1.Canvas.Brush.Style := bsClear;
dihapus, apa yang terjadi pada tampilan program, mengapa ?
Tugas
Modifikasi tampilan program di atas sehinga tampilan untuk kotak dan
lingkaran menjadi sebagai berikut:

Latihan 1.2
Pada latihan ini dibuat fungsi primitif untuk menggambar garis dan elips.
Desain tampilan hampir sama dengan Latihan 1.1. Jika program dijalankan dan
ButtonGaris diklik akan dihasilkan tampilan sebagai berikut:

Grafika Komputer - Sept 2004 5


Jika ButtonEllipse diklik akan dihasilkan tampilan sebagai berikut:

Isi event sebagai berikut:


Unit1.PAS
01 …
02 type
03 TForm1 = class(TForm)
04 PaintBox1: TPaintBox;
05 ButtonGaris: TButton;
06 ButtonHapus: TButton;
07 ButtonEllipse: TButton;
08 ButtonKeluar: TButton;
09 procedure ButtonKeluarClick(Sender: TObject);
10 procedure ButtonHapusClick(Sender: TObject);
11 procedure ButtonGarisClick(Sender: TObject);
12 procedure ButtonEllipseClick(Sender: TObject);
13 end;
14
15 var
16 Form1: TForm1;
17
18 implementation
19
20 {$R *.DFM}
21
22 procedure Line (Canvas:TCanvas;
23 x1,y1,x2,y2:integer; Clr:TColor);
24 var
25 dx,dy,steps,k : integer;
Grafika Komputer - Sept 2004 6
26 xinc,yinc,x,y : real;
27 begin
28 dx := x2-x1;
29 dy := y2-y1;
30 if Abs(dx)>Abs(dy) then
31 steps := abs(dx)
32 else
33 steps := abs(dy);
34
35 xinc := dx/steps;
36 yinc := dy/steps;
37 x := x1;
38 y := y1;
39 Canvas.Pixels[Round(x),Round(y)] := Clr;
40 for k:=1 to steps do
41 begin
42 x := x + xinc;
43 y := y + yinc;
44 Canvas.Pixels[Round(x),Round(y)] := Clr;
45 end;
46 end;
47
48 procedure Ellipse (Canvas:TCanvas;
49 x1,y1,x2,y2:integer; Clr:TColor);
50 var x,y,xc,yc,a,b,aa,bb,xx:real;
51 begin
52 xc := (x2-x1) / 2 + x1 - 1;
53 yc := (y2-y1) / 2 + y1 - 1;
54 a := Abs(x2-x1)/2;
55 b := Abs(y2-y1)/2;
56 aa := Sqr (a);
57 bb := Sqr (b);
58
59 x := 0;
60 while x<=a do
61 begin
62 xx := Sqr(x);
63 y := Sqrt (bb*(1-xx/aa));
64 Canvas.Pixels[Round(xc+x),Round(yc+y)] := Clr;
65 Canvas.Pixels[Round(xc+x),Round(yc-y)] := Clr;
66 Canvas.Pixels[Round(xc-x),Round(yc+y)] := Clr;
67 Canvas.Pixels[Round(xc-x),Round(yc-y)] := Clr;
68 x := x + 1;
69 end;

Grafika Komputer - Sept 2004 7


70 end;
71
72 procedure TForm1.ButtonKeluarClick(Sender:TObject);
73 begin
74 Close;
75 end;
76
77 procedure TForm1.ButtonHapusClick(Sender: TObject);
78 begin
79 PaintBox1.Refresh;
80 end;
81
82 procedure TForm1.ButtonGarisClick(Sender: TObject);
83 var w,h:integer;
84 begin
85 w := PaintBox1.Width;
86 h := PaintBox1.Height;
87 Line (PaintBox1.Canvas, 0,0, w, 0, clRed);
88 Line (PaintBox1.Canvas, 0,0, w, h, clRed);
89 Line (PaintBox1.Canvas, 0,0, 0, h, clRed);
90 Line (PaintBox1.Canvas, w,h, w-160,h-50,clBlue);
91 end;
92
93 procedure TForm1.ButtonEllipseClick(Sender:TObject);
94 var i:integer; x1,x2,y1,y2:integer;
95 begin
96 x1 := 1;
97 x2 := PaintBox1.Width-2;
98 y1 := 1;
99 y2 := PaintBox1.Height-2;
100 Ellipse (PaintBox1.Canvas, x1,y1,x2,y2, clRed);
101 Ellipse (PaintBox1.Canvas, x1+60,y1+10,
102 x2-60,y2-10, clGreen);
103 end;

Tugas
Modifikasi program di atas sehingga tampilan elips tidak terputus.

Grafika Komputer - Sept 2004 8


Bab
Kurva
2
Tujuan
Mengambar bentuk-bentuk kurva pada sistem koordinat kartesius dan polar.

Latihan 2.1
Susun project untuk menggambar kurva sinus dengan tampilan sebagai berikut:

Anda harus menambahkan komponen non visual ColorDialog yang digunakan


untuk mengatur warna kurva yang dihasilkan.
Isi event sebagai berikut:
Unit1.PAS
01 type
02 TForm1 = class(TForm)
03 PaintBox1: TPaintBox;
04 ColorDialog1: TColorDialog;
05 ButtonHapus: TButton;
06 ButtonKeluar: TButton;
07 ButtonSin: TButton;
08 ButtonWarna: TButton;

Grafika Komputer - Sept 2004 9


09 procedure FormCreate(Sender: TObject);
10 procedure ButtonKeluarClick(Sender: TObject);
11 procedure ButtonHapusClick(Sender: TObject);
12 procedure ButtonSinClick(Sender: TObject);
13 procedure ButtonWarnaClick(Sender: TObject);
14 end;
15
16 var
17 Form1: TForm1;
18 Clr: TColor;
19
20 implementation
21
22 {$R *.DFM}
23
24 const
25 DeltaX = 0.5;
26 Periode = 4;
27
28 // fungsi yang akan digambar
29 function Func (x:real):real;
30 begin
31 x := x/180 * PI;
32 Result := Sin (Periode*x);
33 end;
34
35 // x1,x2,y1,y2 menunjukkan nilai koordinat
36 // pada bidang cartesius
37 // x1 : nilai koordinat paling kiri pada PaintBox
38 // x2 : nilai koordinat paling kanan pada PaintBox
39 // y1 : nilai koordinat paling bawah pada PaintBox
40 // y2 : nilai koordinat paling atas pada PaintBox
41 procedure GambarKurva (var PB:TPaintBox;
42 x1,y1,x2,y2:real; Clr:TColor);
43 var xs,ys,x,y:real; W,H:integer;
44 begin
45 PB.Canvas.Pen.Color := Clr;
46 W := PB.Width;
47 H := PB.Height;
48 PB.Canvas.Rectangle(0,0,W,H);
49 x := x1;
50 y := Func (x);
51 xs := (x-x1)/(x2-x1) * W;
52 ys := H - (y-y1)/(y2-y1)*H;

Grafika Komputer - Sept 2004 10


53 PB.Canvas.MoveTo (Round(xs),Round(ys));
54 repeat
55 x := x + DeltaX;
56 y := Func (x);
57 xs := (x-x1)/(x2-x1) * W;
58 ys := H - (y-y1)/(y2-y1)*H;
59 PB.Canvas.LineTo(Round(xs),Round(ys));
60 until x>=x2;
61 end;
62
63 procedure TForm1.FormCreate(Sender: TObject);
64 begin
65 Clr := clBlack;
66 end;
67
68 procedure TForm1.ButtonKeluarClick(Sender:TObject);
69 begin
70 Close;
71 end;
72
73 procedure TForm1.ButtonHapusClick(Sender: TObject);
74 begin
75 PaintBox1.Refresh;
76 end;
77
78 procedure TForm1.ButtonSinClick(Sender: TObject);
79 begin
80 GambarKurva (PaintBox1, 0,-2,360,2, Clr);
81 end;
82
83 procedure TForm1.ButtonWarnaClick(Sender: TObject);
84 begin
85 if ColorDialog1.Execute then
86 Clr := ColorDialog1.Color;
87 end;

Pengembangan
Modifikasi program di atas dengan membuat nilai DeltaX, Periode, x1, x2, y1,
y2 bisa diinputkan saat program berjalan dengan menggunakan komponen
Edit.

Grafika Komputer - Sept 2004 11


Latihan 2.2
Project ini hampir sama dengan Latihan 2.1 dengan tambahan beberapa kurva
lain.

Perhatikan fungsi kurva yang akan digambar dimasukkan sebagai parameter


pada procedure GambarKurva.
Unit1.PAS
01 type
02 TForm1 = class(TForm)
03 PaintBox1: TPaintBox;
04 ButtonHapus: TButton;
05 ButtonKeluar: TButton;
06 ButtonSin: TButton;
07 ButtonSinCos: TButton;
08 ButtonPoli: TButton;
09 procedure ButtonKeluarClick(Sender: TObject);
10 procedure ButtonHapusClick(Sender: TObject);
11 procedure ButtonSinClick(Sender: TObject);
12 procedure ButtonSinCosClick(Sender: TObject);
13 procedure ButtonPoliClick(Sender: TObject);
14 end;
15
16 var
17 Form1: TForm1;
18
19 implementation

Grafika Komputer - Sept 2004 12


20
21 {$R *.DFM}
22
23 const
24 DeltaX = 0.5;
25 Periode = 3;
26
27 type
28 TFuncX = function (x:real) : real;
29
30 {$F+}
31 function SinX (x:real):real;
32 begin
33 x := x/180 * PI;
34 SinX := Sin(Periode*x);
35 end;
36
37 function SinCosX (x:real):real;
38 begin
39 x := x/180*PI;
40 SinCosX := Sin(2*x) + Cos(x);
41 end;
42
43 function Poli (x:real):real;
44 begin
45 Poli := 0.5*x*x*x - 2*x*x + x ;
46 end;
47 {$F-}
48
49 procedure GambarKurva (FuncX:TFuncX;
50 x1,y1,x2,y2:real; var PaintBox1:TPaintBox);
51 var xs,ys,x,y:real; W,H:integer;
52 begin
53 W := PaintBox1.Width;
54 H := PaintBox1.Height;
55 PaintBox1.Canvas.Rectangle(0,0,W,H);
56 x := x1;
57 y := FuncX(x);
58 xs := (x-x1)/(x2-x1) * W;
59 ys := H - (y-y1)/(y2-y1)*H;
60 PaintBox1.Canvas.MoveTo(Round(xs),Round(ys));
61 repeat
62 x := x + DeltaX;
63 y := FuncX(x);

Grafika Komputer - Sept 2004 13


64 xs := (x-x1)/(x2-x1) * W;
65 ys := H - (y-y1)/(y2-y1)*H;
66 PaintBox1.Canvas.LineTo(Round(xs),Round(ys));
67 until x>=x2;
68 end;
69
70 procedure TForm1.ButtonKeluarClick(Sender:TObject);
71 begin
72 Close;
73 end;
74
75 procedure TForm1.ButtonHapusClick(Sender: TObject);
76 begin
77 PaintBox1.Refresh;
78 end;
79
80 procedure TForm1.ButtonSinClick(Sender: TObject);
81 begin
82 GambarKurva (SinX, 0,-2,360,2, PaintBox1);
83 end;
84
85 procedure TForm1.ButtonSinCosClick(Sender:TObject);
86 begin
87 GambarKurva (SinCosX, 0,-2,360,2, PaintBox1);
88 end;
89
90 procedure TForm1.ButtonPoliClick(Sender: TObject);
91 begin
92 GambarKurva (Poli, -5,-150,10,150, PaintBox1);
93 end;

Pengembangan
Seperti pada latihan sebelumnya modifikasi program di atas dengan membuat
nilai DeltaX, Periode, x1, x2, y1, y2 bisa diinputkan saat program berjalan
dengan menggunakan komponen Edit. Tambahkan juga pengatur warna kurva
seperti pada latihan sebelumnya.

Grafika Komputer - Sept 2004 14


Latihan 2.3
Susun project untuk menggambar kurva dalam koordinat polar dengan tampilan
sebagai berikut:

Isi event sebagai berikut:


Unit1.PAS
01 type
02 TForm1 = class(TForm)
03 PaintBox1: TPaintBox;
04 ButtonHapus: TButton;
05 ButtonKeluar: TButton;
06 ButtonLingkaran: TButton;
07 ButtonSinus: TButton;
08 ButtonSinCos: TButton;
09 procedure ButtonKeluarClick(Sender: TObject);
10 procedure ButtonHapusClick(Sender: TObject);
11 procedure ButtonLingkaranClick(Sender: TObject);
12 procedure ButtonSinusClick(Sender: TObject);
13 procedure ButtonSinCosClick(Sender: TObject);
14 end;
15
16 var
17 Form1: TForm1;
18
19 implementation
Grafika Komputer - Sept 2004 15
20
21 {$R *.DFM}
22
23 type
24 TFuncT = function (t:real) : real;
25
26 {$F+}
27 function Lingkaran (t:real) : real;
28 begin
29 Lingkaran := 5
30 end;
31
32 function Sinus (t:real) : real;
33 begin
34 t := t/180 * PI;
35 Sinus := Abs(5*Sin(t));
36 end;
37
38 function SinCos (t:real):real;
39 begin
40 t := t/180 * PI;
41 SinCos:= Abs(10*Sin(4*Cos(4*t)));
42 end;
43 {$F-}
44
45 procedure KonversiKoordinat (t,r:real; var x,y:real);
46 begin
47 x := r * Cos (t/180*PI);
48 y := r * Sin (t/180*PI);
49 end;
50
51 procedure GambarKurva (FuncT:TFuncT; t1,t2,dt,
52 x1,y1,x2,y2:real;
53 var PaintBox1:TPaintBox);
54 var xs,ys,t,x,y:real; W,H:integer;
55 begin
56 W := PaintBox1.Width;
57 H := PaintBox1.Height;
58 PaintBox1.Canvas.Rectangle(0,0,W,H);
59 t := t1;
60 KonversiKoordinat (t, FuncT(t), x,y);
61 xs := (x-x1)/(x2-x1) * W;
62 ys := H - (y-y1)/(y2-y1)*H;
63 PaintBox1.Canvas.MoveTo(Round(xs),Round(ys));

Grafika Komputer - Sept 2004 16


64 repeat
65 t := t + dt;
66 KonversiKoordinat (t, FuncT(t), x,y);
67 xs := (x-x1)/(x2-x1) * W;
68 ys := H - (y-y1)/(y2-y1)*H;
69 PaintBox1.Canvas.LineTo(Round(xs),Round(ys));
70 until t>=t2;
71 end;
72
73 procedure TForm1.ButtonKeluarClick(Sender:TObject);
74 begin
75 Close;
76 end;
77
78 procedure TForm1.ButtonHapusClick(Sender: TObject);
79 begin
80 PaintBox1.Refresh;
81 end;
82
83 procedure TForm1.ButtonLingkaranClick(Sender:
84 TObject);
85 begin
86 GambarKurva (Lingkaran, 0,360,1, -10,-10,10,10,
87 PaintBox1);
88 end;
89
90 procedure TForm1.ButtonSinusClick(Sender: TObject);
91 begin
92 GambarKurva (Sinus, 0,360,1, -10,-10,10,10,
93 PaintBox1);
94 end;
95
96 procedure TForm1.ButtonSinCosClick(Sender:TObject);
97 begin
98 GambarKurva (SinCos, 0,360,1, -10,-10,10,10,
99 PaintBox1);
100 end;

Grafika Komputer - Sept 2004 17


Pengembangan
Cobalah untuk mengganti fungsi Lingkaran sebagai berikut:
01 function Lingkaran (t:real) : real;
02 begin
03 case Round(t) of
04 0..90, 180..270, 360 : Lingkaran := 5
05 else
06 Lingkaran := 8;
07 end;
08 end;
Jika ButtonLingkaran diklik maka dihasilkan tampilan sebagai berikut:

Lakukan modifikasi fungsi Lingkaran untuk menghasilkan tampilan sebagai


berikut:

Grafika Komputer - Sept 2004 18


Bab
Interpolasi
3
Tujuan
Menggambar kurva hasil interpolasi beberapa titik.

Latihan 3
Pada project ini digunakan metode Interpolasi Newton Umum Beda Terbagi
untuk menghubungkan titik-titik yang ditulis pada StringGrid. Metode
interpolasi Newton ditulis pada unit INBT dan tidak dibahas secara matematis.
Sebagai panduan untuk menentukan posisi titik pada PaintBox digunakan
event OnMouseMove untuk menampilkan pada sebuah label posisi x dan y dari
kursor pada PaintBox.

Isi event sebagai berikut:


Unit1.PAS
01 type
02 TForm1 = class(TForm)
03 PaintBox1: TPaintBox;

Grafika Komputer - Sept 2004 19


04 ButtonHapus: TButton;
05 ButtonKeluar: TButton;
06 ButtonPlot: TButton;
07 ButtonKurva: TButton;
08 StringGrid1: TStringGrid;
09 LabelPos: TLabel;
10 procedure FormCreate(Sender: TObject);
11 procedure PaintBox1MouseMove(Sender: TObject;
12 Shift: TShiftState; X,Y: Integer);
13 procedure ButtonKeluarClick(Sender: TObject);
14 procedure ButtonHapusClick(Sender: TObject);
15 procedure ButtonPlotClick(Sender: TObject);
16 procedure ButtonKurvaClick(Sender: TObject);
17 end;
18
19 var
20 Form1: TForm1;
21
22 implementation
23
24 {$R *.DFM}
25
26 uses INBT;
27
28 var
29 Newton: TIntNewton;
30
31 procedure TForm1.FormCreate(Sender: TObject);
32 begin
33 StringGrid1.Cells [0,0] := ' X ';
34 StringGrid1.Cells [1,0] := ' Y ';
35 Newton := TIntNewton.Create;
36 end;
37
38 procedure TForm1.PaintBox1MouseMove(Sender:
39 TObject; Shift: TShiftState; X,Y: Integer);
40 begin
41 LabelPos.Caption := 'X='+IntToStr(x)+
42 ' Y='+IntToStr(y);
43 end;
44
45 procedure TForm1.ButtonKeluarClick(Sender:TObject);
46 begin
47 Close;

Grafika Komputer - Sept 2004 20


48 end;
49
50 procedure TForm1.ButtonHapusClick(Sender: TObject);
51 begin
52 PaintBox1.Refresh;
53 PaintBox1.Canvas.Rectangle (0,0, PaintBox1.Width,
54 PaintBox1.Height);
55 end;
56
57 procedure TForm1.ButtonPlotClick(Sender: TObject);
58 var i,x,y:integer;
59 begin
60 { gambar titik asal }
61 PaintBox1.Canvas.Pen.Color := clBlue;
62 for i:=1 to StringGrid1.RowCount do
63 if (StringGrid1.Cells[0,i]<>'')
64 and (StringGrid1.Cells[1,i]<>'') then
65 begin
66 x := StrToInt (StringGrid1.Cells[0,i]);
67 y := StrToInt (StringGrid1.Cells[1,i]);
68 PaintBox1.Canvas.Ellipse (x-2,y-2,x+2,y+2);
69 end;
70 end;
71
72 procedure TForm1.ButtonKurvaClick(Sender: TObject);
73 var x,y:real; i:TIndex;
74 begin
75 Newton.Clear;
76 for i:=1 to StringGrid1.RowCount do
77 if (StringGrid1.Cells[0,i]<>'')
78 and (StringGrid1.Cells[1,i]<>'') then
79 begin
80 x := StrToFloat (StringGrid1.Cells[0,i]);
81 y := StrToFloat (StringGrid1.Cells[1,i]);
82 Newton.Add (x,y);
83 end;
84
85 x := Newton.GetX (1);
86 y := Newton.GetY (1);
87 PaintBox1.Canvas.Pen.Color := clRed;
88 PaintBox1.Canvas.MoveTo (Round(x), Round(y));
89 while x<Newton.Xmax do
90 begin
91 PaintBox1.Canvas.LineTo (Round(x), Round(y));

Grafika Komputer - Sept 2004 21


92 x := x + 1;
93 y := Newton.Yint(x);
94 end;
95 end;

INBT.PAS
01 unit INBT;
02
03 { Interpolasi Newton Umum Beda Terbagi }
04
05 interface
06
07 const
08 MAXDATA = 20;
09
10 type
11 TIndex = 0..MAXDATA;
12 TFloat = real;
13 TArray1 = array [1..MAXDATA] of TFloat;
14 TArray2 = array [1..MAXDATA, 0..MAXDATA] of
15 TFloat;
16
17 TIntNewton = class
18 private
19 DataX : TArray1;
20 DataY : TArray2;
21 nData : TIndex;
22 public
23 constructor Create;
24 destructor Destroy;
25
26 procedure Clear;
27 procedure Add (x,y:TFloat);
28
29 function GetNumData : TIndex;
30 function Xmin : TFloat;
31 function Xmax : TFloat;
32 function GetX (i:integer) : TFloat;
33 function GetY (i:integer) : TFloat;
34 function Yint (Xi:TFloat) : TFloat;
35 end;
36
37 implementation

Grafika Komputer - Sept 2004 22


38
39 constructor TIntNewton.Create;
40 begin
41 Clear;
42 end;
43
44 destructor TIntNewton.Destroy;
45 begin
46 end;
47
48 procedure TIntNewton.Clear;
49 begin
50 nData := 0;
51 end;
52
53 procedure TIntNewton.Add (x,y:TFloat);
54 begin
55 Inc (nData);
56 DataX[nData] := x;
57 DataY[nData,0] := y;
58 end;
59
60 function TIntNewton.GetNumData : TIndex;
61 begin
62 GetNumData := nData;
63 end;
64
65 function TIntNewton.Xmin : TFloat;
66 begin
67 Xmin := DataX[1];
68 end;
69
70 function TIntNewton.Xmax : TFloat;
71 begin
72 Xmax := DataX[nData];
73 end;
74
75 function TIntNewton.GetX (i:integer) : TFloat;
76 begin
77 GetX := DataX[i];
78 end;
79
80 function TIntNewton.GetY (i:integer) : TFloat;
81 begin

Grafika Komputer - Sept 2004 23


82 GetY := DataY[i,0];
83 end;
84
85 // mencari perkiraan nilai Y pada posisi X
86
87 function TIntNewton.Yint (Xi:TFloat) : TFloat;
88 var i,j:TIndex;
89 Yi, TotX : TFloat;
90 N : TIndex;
91 X : TArray1;
92 Y : TArray2;
93 begin
94 N := nData;
95 X := DataX;
96 Y := DataY;
97
98 if (Xi<=X[1]) or (Xi>=X[N]) then
99 begin
100 Yint := 0;
101 Exit;
102 end;
103
104 i:=1;
105 while X[i]<Xi do Inc(i);
106 j := i-1;
107 N := N - j + 1;
108 for i:=1 to N do
109 begin
110 X[i] := X[i+j-1];
111 Y[i,0] := Y[i+j-1, 0]
112 end;
113
114 { hitung beda }
115 for j:=1 to N-1 do
116 for i:=1 to N-j do
117 Y[i,j] := (Y[i+1,j-1] - Y[i,j-1]) /
118 (X[i+j]-X[i]);
119
120 TotX := (Xi-X[1]);
121 Yi := Y[1,0];
122 for i:=1 to N-1 do
123 begin
124 Yi := Yi + TotX * Y[1,i];
125 TotX := TotX * (Xi-X[i+1]);

Grafika Komputer - Sept 2004 24


126 end;
127 Yint := Yi;
128 end;
129
130 end.

Pengembangan
Modifikasi program di atas sebagai berikut:
- Tambahkan satu buton untuk menghapus isi StringGrid.
- Tambahkan fasilitas sehingga user bisa menambahkan titik pada StringGrid
dengan mengklik pada PaintBox dan sekaligus menampilkan plot titik.

Grafika Komputer - Sept 2004 25


Bab
Fractal Garis
4
Tujuan
Menggambar berbagai macam fractal garis.

Latihan 4
Susun project untuk menggambar fractal garis dengan contoh tampilan sebagai
berikut (Quadric Koch orde 5):

Contoh fractal lain (kurva C orde 10) yang dihasilkan adalah:

Grafika Komputer - Sept 2004 26


Perhatikan dalam unit berikut event untuk empat buton penggambar fractal
mengarah ke satu procedur.
Unit1.PAS
01 type
02 TForm1 = class(TForm)
03 PaintBox1: TPaintBox;
04 ButtonClear: TButton;
05 BitBtnClose: TBitBtn;
06 ButtonTriKoch: TButton;
07 ButtonQuaKoch: TButton;
08 ButtonKurvaC: TButton;
09 ButtonLayangLayang: TButton;
10
11 SpinEditOrde: TSpinEdit;
12 SpinEditArah: TSpinEdit;
13 SpinEditPanjang: TSpinEdit;
14 SpinEditX: TSpinEdit;
15 SpinEditY: TSpinEdit;
16 LabelFractal: TLabel;
17 Label1: TLabel;
18 Label2: TLabel;
19 Label3: TLabel;
20 Label4: TLabel;
21 Label5: TLabel;
22 procedure FormCreate(Sender: TObject);
23 procedure PaintBox1MouseMove(Sender: TObject;
24 Shift: TShiftState; X,Y: Integer);
25 procedure ButtonClearClick(Sender: TObject);
26 procedure ButtonDrawFractalClick(Sender:TObject);
27 end;
28
29 var
30 Form1: TForm1;
31
32 implementation
33 {$R *.DFM}
34
35 uses Fractal;
36
37 procedure TForm1.FormCreate(Sender: TObject);
38 begin
39 PtrPaintBox := @PaintBox1;
40 end;

Grafika Komputer - Sept 2004 27


41
42 procedure TForm1.PaintBox1MouseMove(Sender:TObject;
43 Shift: TShiftState; X,Y: Integer);
44 begin
45 Caption := 'Fractal Generator - X, Y : ' +
46 IntToStr(X) + ',' + IntToStr(Y);
47 end;
48
49 procedure TForm1.ButtonClearClick(Sender: TObject);
50 begin
51 PaintBox1.Refresh;
52 end;
53
54 procedure TForm1.ButtonDrawFractalClick(Sender:TObject);
55 var x,y,Orde,Arah,Panjang:integer;
56 begin
57 x := SpinEditX.Value;
58 y := SpinEditY.Value;
59 PaintBox1.Canvas.MoveTo (x,y);
60 Orde := SpinEditOrde.Value;
61 Arah := SpinEditArah.Value;
62 Panjang := SpinEditPanjang.Value;
63
64 if Sender=ButtonTriKoch then
65 begin
66 LabelFractal.Caption := 'Triadic Koch';
67 TriadicKoch (Orde, Arah, Panjang);
68 end
69 else if Sender=ButtonQuaKoch then
70 begin
71 LabelFractal.Caption := 'Quadric Koch';
72 QuadricKoch (Orde, Arah, Panjang);
73 end
74 else if Sender=ButtonKurvaC then
75 begin
76 LabelFractal.Caption := 'Kurva C';
77 KurvaC (Orde, Arah, Panjang);
78 end
79 else if Sender=ButtonLayangLayang then
80 begin
81 LabelFractal.Caption := 'Layang-Layang';
82 LayangLayang (Orde, Arah, Panjang);
83 end;
84 end;

Grafika Komputer - Sept 2004 28


Unit berikut berisi prosedur penggambar beberapa fractal. Fractal Quadric
Koch dan Kurva C tidak ditulis dan anda harus melengkapi sendiri.
Fractal.PAS
01 unit Fractal;
02
03 interface
04
05 uses
06 WinTypes, ExtCtrls;
07
08 var
09 PtrPaintBox : ^TPaintBox;
10
11 procedure TriadicKoch (Orde:byte; Arah,Panjang:real);
12 procedure QuadricKoch (Orde:byte; Arah,Panjang:real);
13 procedure KurvaC (Orde:byte; Arah,Panjang:real);
14 procedure LayangLayang (Orde:byte; Arah,Panjang:real);
15
16 implementation
17
18 procedure LineRel (dx,dy:integer);
19 var Point:TPoint;
20 begin
21 Point := PtrPaintBox^.Canvas.PenPos;
22 PtrPaintBox^.Canvas.LineTo (Point.X+dx, Point.Y+dy);
23 end;
24
25 procedure TriadicKoch (Orde:byte; Arah,Panjang:real);
26 var dX,dY:integer;
27 begin
28 if Orde>0 then
29 begin
30 TriadicKoch (Orde-1,Arah,Panjang/3);
31 Arah := Arah+60;
32 TriadicKoch (Orde-1,Arah,Panjang/3);
33 Arah := Arah-120;
34 TriadicKoch (Orde-1,Arah,Panjang/3);
35 Arah := Arah+60;
36 TriadicKoch (Orde-1,Arah,Panjang/3);
37 end
38 else
39 begin
40 dX := Round(Panjang*Cos(Arah/180*PI));

Grafika Komputer - Sept 2004 29


41 dY := Round(Panjang*Sin(Arah/180*PI));
42 LineRel (dX,dY);
43 end;
44 end;
45
46 procedure QuadricKoch (Orde:byte; Arah,Panjang:real);
47 begin
48 …
49 end;
50
51 procedure KurvaC (Orde:byte; Arah,Panjang:real);
52 begin
53 …
54 end;
55
56 procedure LayangLayang (Orde:byte; Arah,Panjang:real);
57 var dX,dY:integer;
58 begin
59 if Orde>0 then
60 begin
61 LayangLayang (Orde-1,Arah,Panjang/2);
62 Arah := Arah+45;
63 LayangLayang (Orde-1,Arah,Panjang/2);
64 Arah := Arah-90;
65 LayangLayang (Orde-1,Arah,Panjang/2);
66 Arah := Arah-90;
67 LayangLayang (Orde-1,Arah,Panjang/2);
68 Arah := Arah-90;
69 LayangLayang (Orde-1,Arah,Panjang/2);
70 Arah := Arah+45;
71 LayangLayang (Orde-1,Arah,Panjang/2);
72 end
73 else
74 begin
75 dX := Round(Panjang*Cos(Arah/180*PI));
76 dY := Round(Panjang*Sin(Arah/180*PI));
77 LineRel (dX,dY);
78 end;
79 end;
80
81 end.

Grafika Komputer - Sept 2004 30


Berikut ini contoh gambar fractal Triadic dan Quadric Koch untuk tiap orde:

Orde Triadic Koch Quadric Koch


0

Grafika Komputer - Sept 2004 31


Berikut ini contoh gambar fractal C dan Layang-Layang untuk tiap orde:

Orde Kurva C Layang-Layang


0

Pengembangan

Cobalah definisikan fractal buatan anda sendiri dan tambahkan ke program.

Grafika Komputer - Sept 2004 32


Bab
Fractal Bidang Kompleks
5
Tujuan
Menggambar berbagai macam fractal dalam bidang kompleks.

Latihan 5
Susun project untuk menggambar fractal bidang kompleks dengan struktur
menu sebagai berikut:
File
Open : menampilkan isi file bmp
Save : menyimpan gambar fractal ke file bmp
Draw
Mandelbrot : menggambar fractal Mandelbrot
Julia : menggambar fractal Julia
Untuk bisa menampilkan dan menyimpan gambar digunakan komponen Image
untuk menggantikan PaintBox.
Contoh tampilan jika dipilih Draw – Mandelbrot.

Grafika Komputer - Sept 2004 33


Contoh tampilan jika dipilih Draw – Julia.

Isi event sebagai berikut:


Unit1.PAS
01 type
02 TForm1 = class(TForm)
03 MainMenu1: TMainMenu;
04 MenuFile: TMenuItem;
05 MenuFileOpen: TMenuItem;
06 MenuFileSave: TMenuItem;
07 MenuDraw: TMenuItem;
08 MenuDrawMandelbrot: TMenuItem;
09 MenuDrawJulia: TMenuItem;
10 Image1: TImage;
11 OpenDialog1: TOpenDialog;
12 SaveDialog1: TSaveDialog;
13
14 procedure FormCreate(Sender: TObject);
15 procedure MenuFileOpenClick(Sender: TObject);
16 procedure MenuFileSaveClick(Sender: TObject);
17 procedure MenuDrawMandelbrotClick(Sender: TObject);
18 procedure MenuDrawJuliaClick(Sender: TObject);
19 end;
20 var
21 Form1 : TForm1;
22
23 implementation
24
25 {$R *.DFM}

Grafika Komputer - Sept 2004 34


26
27 uses
28 Palets, FrtCmp, Complex;
29
30 var
31 Parameter: TParCmpFrt;
32 Palet: TPalet;
33
34 procedure TForm1.FormCreate(Sender: TObject);
35 begin
36 Palet := TPalet.Create (64);
37 Parameter.C := TComplex.Create (0,0);
38 end;
39
40 procedure TForm1.MenuFileOpenClick(Sender:TObject);
41 begin
42 if OpenDialog1.Execute then
43 Image1.Picture.LoadFromFile (OpenDialog1.FileName);
44 end;
45
46 procedure TForm1.MenuFileSaveClick(Sender:TObject);
47 var FileName:string;
48 begin
49 if SaveDialog1.Execute then
50 begin
51 FileName := SaveDialog1.FileName;
52 if ExtractFileExt(FileName)='' then
53 FileName:=FileName+'.bmp';
54 Image1.Picture.SaveToFile (FileName);
55 end;
56 end;
57
58 procedure TForm1.MenuDrawMandelbrotClick(Sender:TObject);
59 begin
60 with Parameter do
61 begin
62 xMin := -2;
63 xMax := 1;
64 yMin := -1.5;
65 yMax := 1.5;
66 MaxIteration := 64;
67 MaxMagnitude := 4.0;
68 end;
69 MandelbrotDraw (Parameter, Palet, Image1);

Grafika Komputer - Sept 2004 35


70 end;
71
72 procedure TForm1.MenuDrawJuliaClick(Sender: TObject);
73 begin
74 with Parameter do
75 begin
76 xMin := -0.2;
77 xMax := 0.2;
78 yMin := 0.5;
79 yMax := 0.9;
80 MaxIteration := 64;
81 MaxMagnitude := 1000;
82 C.Init (0.6, 0.8);
83 end;
84 JuliaDraw (Parameter, Palet, Image1);
85 end;

Unit berikut berisi prosedur penggambar fractal bidang kompleks.


FrctCmp.PAS
01 unit FrtCmp;
02
03 interface
04
05 uses
06 Complex, Palets, ExtCtrls;
07
08 procedure MandelbrotDraw (Par:TParCmpFrT;
09 var Palet:TPalet; var Image:TImage);
10 procedure JuliaDraw (Par:TParCmpFrT;
11 var Palet:TPalet; var Image:TImage);
12
13 implementation
14
15 procedure MandelbrotDraw (Par:TParCmpFrT;
16 var Palet:TPalet; var Image:TImage);
17 var x,y:integer; dX,dY:real; Z:TComplex;
18 Color:byte;
19 Width,Height:integer;
20 begin
21 Width := Image.Width;
22 Height := Image.Height;

Grafika Komputer - Sept 2004 36


23 dX := (Par.XMax-Par.XMin) / Width;
24 dY := (Par.YMax-Par.YMin) / Height;
25 Z := TComplex.Create (0,0);
26 for x:=0 to Width-1 do
27 begin
28 for y:=0 to Height-1 do
29 with Par do
30 begin
31 Z.Init (0,0);
32 C.Init (XMin+x*dX, YMax-y*dY);
33 Color := 0;
34 while (Color<MaxIteration) and
35 (Z.Magnitude<MaxMagnitude) do
36 begin
37 Inc (Color);
38 Z.Mul (Z,Z);
39 Z.Add (Z,C);
40 end;
41 Image.Canvas.Pixels[x,y] := Palet[Color];
42 end;
43 Image.Repaint;
44 end;
45 end;
46
47 procedure JuliaDraw (Par:TParCmpFrT;
48 var Palet:TPalet; var Image:TImage);
49 var x,y:integer; dX,dY:real; Z:TComplex;
50 Color:byte;
51 Width,Height:integer;
52 begin
53 Width := Image.Width;
54 Height := Image.Height;
55 dX := (Par.XMax-Par.XMin) / Width;
56 dY := (Par.YMax-Par.YMin) / Height;
57 Z := TComplex.Create (0,0);
58 for x:=0 to Width-1 do
59 begin
60 for y:=0 to Height-1 do
61 with Par do
62 begin
63 Z.Init (XMin+x*dX, YMax-y*dY);
64 Color := 0;
65 while (Color<MaxIteration) and
66 (Z.Magnitude<MaxMagnitude) do

Grafika Komputer - Sept 2004 37


67 begin
68 Inc (Color);
69 Z.Mul (Z,Z);
70 Z.Add (Z,C);
71 end;
72 Image.Canvas.Pixels[x,y] := Palet[Color];
73 end;
74 Image.Repaint;
75 end;
76 end;
77
78 begin
79 end.

Unit berikut mengatur palet warna yang dipakai dalam menggambar fractal.
Palet.PAS
01 unit Palets;
02
03 interface
04
05 uses
06 Graphics, Complex;
07
08 type
09
10 { parameters of complex fractal }
11 TParCmpFrt = record
12 xMin, xMax, yMin, yMax : real;
13 MaxIteration : byte;
14 MaxMagnitude : real;
15 C : TComplex;
16 end;
17
18 TRecPalet = record
19 Red,
20 Green,
21 Blue : byte;
22 dR, dG, dB : integer;
23 end;
24
25 TPalet = class
26 private
27 DataPalet : array [0..255] of TColor;

Grafika Komputer - Sept 2004 38


28 Palet : TRecPalet;
29 function GetColor (idx:byte) : TColor;
30 public
31 constructor Create (MaxColor:byte);
32 procedure Init (MaxColor:byte; SP:TRecPalet);
33 procedure InitDefault (MaxColor:byte);
34 property Color [idx:byte]:TColor read
35 GetColor; default;
36 end;
37
38 IMPLEMENTATION
39
40 constructor TPalet.Create (MaxColor:byte);
41 begin
42 InitDefault (MaxColor);
43 end;
44
45 procedure TPalet.Init (MaxColor:byte;
46 SP:TRecPalet);
47 var i,R,G,B : byte;
48 begin
49 R:=SP.Red; G:=SP.Green; B:=SP.Blue;
50 for i:=0 to MaxColor-1 do
51 begin
52 DataPalet[i] := R + 256*G + 256*256*B;
53 R:=R+SP.dR; G:=G+SP.dG; B:=B+SP.dB;
54 end;
55 DataPalet[MaxColor] := 0;
56 end;
57
58 procedure TPalet.InitDefault (MaxColor:byte);
59 var SP:TRecPalet;
60 begin
61 SP.Red:=0;
62 SP.Green:=200;
63 SP.Blue:=100;
64 SP.dR:=30;
65 SP.dG:=-30;
66 SP.dB:=50;
67 Init (MaxColor, SP);
68 end;
69
70 function TPalet.GetColor (idx:byte) : TColor;
71 begin

Grafika Komputer - Sept 2004 39


72 Result := DataPalet[idx];
73 end;
74
75 begin
76 end.

Unit berikut berisi implementasi operasi bilangan kompleks.


Complex.PAS
01 unit Complex;
02
03 INTERFACE
04
05 type
06
07 TComplex = class
08 private
09 re,im : real;
10 public
11 constructor Create (r,i:real);
12 destructor Destroy;
13
14 procedure Init (r,i:real);
15 function Magnitude : real;
16 procedure Con;
17 procedure Add (A,B:TComplex);
18 procedure Sub (A,B:TComplex);
19 procedure Mul (A,B:TComplex);
20 procedure Divi (A,B:TComplex);
21 end;
22
23 IMPLEMENTATION
24
25 constructor TComplex.Create (r,i:real);
26 begin
27 Init (r,i);
28 end;
29
30 destructor TComplex.Destroy;
31 begin
32 end;
33
34 procedure TComplex.Init (r,i:real);
35 begin

Grafika Komputer - Sept 2004 40


36 re:=r; im:=i;
37 end;
38
39 function TComplex.Magnitude : real;
40 begin
41 Result := re*re + im*im;
42 end;
43
44 procedure TComplex.Con;
45 begin
46 im := -im;
47 end;
48
49 procedure TComplex.Add (A,B:TComplex);
50 var _re,_im:real;
51 begin
52 _re := A.re + B.re;
53 _im := A.im + B.im;
54 Init (_re,_im);
55 end;
56
57 procedure TComplex.Sub (A,B:TComplex);
58 var _re,_im:real;
59 begin
60 _re := A.re - B.re;
61 _im := A.im - B.im;
62 Init (_re,_im);
63 end;
64
65 procedure TComplex.Mul (A,B:TComplex);
66 var _re,_im:real;
67 begin
68 _re := (A.re * B.re) - (A.im * B.im);
69 _im := (A.re * B.im) + (A.im * B.re);
70 Init (_re,_im);
71 end;
72
73 procedure TComplex.Divi (A,B:TComplex);
74 var
75 Bc, C : TComplex;
76 sq : real;
77 begin
78 Bc := B; BC.Con;
79 C.Mul (A,Bc);

Grafika Komputer - Sept 2004 41


80 sq := Sqr(B.re) + Sqr(B.im);
81 re := C.re / sq;
82 im := C.im / sq;
83 end;
84
85 begin
86 end.

Pengembangan
Lakukan modifikasi sebagai berikut:
- Tambahkan menu untuk mengatur parameter penggambaran fractal dalam
record TParCmpFrt.

- Tambahkan menu untuk mengatur palet warna.

Grafika Komputer - Sept 2004 42


Bab
Obyek 2D
6
Tujuan
Melakukan transformasi obyek dua dimensi.

Latihan 6
Susun project untuk trasformasi obyek 2D dengan tampilan sebagai berikut:

ButtonLoad dan ButtonSave di atas digunakan untuk mengambil dan


meyimpan data koordinat obyek 2D bukan gambarnya.

Grafika Komputer - Sept 2004 43


Isi event sebagai berikut:
Unit1.PAS
01 type
02 TForm1 = class(TForm)
03 PaintBox1: TPaintBox;
04 OpenDialog1: TOpenDialog;
05 SaveDialog1: TSaveDialog;
06
07 BitBtnLoad: TBitBtn;
08 BitBtnSave: TBitBtn;
09 BitBtnClear: TBitBtn;
10 BitBtnClose: TBitBtn;
11
12 GroupBox1: TGroupBox;
13 GroupBox2: TGroupBox;
14 GroupBox3: TGroupBox;
15 GroupBox4: TGroupBox;
16 GroupBox5: TGroupBox;
17
18 CheckBoxCenter: TCheckBox;
19
20 EditScalingDX: TEdit;
21 EditScalingDY: TEdit;
22 EditAngle: TEdit;
23 EditShearingdX: TEdit;
24 EditShearingdY: TEdit;
25
26 ButtonViewCenter: TButton;
27 ButtonScaling: TButton;
28 ButtonRotation: TButton;
29 ButtonReflectionX: TButton;
30 ButtonReflectionY: TButton;
31 ButtonShearing: TButton;
32
33 Label1: TLabel;
34 Label2: TLabel;
35 Label3: TLabel;
36 Label4: TLabel;
37 Label5: TLabel;
38 procedure FormCreate(Sender: TObject);
39 procedure BitBtnLoadClick(Sender: TObject);
40 procedure BitBtnSaveClick(Sender: TObject);
41 procedure BitBtnClearClick(Sender: TObject);

Grafika Komputer - Sept 2004 44


42
43 procedure ButtonViewCenterClick(Sender: TObject);
44 procedure ButtonScalingClick(Sender: TObject);
45 procedure ButtonRotationClick(Sender: TObject);
46 procedure ButtonReflectionXClick(Sender: TObject);
47 procedure ButtonReflectionYClick(Sender: TObject);
48 procedure ButtonShearingClick(Sender: TObject);
49 end;
50
51 var
52 Form1: TForm1;
53
54 implementation
55
56 {$R *.DFM}
57
58 uses
59 OB2D;
60
61 var
62 Object2D : TObject2D;
63
64 procedure TForm1.FormCreate(Sender: TObject);
65 begin
66 Object2D := TObject2D.Create;
67 end;
68
69 procedure TForm1.BitBtnLoadClick(Sender: TObject);
70 begin
71 if OpenDialog1.Execute then
72 begin
73 Object2D.Load (OpenDialog1.FileName);
74 Caption := 'Obyek 2 Dimensi - ' +
75 ExtractFileName(OpenDialog1.FileName);
76 end;
77 Object2D.Draw(CheckBoxCenter.Checked,PaintBox1);
78 end;
79
80 procedure TForm1.BitBtnSaveClick(Sender: TObject);
81 begin
82 if SaveDialog1.Execute then
83 Object2D.Save (SaveDialog1.FileName);
84 end;
85

Grafika Komputer - Sept 2004 45


86 procedure TForm1.BitBtnClearClick(Sender: TObject);
87 begin
88 PaintBox1.Refresh;
89 end;
90
91 procedure TForm1.ButtonViewCenterClick(Sender:TObject);
92 begin
93 Object2D.Draw (true, PaintBox1);
94 end;
95
96 procedure TForm1.ButtonScalingClick(Sender:TObject);
97 begin
98 Object2D.Scaling (
99 StrToFloat(EditScalingDX.text),
100 StrToFloat(EditScalingDY.text));
101 Object2D.Draw (CheckBoxCenter.Checked, PaintBox1);
102 end;
103
104 procedure TForm1.ButtonRotationClick(Sender: TObject);
105 begin
106 Object2D.Rotation (StrToFloat(EditAngle.text));
107 Object2D.Draw (CheckBoxCenter.Checked, PaintBox1);
108 end;
109
110 procedure TForm1.ButtonReflectionXClick(Sender:TObject);
111 begin
112 Object2D.Reflection (true,false);
113 Object2D.Draw (CheckBoxCenter.Checked, PaintBox1);
114 end;
115
116 procedure TForm1.ButtonReflectionYClick(Sender:TObject);
117 begin
118 Object2D.Reflection (false,true);
119 Object2D.Draw (CheckBoxCenter.Checked, PaintBox1);
120 end;
121
122 procedure TForm1.ButtonShearingClick(Sender: TObject);
123 var fx,fy:real;
124 begin
125 fx := StrToFloat (EditShearingDX.text);
126 fy := StrToFloat (EditShearingDY.text);
127 Object2D.Shearing (fx, fy);
128 Object2D.Draw (CheckBoxCenter.Checked, PaintBox1);
129 end;

Grafika Komputer - Sept 2004 46


Unit berikut berisi struktur data beserta operasinya dari obyek 2D.
OB2D.PAS
01 unit OB2D;
02
03 interface
04
05 uses ExtCtrls;
06
07 const
08 MAX_VERTEX = 30;
09
10 type
11 TVertex = record
12 x,y : real;
13 end;
14
15 TIndex = 0..MAX_VERTEX;
16
17 TObject2D = class
18 private
19 nVertexs : byte;
20 Vertexs : array [1..MAX_VERTEX] of TVertex;
21 public
22 constructor Create;
23 destructor Destroy;
24
25 procedure Draw (Centered:boolean;
26 var PB:TPaintBox);
27 procedure Load (FileName:string);
28 procedure Save (FileName:string);
29 procedure Translation (dx,dy:real);
30 procedure Scaling (dx,dy:real);
31 procedure Rotation (t:real);
32 procedure Shearing (dx,dy:real);
33 procedure Reflection (rx,ry:boolean);
34 procedure Center (x1,y1,x2,y2:real);
35 end;
36
37 implementation
38
39 constructor TObject2D.Create;
40 begin
41 nVertexs := 0;

Grafika Komputer - Sept 2004 47


42 end;
43
44 destructor TObject2D.Destroy;
45 begin
46 end;
47
48 procedure TObject2D.Draw (Centered:boolean;
49 var PB:TPaintBox);
50 var i:TIndex;
51 begin
52 PB.Refresh;
53 if Centered then
54 Center (0,0,PB.Width-1,PB.Height-1);
55 i := nVertexs;
56 with Vertexs[i] do
57 PB.Canvas.MoveTo (Round(x), Round(y));
58 for i:=1 to nVertexs do
59 with Vertexs[i] do
60 PB.Canvas.LineTo (Round(x), Round(y));
61 end;
62
63 procedure TObject2D.Load (FileName:string);
64 var i:TIndex; F:TextFile;
65 begin
66 AssignFile (F,FileName);
67 Reset(F);
68 ReadLn (F, nVertexs);
69 for i:=1 to nVertexs do
70 ReadLn(F, Vertexs[i].x, Vertexs[i].y);
71 CloseFile (F);
72 end;
73
74 procedure TObject2D.Save (FileName:string);
75 var i:TIndex; F:TextFile;
76 begin
77 AssignFile (F,FileName);
78 ReWrite (F);
79 WriteLn(F, nVertexs);
80 for i:=1 to nVertexs do
81 WriteLn(F, Vertexs[i].x,' ',Vertexs[i].y);
82 CloseFile (F);
83 end;
84
85 procedure TObject2D.Translation (dx,dy:real);

Grafika Komputer - Sept 2004 48


86 var i:TIndex;
87 begin
88 for i:=1 to nVertexs do
89 begin
90 Vertexs[i].x := Vertexs[i].x + dx;
91 Vertexs[i].y := Vertexs[i].y + dy;
92 end;
93 end;
94
95 procedure TObject2D.Scaling (dx,dy:real);
96 var i:TIndex;
97 begin
98 for i:=1 to nVertexs do
99 begin
100 Vertexs[i].x := Vertexs[i].x * dx;
101 Vertexs[i].y := Vertexs[i].y * dy;
102 end;
103 end;
104
105 procedure TObject2D.Rotation (t:real);
106 var i:TIndex; x,y:real;
107 begin
108 t := t/180*PI;
109 for i:=1 to nVertexs do
110 begin
111 x := Vertexs[i].x;
112 y := Vertexs[i].y;
113 Vertexs[i].x := x * Cos(t) - y * Sin(t);
114 Vertexs[i].y := x * Sin(t) + y * Cos(t);
115 end;
116 end;
117
118 procedure TObject2D.Shearing (dx,dy:real);
119 var i:TIndex;
120 begin
121 for i:=1 to nVertexs do
122 Vertexs[i].x := Vertexs[i].x +
123 Vertexs[i].y * dx;
124 for i:=1 to nVertexs do
125 Vertexs[i].y := Vertexs[i].y +
126 Vertexs[i].x * dy;
127 end;
128
129 procedure TObject2D.Reflection (rx,ry:boolean);

Grafika Komputer - Sept 2004 49


130 var i:TIndex;
131 begin
132 if rx then
133 for i:=1 to nVertexs do
134 Vertexs[i].y := -Vertexs[i].y;
135 if ry then
136 for i:=1 to nVertexs do
137 Vertexs[i].x := -Vertexs[i].x;
138 end;
139
140 procedure TObject2D.Center (x1,y1,x2,y2:real);
141 var i:TIndex; MinX,MinY,MaxX,MaxY,dx,dy:real;
142 begin
143 MinX := Vertexs[1].x;
144 MinY := Vertexs[1].y;
145 MaxX := Vertexs[1].x;
146 MaxY := Vertexs[1].y;
147 for i:=2 to nVertexs do
148 begin
149 if Vertexs[i].x<MinX then MinX:=Vertexs[i].x;
150 if Vertexs[i].y<MinY then MinY:=Vertexs[i].y;
151 if Vertexs[i].x>MaxX then MaxX:=Vertexs[i].x;
152 if Vertexs[i].y>MaxY then MaxY:=Vertexs[i].y;
153 end;
154 dx := ((x2-x1) - (MaxX-MinX)) / 2 - MinX;
155 dy := ((y2-y1) - (MaxY-MinY)) / 2 - MinY;
156 Translation (dx,dy);
157 end;
158
159 begin
160 end.

Berikut ini contoh struktur obyek 2D yang disimpan dalam file dengan ekstensi
2D. Baris pertama menunjukkan banyaknya titik/vertek dan baris berikutnya
menunjukan pasangan nilai x dan y.
5
25 10
10 30
10 60
40 60
40 30
Grafika Komputer - Sept 2004 50
Pengembangan
- Tambahkan fasilitas untuk mengatur warna dan ketebalan garis untuk
mengambar obyek,
- Tambahkan fasilitas untuk menyimpan obyek dalam bentuk file bmp.

Grafika Komputer - Sept 2004 51


Bab
Obyek 3D
7
Tujuan
Melakukan transformasi obyek tiga dimensi.

Latihan 7
Susun project untuk trasformasi obyek 3D dengan tampilan sebagai berikut:

Isi event sebagai berikut:


Unit1.PAS
01 type
02 TForm1 = class(TForm)
03 Image1: TImage;
04 OpenDialog1: TOpenDialog;
05 SaveDialog1: TSaveDialog;
06 GroupBox1: TGroupBox;
07 GroupBox2: TGroupBox;

Grafika Komputer - Sept 2004 52


08
09 BitBtnClose: TBitBtn;
10 BitBtnLoad: TBitBtn;
11 BitBtnSave: TBitBtn;
12 BitBtnClear: TBitBtn;
13
14 ButtonRotateX: TButton;
15 ButtonRotateY: TButton;
16 ButtonRotateZ: TButton;
17 ButtonResize: TButton;
18
19 EditAngle: TEdit;
20 EditResize: TEdit;
21
22 Label3: TLabel;
23 procedure FormCreate(Sender: TObject);
24 procedure BitBtnLoadClick(Sender: TObject);
25 procedure BitBtnSaveClick(Sender: TObject);
26 procedure BitBtnClearClick(Sender: TObject);
27 procedure ButtonRotateClick(Sender: TObject);
28 procedure ButtonResizeClick(Sender: TObject);
29 end;
30
31 var
32 Form1: TForm1;
33
34 implementation
35
36 {$R *.DFM}
37
38 uses
39 OB3D;
40
41 var
42 Object3D : TObject3D;
43
44 procedure TForm1.FormCreate(Sender: TObject);
45 begin
46 Image1.Canvas.Rectangle (0,0,
47 Image1.Width,Image1.Height);
48 Object3D := TObject3D.Create;
49 end;
50
51 procedure TForm1.BitBtnLoadClick(Sender: TObject);

Grafika Komputer - Sept 2004 53


52 begin
53 if OpenDialog1.Execute then
54 begin
55 Object3D.Load (OpenDialog1.FileName);
56 Object3D.DrawOnXY (Image1,0,0,
57 Image1.Width,Image1.Height);
58 end;
59 end;
60
61 procedure TForm1.BitBtnSaveClick(Sender: TObject);
62 begin
63 if SaveDialog1.Execute then
64 Object3D.Save (SaveDialog1.FileName);
65 end;
66
67 procedure TForm1.BitBtnClearClick(Sender: TObject);
68 begin
69 Image1.Canvas.Rectangle (0,0,
70 Image1.Width,Image1.Height);
71 end;
72
73 procedure TForm1.ButtonRotateClick(Sender: TObject);
74 var a:real;
75 begin
76 a := StrToFloat(EditAngle.text);
77 case (Sender as TButton).Tag of
78 1 : Object3D.RotateX (a);
79 2 : Object3D.RotateY (a);
80 3 : Object3D.RotateZ (a);
81 end;
82 Image1.Canvas.Rectangle (0,0,
83 Image1.Width,Image1.Height);
84 Object3D.DrawOnXY (Image1,0,0,
85 Image1.Width,Image1.Height);
86 end;
87
88 procedure TForm1.ButtonResizeClick(Sender: TObject);
89 begin
90 Object3D.Resize (StrToFloat(EditResize.text));
91 Image1.Canvas.Rectangle (0,0,
92 Image1.Width,Image1.Height);
93 Object3D.DrawOnXY (Image1,0,0,
94 Image1.Width,Image1.Height);
95 end;

Grafika Komputer - Sept 2004 54


Unit berikut berisi struktur data beserta operasinya dari obyek 3D. Lengkapi
procedure Save.
OB3D.PAS
01 unit OB3D;
02
03 interface
04
05 uses
06 ExtCtrls;
07
08 const
09 MAX_VERTEX = 30;
10 MAX_FACE = 20;
11 MAX_VERTEX_PER_FACE = 20;
12
13 type
14 TVertex = record
15 x,y,z : real;
16 end;
17
18 TObject3D = class
19 private
20 nVertexs,
21 nFaces : byte;
22 Vertex : array [1..MAX_VERTEX] of TVertex;
23 Face : array [1..MAX_FACE,
24 1..MAX_VERTEX_PER_FACE] of byte;
25 nVertexFace : array [1..MAX_FACE] of byte;
26
27 public
28 constructor Create;
29 destructor Destroy;
30
31 procedure Load (FileName:string);
32 procedure Save (FileName:string);
33 procedure Normal (var Lx,Ly,Lz:real);
34 procedure DrawOnXY (var Image:TImage;
35 x1,y1,x2,y2:integer);
36 procedure RotateX (T:real);
37 procedure RotateY (T:real);
38 procedure RotateZ (T:real);
39 procedure ReSize (S:real);
40 end;

Grafika Komputer - Sept 2004 55


41
42 IMPLEMENTATION
43
44 constructor TObject3D.Create;
45 begin
46 nVertexs := 0;
47 nFaces := 0;
48 end;
49
50 destructor TObject3D.Destroy;
51 begin
52 end;
53
54 procedure TObject3D.Load (FileName:string);
55 var FL:TextFile; i,j:byte;
56 begin
57 AssignFile(FL,FileName); Reset(FL);
58 ReadLn(FL, nVertexs, nFaces);
59 ReadLn(FL);
60 for i:=1 to nVertexs do
61 with Vertex[i] do
62 ReadLn(FL, x,y,z);
63 ReadLn(FL);
64 for i:=1 to nFaces do
65 begin
66 Read(FL,nVertexFace[i]);
67 for j:=1 to nVertexFace[i] do
68 Read(FL, Face[i,j]);
69 Face[i,nVertexFace[i]+1] := Face[i,1];
70 ReadLn(FL);
71 end;
72 CloseFile(FL);
73 end;
74
75 procedure TObject3D.Save (FileName:string);
76 begin
77 …
78 …
79 end;
80
81 procedure TObject3D.Normal (var Lx,Ly,Lz:real);
82 var i:byte; MinX,MinY,MinZ:real;
83 begin
84 MinX := Vertex[1].x;

Grafika Komputer - Sept 2004 56


85 MinY := Vertex[1].y;
86 MinZ := Vertex[1].z;
87 for i:=2 to nVertexs do
88 begin
89 if Vertex[i].x < MinX then
90 MinX := Vertex[i].x;
91 if Vertex[i].y < MinY then
92 MinY := Vertex[i].y;
93 if Vertex[i].z < MinZ then
94 MinZ := Vertex[i].z;
95 end;
96 Lx:=0; Ly:=0; Lz:=0;
97 for i:=1 to nVertexs do
98 begin
99 Vertex[i].x := Vertex[i].x - MinX;
100 Vertex[i].y := Vertex[i].y - MinY;
101 Vertex[i].z := Vertex[i].z - MinZ;
102 if Vertex[i].x > Lx then Lx := Vertex[i].x;
103 if Vertex[i].y > Ly then Ly := Vertex[i].y;
104 if Vertex[i].z > Lz then Lz := Vertex[i].z;
105 end;
106 end;
107
108 procedure TObject3D.DrawOnXY (var Image:TImage;
109 x1,y1,x2,y2:integer);
110 var i,j:byte; xa,xb,ya,yb:integer; Lx,Ly,Lz:real;
111 MarginX,MarginY:integer;
112 begin
113 Normal (Lx,Ly,Lz);
114 MarginX := Round((x2-x1+1)-Lx) div 2;
115 MarginY := Round((y2-y1+1)-Ly) div 2;
116 for i:=1 to nFaces do
117 for j:=1 to nVertexFace[i] do
118 begin
119 xa := Round (Vertex[Face[i,j]].x) +
120 MarginX;
121 xb := Round (Vertex[Face[i,j+1]].x) +
122 MarginX;
123 ya := Round (Vertex[Face[i,j]].y) +
124 MarginY;
125 yb := Round (Vertex[Face[i,j+1]].y) +
126 MarginY;
127 Image.Canvas.MoveTo (xa,ya);
128 Image.Canvas.LineTo (xb,yb);

Grafika Komputer - Sept 2004 57


129 end;
130 end;
131
132 procedure TObject3D.RotateX (T:real);
133 var i:byte; yy,zz:real;
134 begin
135 T := T/180*PI;
136 for i:=1 to nVertexs do
137 with Vertex[i] do
138 begin
139 yy := y*Cos(T) + z*Sin(T);
140 zz := -y*Sin(T) + z*Cos(T);
141 y:=yy; z:=zz;
142 end;
143 end;
144
145 procedure TObject3D.RotateY (T:real);
146 var i:byte; xx,zz:real;
147 begin
148 T := T/180*PI;
149 for i:=1 to nVertexs do
150 with Vertex[i] do
151 begin
152 xx := x*Cos(T) + z*Sin(T);
153 zz := -x*Sin(T) + z*Cos(T);
154 x:=xx; z:=zz;
155 end;
156 end;
157
158 procedure TObject3D.RotateZ (T:real);
159 var i:byte; xx,yy:real;
160 begin
161 T := T/180*PI;
162 for i:=1 to nVertexs do
163 with Vertex[i] do
164 begin
165 xx := x*Cos(T) + y*Sin(T);
166 yy := -x*Sin(T) + y*Cos(T);
167 x:=xx; y:=yy;
168 end;
169 end;
170
171 procedure TObject3D.ReSize (S:real);
172 var i:byte;

Grafika Komputer - Sept 2004 58


173 begin
174 for i:=1 to nVertexs do
175 begin
176 Vertex[i].x := Vertex[i].x * S;
177 Vertex[i].y := Vertex[i].y * S;
178 Vertex[i].z := Vertex[i].z * S;
179 end;
180 end;
181
182 end.

Berikut ini contoh struktur obyek 3D yang disimpan dalam file dengan ekstensi
3D. Baris pertama menunjukkan banyaknya vertek dan face. Pada bagian
VERTEX terdapat pasangan nilai x, y dan z. Pada bagian FACE kolom pertama
menunjukkan banyaknya vertex yang menyusun face dan kolom berikutnya
menunjukan nomer vertex penyusun face tersebut.
5 5
VERTEX
0 0 0
0 50 0
50 50 0
50 0 0
25 25 40
FACE
4 1 2 3 4
3 1 2 5
3 2 3 5
3 3 4 5
3 4 1 5

Pengembangan
- Tambahkan fasilitas untuk mengatur warna dan ketebalan garis untuk
mengambar obyek,
- Tambahkan fasilitas untuk menyimpan obyek dalam bentuk file bmp.
- Tambahkan fasilitas transformasi lainnya seperti pada program untuk 2D.

Grafika Komputer - Sept 2004 59


Bab
Dasar-Dasar Pengolahan Citra Digital
8
Tujuan
Melakukan pengolahan citra digital.

Latihan 8
Susun project untuk mengolah citra digital (format bmp) dengan tampilan
utama (FormMain) sebagai berikut:

BitBtnUndo

Panel1

FormImg
Panel2

Gauge1

Pengaturan komponen dalam FormMain sebagai berikut:


Component Property Value
Form • Name • FormMain
• Caption • MyImage 1.0
• Color • clWhite
• FormStyle • fsMDIForm
Panel • Name • Panel1
Grafika Komputer - Sept 2004 60
• Align • alTop
• Caption • ‘’ (kosong)
Panel • Name • Panel2
• Align • alBottom
• Caption • ‘’ (kosong)
BitBtn • Name • BitBtnUndo
• Glyph •
Gauge • Name • Gauge1
• Align • alRight
Dalam FormMain terdapat form anak (FormImg) dengan pengaturan
komponen sebagai berikut:
Component Property Value
Form • Name • FormImg
• FormStyle • fsMDIChild
Image • Name • Image1
Dalam FormMain terdapat juga komponen non visual OpenPictureDialog,
SavePictureDialog dan MainMenu. Struktur menu program dalam
MainMenu sebagai berikut:
File
Open Image
Save Image As
Image
Convert to Grayscale
Invert Color
Lightening
Exit

Isi event dalam FormMain dan dan FormImg sebagai berikut:


F_Main.PAS
01 type
02 TFormMain = class(TForm)
03 MainMenu1: TMainMenu;
04 MenuFile: TMenuItem;
05 MenuFileOpen: TMenuItem;
06 MenuFileSave: TMenuItem;
07 MenuImage: TMenuItem;
08 MenuImageGrayscale: TMenuItem;
Grafika Komputer - Sept 2004 61
09 MenuImageInvertColor: TMenuItem;
10 MenuImageLighten: TMenuItem;
11 MenuExit: TMenuItem;
12
13 Panel1: TPanel;
14 Panel2: TPanel;
15 ImageBuffer: TImage;
16 Gauge1: TGauge;
17 BitBtnUndo: TBitBtn;
18 OpenPictureDialog1: TOpenPictureDialog;
19 SavePictureDialog1: TSavePictureDialog;
20 procedure FormShow(Sender: TObject);
21 procedure MenuFileOpenClick(Sender: TObject);
22 procedure MenuFileSaveClick(Sender: TObject);
23 procedure MenuExitClick(Sender: TObject);
24 procedure MenuImageClick(Sender: TObject);
25 procedure BitBtnUndoClick(Sender: TObject);
26 end;
27
28 var
29 FormMain: TFormMain;
30
31 implementation
32
33 {$R *.DFM}
34
35 uses
36 F_Img, F_Light, ImgProc;
37
38 procedure TFormMain.FormShow(Sender: TObject);
39 var s:string;
40 begin
41 FormImg.Left := 2;
42 FormImg.Top := 2;
43 GetDir (0, s);
44 OpenPictureDialog1.InitialDir := s;
45 SavePictureDialog1.InitialDir := s;
46 end;
47
48 procedure TFormMain.MenuExitClick(Sender:TObject);
49 begin
50 Close;
51 end;
52

Grafika Komputer - Sept 2004 62


53 procedure TFormMain.MenuFileOpenClick(Sender: TObject);
54 begin
55 if OpenPictureDialog1.Execute then
56 begin
57 FormImg.OpenImage(OpenPictureDialog1.FileName);
58 MenuFileSave.Enabled := true;
59 MenuImage.Enabled := true;
60 end;
61 end;
62
63 procedure TFormMain.MenuFileSaveClick(Sender:TObject);
64 begin
65 if SavePictureDialog1.Execute then
66 begin
67 FormImg.Image1.Picture.SaveToFile
68 (SavePictureDialog1.FileName);
69 FormImg.Caption := ExtractFileName
70 (SavePictureDialog1.FileName);
71 end;
72 end;
73
74 procedure TFormMain.MenuImageClick(Sender:TObject);
75 begin
76 ImageBuffer.Picture.Graphic :=
77 FormImg.Image1.Picture.Graphic;
78 FormImg.Image1.Canvas.Pen.Mode := pmCOPY;
79 if Sender=MenuImageGrayscale then
80 ImgConvertToGrayscale (FormImg.Image1, Gauge1)
81 else if Sender=MenuImageInvertColor then
82 ImgInvertColor (FormImg.Image1, Gauge1)
83 else if Sender=MenuImageLighten then
84 FormLighten.ShowModal;
85 end;
86
87 procedure TFormMain.BitBtnUndoClick(Sender:TObject);
88 begin
89 FormImg.Image1.Picture.Graphic :=
90 ImageBuffer.Picture.Graphic;
91 end;

Grafika Komputer - Sept 2004 63


F_Img.PAS
01 type
02 TFormImg = class(TForm)
03 Image1: TImage;
04 public
05 procedure OpenImage (FileName: string);
06 end;
07
08 var
09 FormImg: TFormImg;
10
11 implementation
12
13 uses F_main,ImgProc;
14
15 {$R *.DFM}
16
17 procedure TFormImg.OpenImage (FileName: string);
18 begin
19 Caption := ExtractFileName (FileName);
20 Image1.Picture.LoadFromFile (FileName);
21 WindowState := wsNormal;
22 ClientWidth := Image1.Width;
23 ClientHeight := Image1.Height;
24 end;
Menu Convert to Grayscale dan Invert Color menghasilkan citra
berikut:

Convert Grayscale Invert Color

Grafika Komputer - Sept 2004 64


Menu Lightening menampilkan FormLighten sebagai berikut:

SpinEditUpDown

ButtonGoLD

EditBrightnessFac
tor

ButtonGoBrightnes
s

Isi event dalam FormLighten sebagai berikut:


F_Light.PAS
01 type
02 TFormLighten = class(TForm)
03 GroupBox1: TGroupBox;
04 GroupBox2: TGroupBox;
05 GroupBox3: TGroupBox;
06 RadioButtonBrightMul: TRadioButton;
07 RadioButtonBrightPow: TRadioButton;
08
09 SpinEditUpDown: TSpinEdit;
10 EditBrightnessFactor: TEdit;
11
12 ButtonGoLD: TButton;
13 ButtonGoBrightness: TButton;
14 BitBtnClose: TBitBtn;
15 BitBtnUndo: TBitBtn;
16
17 CheckBoxR: TCheckBox;
18 CheckBoxG: TCheckBox;
19 CheckBoxB: TCheckBox;
20 Label1: TLabel;
21 Label2: TLabel;
22 procedure ButtonGoLDClick(Sender: TObject);
23 procedure ButtonGoBrightnessClick(Sender:TObject);

Grafika Komputer - Sept 2004 65


24 procedure BitBtnUndoClick(Sender: TObject);
25 end;
26
27 var
28 FormLighten: TFormLighten;
29
30 implementation
31
32 uses F_Img, F_Main, ImgProc;
33
34 {$R *.DFM}
35
36 procedure TFormLighten.ButtonGoLDClick(Sender:TObject);
37 var r,g,b: integer;
38 begin
39 r:=0; g:=0; b:=0;
40 if CheckBoxR.Checked then
41 r := SpinEditUpDown.Value;
42 if CheckBoxG.Checked then
43 g := SpinEditUpDown.Value;
44 if CheckBoxB.Checked then
45 b := SpinEditUpDown.Value;
46
47 ImgAdd (FormImg.Image1, r,g,b, FormMain.Gauge1);
48 end;
49
50 procedure TFormLighten.ButtonGoBrightnessClick
51 (Sender:TObject);
52 var r,g,b: real;
53 begin
54 r:=1; g:=1; b:=1;
55 if CheckBoxR.Checked then
56 r := StrToFloat(EditBrightnessFactor.Text);
57 if CheckBoxG.Checked then
58 g := StrToFloat(EditBrightnessFactor.Text);
59 if CheckBoxB.Checked then
60 b := StrToFloat(EditBrightnessFactor.Text);
61
62 if RadioButtonBrightMul.Checked then
63 ImgMul (FormImg.Image1, r,g,b, FormMain.Gauge1)
64 else
65 ImgPow (FormImg.Image1, r,g,b, FormMain.Gauge1)
66 end;
67

Grafika Komputer - Sept 2004 66


68 procedure TFormLighten.BitBtnUndoClick(Sender:TObject);
69 begin
70 FormImg.Image1.Picture.Graphic :=
71 FormMain.ImageBuffer.Picture.Graphic;
72 end;

Beberapa citra hasil proses dalam FormLighten adalah sebagai berikut:

Lighten -50 Lighten 50

Brightness Multiply 1.5 Brightness Power 1.3

Semua procedure pengolahan citra diletakkan dalam unit ImgProc.


ImgProc.PAS
01 unit ImgProc;
02
03 interface
04
05 uses
06 WINPROCS, Graphics, ExtCtrls, Gauges;

Grafika Komputer - Sept 2004 67


07
08 function RgbToGray (Clr:TColor) : byte;
09 function ByteRange (r:double) : byte;
10 procedure ImgConvertToGrayscale (
11 var Image1:TImage; var Gauge:TGauge);
12 procedure ImgInvertColor (var Image1:TImage;
13 var Gauge:TGauge);
14
15 procedure ImgAdd (var Image1:TImage;
16 dr,dg,db:integer; var Gauge:TGauge);
17 procedure ImgMul (var Image1:TImage;
18 mr,mg,mb:real; var Gauge:TGauge);
19 procedure ImgPow (var Image1:TImage;
20 pr,pg,pb:real; var Gauge:TGauge);
21
22 implementation
23
24 const
25 PercentR = 0.299;
26 PercentG = 0.587;
27 PercentB = 0.114;
28
29 function ByteRange (r:double) : byte;
30 begin
31 if r<0 then ByteRange:=0
32 else if r>255 then ByteRange:=255
33 else ByteRange:=Round(r);
34 end;
35
36 function Pow (x,n:double) : double;
37 begin
38 if x=0 then
39 Pow := 0
40 else
41 Pow := Exp (n*Ln(x));
42 end;
43
44 procedure GaugeStart (var Gauge:TGauge; n:longint);
45 begin
46 Gauge.MaxValue := n;
47 Gauge.Progress := 0;
48 end;
49
50 procedure GaugeStop (var Gauge:TGauge);

Grafika Komputer - Sept 2004 68


51 begin
52 Gauge.Progress := Gauge.MaxValue;
53 end;
54
55 procedure GaugeProgress (var Gauge:TGauge;
56 var Image1:TImage; n:longint);
57 begin
58 if n mod 20 = 0 then
59 begin
60 Image1.Repaint;
61 Gauge.Progress := n;
62 end;
63 end;
64
65 function RgbToGray (Clr:TColor) : byte;
66 var r,g,b:byte;
67 begin
68 r := GetRValue(Clr);
69 g := GetGValue(Clr);
70 b := GetBValue(Clr);
71 RgbToGray := ByteRange (r*PercentR +
72 g*PercentG + b*PercentB);
73 end;
74
75 procedure ImgConvertToGrayscale (
76 var Image1:TImage; var Gauge:TGauge);
77 var x,y:integer; Clr:TColor; ClrGray:byte;
78 begin
79 GaugeStart (Gauge, Image1.Width-1);
80 for x:=0 to Image1.Width-1 do
81 begin
82 for y:=0 to Image1.Height-1 do
83 begin
84 Clr := Image1.Canvas.Pixels[x,y];
85 ClrGray := RgbToGray (Clr);
86 Image1.Canvas.Pixels[x,y] :=
87 RGB (ClrGray,ClrGray,ClrGray);
88 end;
89 GaugeProgress (Gauge, Image1, x);
90 end;
91 GaugeStop (Gauge);
92 end;
93
94 procedure ImgInvertColor (var Image1:TImage;

Grafika Komputer - Sept 2004 69


95 var Gauge:TGauge);
96 var x,y:integer; Clr:TColor; r,g,b:byte;
97 begin
98 GaugeStart (Gauge, Image1.Width-1);
99 for x:=0 to Image1.Width-1 do
100 begin
101 for y:=0 to Image1.Height-1 do
102 begin
103 Clr := Image1.Canvas.Pixels[x,y];
104 r := 255 - GetRValue(Clr);
105 g := 255 - GetGValue(Clr);
106 b := 255 - GetBValue(Clr);
107 Image1.Canvas.Pixels[x,y] := RGB (r,g,b);
108 end;
109 GaugeProgress (Gauge, Image1, x);
110 end;
111 GaugeStop (Gauge);
112 end;
113
114 procedure ImgAdd (var Image1:TImage;
115 dr,dg,db:integer; var Gauge:TGauge);
116 var
117 x,y:integer; Clr:TColor; r,g,b:byte;
118 begin
119 GaugeStart (Gauge, Image1.Width-1);
120 for x:=0 to Image1.Width-1 do
121 begin
122 for y:=0 to Image1.Height-1 do
123 begin
124 Clr := Image1.Canvas.Pixels[x,y];
125 r := ByteRange (GetRValue(Clr) + dr);
126 g := ByteRange (GetGValue(Clr) + dg);
127 b := ByteRange (GetBValue(Clr) + db);
128 Image1.Canvas.Pixels[x,y] := RGB (r,g,b);
129 end;
130 GaugeProgress (Gauge, Image1, x);
131 end;
132 GaugeStop (Gauge);
133 end;
134
135 procedure ImgMul (var Image1:TImage;
136 mr,mg,mb:real; var Gauge:TGauge);
137 var
138 x,y:integer; Clr:TColor; r,g,b:byte;

Grafika Komputer - Sept 2004 70


139 begin
140 GaugeStart (Gauge, Image1.Width-1);
141 for x:=0 to Image1.Width-1 do
142 begin
143 for y:=0 to Image1.Height-1 do
144 begin
145 Clr := Image1.Canvas.Pixels[x,y];
146 r := ByteRange (GetRValue(Clr) * mr);
147 g := ByteRange (GetGValue(Clr) * mg);
148 b := ByteRange (GetBValue(Clr) * mb);
149 Image1.Canvas.Pixels[x,y] := RGB (r,g,b);
150 end;
151 GaugeProgress (Gauge, Image1, x);
152 end;
153 GaugeStop (Gauge);
154 end;
155
156 procedure ImgPow (var Image1:TImage;
157 pr,pg,pb:real; var Gauge:TGauge);
158 var
159 x,y:integer; Clr:TColor; r,g,b:byte;
160 begin
161 GaugeStart (Gauge, Image1.Width-1);
162 for x:=0 to Image1.Width-1 do
163 begin
164 for y:=0 to Image1.Height-1 do
165 begin
166 Clr := Image1.Canvas.Pixels[x,y];
167 r := ByteRange (Pow (GetRValue(Clr), pr));
168 g := ByteRange (Pow (GetGValue(Clr), pg));
169 b := ByteRange (Pow (GetBValue(Clr), pb));
170 Image1.Canvas.Pixels[x,y] := RGB (r,g,b);
171 end;
172 GaugeProgress (Gauge, Image1, x);
173 end;
174 GaugeStop (Gauge);
175 end;
176
177 end.

Grafika Komputer - Sept 2004 71