Grafika Komputer Pertemuan Ke-9
BAB-8 CLIPPING 2D Garis yang tidak perlu digambar dapat kita potong dengan metode clipping. Ada beberapa metode clipping yang umum diantaranya Cohen-Sutherland dan Liang-Barsky. By: I Gusti Ngurah Suryantara, S.Kom., M.Kom 8.1. Pendahuluan Clipping merupakan metode untuk menghilangkan garis yang tidak perlu digambar apabila melebihi window viewing (area gambar). Ada beberapa algoritam clipping yang ada, namun kita akan membahas algoritma yang diusulkan oleh Cohen–Sutherland dan Liang-Barsky. 8.2. Ketampakan Garis (Line Visibility) Garis-garis yang tampak pada area gambar atau viewing dapat dikelompokkan menjadi tiga kelompok, yaitu: 1. Garis yang terlihat seluruhnya (Fully visible). 2. Garis yang hanya terlihat sebagian (Partiality Visible). 3. Garis yang tidak terlihat seama sekali (Fully Invisible). Fully invisible Partially Invisible viewport
Fully visible Gambar 8.1. Ketampakan sebuah garis terhadap viewport Garis yang dipotong adalah garis yang memiliki ketampakan sebagaian (Partiality Visible) dari daerah area gambar. Sedangkan garis yang memiliki ketampakan seluruhnya tidak perlu dipotong, dan garis yang memiliki ketampakan tidak terlihat sama sekali tidak perlu digamgar. 8.3. Algoritma Cohen - Sutherland Ivan Edward Sutherland lahir pada tahun 1938 di hastings, nebraska amerika, dia adalah perintis ilmu komputer dan internet. Dia menerima Turing Award 1988 untuk penemu Sketchpad sebuah langkah awal untuk antarmuka pengguna grafis di komputer pribadi. Dia memperoleh sarjana elektro dari institut teknologi carnegie (sekarang carnegie mellon university), pasca sarjana dari caltech, dan PhD dari MIT di EECS pada tahun 1963. Dia adalah anggota dari nastional academy of engineering, serta anggota national academy of sciences.
104
Grafika Komputer Pertemuan Ke-9 8.3.1. Menentukan Ketampakan Garis Cohen-Sutherland mengusulkan sebuah metode untuk menentukan apakah sebuah garis perlu dipotong atau tidak. Area gambar didefinisikan sebagai sebuah area segiempat yang dibatasi oleh xMin, xMax, yMin, yMax seperti pada Gambar 8.2. y
yMax
yMin
xMin
xMax
x
Gambar 8.2. Area gmanar
Setiap ujung garis diberi kode 4 bit dan disebut sebagai region code, region code ditentukan berdasarkan area dimana ujung garis tersebut berada. Cohen-Sutherland menyusun region code seperti pada Gambar 8.3. 3
2
1
0
T B R L Gambar 8.3. Susunan region-code
Dimana isi dari masing-masing bit ditentukan berdasarkan pengujian seperti pada Tabel 8.1.
Bit Ke 0 1 2 3
Tabel 8.1. Isi region code Region Bit Isi L 1 apabila x < xMin 0 apabila x >=xMin R 1 apabila x > xMax 0 apabila x <= xMax B 1 apabila y < yMin 0 apabila y >= yMin T 1 apabila y > yMax 0 apabila y <=yMax
105
Grafika Komputer Pertemuan Ke-9
1001
1000
1010
0001
0000
0010
0101
0100
0110
Gambar 8.4. Region code dinyatakan dengan biner memperlihatkan posisi dari titik terhadap bidang clipping.
Latihan: Jika diketahui area gambar ditentukan dengan xMin = 1, yMin = 1 dan xMax = 4, yMax = 5 dan dua buah garis: 1. P(-1,-2) – (5,6) 2. Q(-1,5) – (6,7) Maka tentukan region code dari masing-masing ujung garis tersebut.
Gambar 8.5. Garis P dan Q
Jawab: 1. Garis P: Ujung garis P(-1, -2) L = 1; karena x < xMin atau -1 < 1 R = 0; karena x < xMax atau -1 < 4 B = 1; karena y < yMin atau -2 < 1 T = 0; karena y < yMax atau -2 < 5 Dengan demikian region code untuk ujung P(-1,-2) adalah 0101, ujung garis P(5,6).
106
Grafika Komputer Pertemuan Ke-9 L = 0; karena x > xMin atau 5 > 1 R = 1; karena x > xMax atau 5 > 4 B = 0; karena y > yMin atau 6 > 1 T = 1; karena y > yMax atau 6 > 5 Dengan demikian region code untuk ujung P(5,6) adalah 1010, karena region code dari kedua ujung garis tidak sama dengan 0000 maka garis P bersifat kemungkinan partialy invisible dan perlu dipotong. 2. Garis Q: Dengan cara yang sama pada garis P maka akan ditentukan region code: Ujung garis Q(-1,5) mempunyai region code = 0001 Ujung garis Q(6,7) mempunyai regian code = 1010 Karena region code tidak sama dengan 0000 maka garis Q bersifat kemungkinan partialy invisible dan perlu dipotong.
8.3.2. Menentukan Titik Potong Setelah garis ditentukan ketampakannya, maka langkah berikutnya adalah menentukan lokasi titik potong antara garis tersebut dengan batas area gambar. Titik potong dihitung berdasarkan bit=1 dari region code dengan menggunakan panduan pada Tabel 8.2. Tabel 8.2. Mencari titik potong Region Bit Berpotongan dengan Dicari Titik Potong L=1 xMin Yp1 (xMin, Yp1) R=1 xMax Yp2 (xMax, Yp2) B=1 yMin Xp1 (xP1, Ymin) T=1 yMax Xp2 (xP2, Ymax) Dengan Xp1, Xp2, Yp1 dan Yp2 dihitung menggunakan persamaan berikut: Ymin – y1 Xp1 = x1+ ---------------------M Ymax – y1 Xp2 = x1 + ---------------------M Yp1 = y1 + m * (xmin – x1) Yp2 = y1 + m * (xmax – x1) Dengan: Y2 – Y1 M = -------------X2 - X1
107
Grafika Komputer Pertemuan Ke-9 Bergantung kepada lokasi ujung garis maka kita akan memperoleh 2, 3, atau 4 titik potong seperti pada Gambar 8.6.
Gambar 8.6. Kemungkinan banyaknya titik potong Apabila ditemukan titik potong lebih dari dua pada suatu ujung maka pilihlah titik potong yang ada di dalam area gambar. Untuk contoh diatas titik potong pada garis P adalah : Region Bit B = 1 titik (1.25 , 1) Region Bit R = 1 titik (4, 4.7) Cara mencari titik potong sebagai berikut : Titik potong garis P (-1,-2) – (5,6) y2 – y1 m = -------x2 – x1
=
6 – (-2 ) --------5 - (-1)
=
8/6
Region code 0101 di titik (-1,-2): L=1 yp1 = y1 + m * (xmin – x1) = -2 + (8/6) * (1-(-1)) yp1 = 0,86 Titik potongnya adalah (xmin,yp1) = (1, 0.86) Ymin – y1 B = 1 xp1 = x1 + --------------M (1 – (-2)) = -1 + --------------8/6 xp1 = 1.25 Titik potongnya adalah (xp1, ymin) = (1.25 , 1)
108
Grafika Komputer Pertemuan Ke-9 Region code 1010 di titik (5,6): R =1 yp2 = y1 + m * (xmax – x1) = 6 + (8/6) * (4-5) yp2 = 4.7 Titik potongnya adalah(xmax, yp2) = (4, 4.7) Ymax – y1 T =1 xp2 = x1 + --------------------m (5 – 6) = 5 + ---------------------8/6 xp2 = 4.25 Titik potongnya adalah (xp2, ymax) = (4.25 , 5) Ada titik potong pada garis P yaitu (1, 0.67) , (1.25 ,1 ) , (4 , 4.7) , (4.25 , 5). Pilih titik potong yang terdapat dalam viewport yaitu (1.25 , 1) dan (4 , 4.7) Penggalan program fungsi Cohen-Sutherland dalam VB. '---------------------------------------------------------'Clipping routine for Cohen-SutherLand '---------------------------------------------------------Function clipLine(l As line, r As line) Dim p1 As Point Dim p2 As Point Dim c1 As Code Dim c2 As Code Dim t As line Dim done As Boolean Dim draw As Boolean Dim m As Variant 'MsgBox toString(l) fixRegion r p1 = l.p1 p2 = l.p2 done = False draw = False While done = False c1 = getCode(p1, r) c2 = getCode(p2, r) If accept(c1, c2) Then done = True draw = True ElseIf reject(c1, c2) Then done = True Else
109
Grafika Komputer Pertemuan Ke-9 If isInside(c1) Then swapPts p1, p2 swapCodes c1, c2 End If m = (p2.Y - p1.Y) / (p2.X - p1.X) If c1.c(1) Then 'crosses left p1.Y = p1.Y + (r.p1.X - p1.X) * m p1.X = r.p1.X ElseIf c1.c(2) Then 'crosses right p1.Y = p1.Y + (r.p2.X - p1.X) * m p1.X = r.p2.X ElseIf c1.c(3) Then 'crosses bottom p1.X = p1.X + (r.p1.Y - p1.Y) / m p1.Y = r.p1.Y ElseIf c1.c(4) Then 'crosses bottom p1.X = p1.X + (r.p2.Y - p1.Y) / m p1.Y = r.p2.Y End If End If Wend t.p1 = p1 t.p2 = p2 t.c = l.c If draw Then drawLine t, 0, Form1.Picture1 Form1.List2.AddItem (toString(t)) End If End Function
Contoh: Penerapan clipping Cohen-Sutherland dengan menggunakan VB.
Gambar 8.7. Clipping garis dengan menggunakan algoritma Cohen-Sutherland.
110
Grafika Komputer Pertemuan Ke-9 8.4. Clipping Garis Liang-Barsky Brian A. Barsky, lahir di Montreal, adalah Profesor di University of California, Berkeley bekerja di komputer grafis dan geometris pemodelan serta promosi dan sebagainya dan visi ilmu pengetahuan. Beliau juga merupakan anggota dari Joint Graduate Group di Bioengineering, sebuah program antar-kampus, antara UC Berkeley dan San Francisco UC. Liang-Barsky menemukan algoritma clipping garis yang lebih cepat. Menurut Liang-Barsky bentuk pertidaksamaan sebagai berikut: Clipping yang lebih cepat dikembangkan berdasarkan persamaan parametrik dari segmen garis dapat ditulis dalam bentuk: x = x1 + u.dx y = y1 + u.dy Dimana dx = x2 – x1 dan dy = y2-y1. Diman nilai uЄ[0,1]. Menurut Liang dan Barsky bentuk pertidaksamaan sebagai berkut: xwmin <= x1 + u.dx <= xwmax ywmin <= y1 + u.dy <= ywmax Dengan u.pk <= qk,
k=1,2,3,4
Dimana parameter p dan q ditentukan sebagai berikut: k = 1 (Kiri): p1 = -dx, q1= x1-xwmin k = 2 (Kanan): p2 = dx, q2= xwmax – x1 k = 3 (Bawah): p3 = -dy, q3= y1-ywmin k = 4 (Atas): p4 = dy, q4= ywmax-y1 Garis yang sejajar dengan salah satu batas clipping mempunyai pk = 0 untuk nilai k=1,2,3,4 yaitu left, right, botton, dan top. Untuk setiap nilai k, juga diperoleh qk<0, maka garis sepenuhnya diluar batas clipping. Bila qk>=0, maka garis didalam dan sejajar batas clipping. Bila pk<0, garis memotong batas clipping dari luar ke dalam, dan bila pk>0, garis memotong batas clipping dari dalam ke luar. Untuk nilai pk yang tidak sama dengan 0, nilai u dapat diperoleh dengan u = qk / pk Untuk setiap garis, dapat dihitung nilai dan parameter u1 dan u2 yang menentukan posisi garis dalam bidang clipping. Nilai u1 diperlihatkan dengan batas clipping dimana garis memotong batas clipping dari luar ke dalam (p<0). rk = qk / pk
111
Grafika Komputer Pertemuan Ke-9 Dengan nilai u1 adalah nilai maksimum dari nilai 0 dan bermacam-macam nilai r. Sebaliknya nilai u2 ditentkan dengan memeriksa batas dimana clipping dipotong oleh garis dari dalam keluar (p>0). nilai rk dihitung untuk setiap batas clipping, dan nilai u2 merupakan nilai minimum dari sekumpulan nilai yang terdiri dari 1 dan nilai r yang dihasilkan. Bila u1>u2, maka garis sepenuhnya berada di luar clip window dan dapat dihilangkan . sebaliknya bila tidak endpoint dari garis yang di clip dihitung dari dua nilai parameter u. Untuk (pi < 0) t1 = Max (rk) Untuk (pi > 0) t2 = Min (rk) Jika t1 < t2 cari nilai endpoint yang baru. t1 ( x1 + dx * t1 , y1 + dy * t1) titik awal garis yang baru t2 ( x1 + dx * t1 , y1 + dy * t1) titik ujung garis yang baru Algoritma Liang-Barsky lebih efisien dibandingkan dengan Cohen-Sutherland karena perhitungan titik potong dihilangkan. Algoritma 1. Initialize line intersection paramter: m1 <- 0, m2 <- 1 2. Compute dx, dy 3. For each window boundray Repeat Compute q,p If p < 0 (outside > inside) Then Compute Int = q / p If Int > m2 Then reject Else If Int > m1 Then M1 Int Else If p > 0 (inside -> outside) Then Compute Int <- q/p If Int < m1 Then Reject Else If Int < m2 Then m2 <- Int Else p = 0 If q < 0 reject 4. If m1 is greater Then 0 (has been modified) compute new x1, y1 5. If m2 is less Then 1 (has been modified) compute new x2, y2 Latihan: Diketahui titik garis P1(-1,-2) dan P2(2,4) dan viewport xl = 0, xr = 1, yb = 0 dan yt = 1.
112
Grafika Komputer Pertemuan Ke-9 Jawab: P1(-1,-2) P2(2,4) xL =0, xR=1, yB = 0, yT =1 dx = x2 – x1 = 2 – (-1) = 3
dy = y2 – y1 = 4 – (-2) = 6
p1 = -dx = -3
, q1 = x1 – x L = -1- 0 = -1
q1/p1 = -1/-3 = 1/3
P2 = dx =3
, q2 = xR – x 1 = 1- (-1) = 2
q2/p2 = 2/3
P3 = -dy = -6
, q3 = y1 - yB = -2- 0 = -2
q3/p3 = -2/-6 = 1/3
P4 = dy =6
, q4 = yT - yl = 1- (-2)= 3
q4/p4 = 3/6 = 1/2
Untuk (pi < 0 ) T1 = “Max” (1/3, 1/3, 0) = 1/3 Untuk (pi > 0 ) T2 = Min (2/3, 1/2, 1) = ½ T1 < T2 Perhitungan endpoint baru T1 = 1/3 X1’ = x1 + dx * t1 = -1 + ( 3 * 1/3) = -1 + 1 = 0 Y1’ = y1 + dy * t1 = -2 + 6 * 1/3 = -2 + 2 = 0 (X1’,Y1’) = (0,0) T2 = 1/2 X2’ = x1 + dx * t2 = -1 + ( 3 * 1/2) = 1/2 Y2’ = y1 + dy * t2 = -2 + 6 * 1/2= 1 (X2’,Y2’) = (1/2,1)
113
Grafika Komputer Pertemuan Ke-9
Function clipLiangBarsky(region As line, p As PictureBox) Form1.Picture1.Cls Form1.List2.Clear p.DrawWidth = 1 p.DrawStyle = 2 p.Line (0, region.p1.Y)-(p.Width, region.p1.Y), QBColor(7) p.Line (0, region.p2.Y)-(p.Width, region.p2.Y), QBColor(7) p.Line (region.p1.X, 0)-(region.p1.X, p.Height), QBColor(7) p.Line (region.p2.X, 0)-(region.p2.X, p.Height), QBColor(7) p.DrawStyle = 1 For i = 0 To n drawLine Garis(i), 7, p Next i p.DrawStyle = 0 p.DrawWidth = 2 fixRegion region For i = 0 To (n - 1) clipLine2 Garis(i), region, p Next i End Function
'====================================================================== ' Liang-Barsky Clipping '====================================================================== Function clipTest(p As Double, q As Double, u1 As Double, u2 As Double) As Boolean Dim r As Double clipTest = True If p < 0 Then r = q / p If r > u2 Then clipTest = False ElseIf r > u1 Then u1 = r End If Else If p > 0 Then r = q / p If r < u1 Then clipTest = False ElseIf r < u2 Then u2 = r End If ElseIf q < 0 Then clipTest = False End If End If End Function
114
Grafika Komputer Pertemuan Ke-9 PR – diskusi boleh 1. Diketahui titik awal P (1,1) dan titik akhir di Q (10,10), dengan area clipping xmin = 1, ymin=1, xmax= 7 dan ymax=7 selesaikan dengan clipping CohenSutherland. 2. Berdasarkan soal no 1 lakukan clipping menggunakan Liang-Barsky dimana xl =1, xr= 7, yb = 1 dan yt = 7. 3. Berdasarkan soal no 1 selesaikan dengan metode Nichol-Lee-Nichol bila area clipping barada pada batas KiriBawah = 1,1 dan batas KanaAtas = 7,7.
Nb: Lengkapi denga gambar sebelum di clipping dan gambar sesudah clipping.
115