Computer Graphics 2018-2019 2D Viewing and Clipping
Computer Graphics 2018-2019 2D Viewing and Clipping
Introduction
Where xleft, xright, ybottom, ytop are the positions of the edges of
the screen. These inequalities provide us with a very simple method
of clipping pictures on a point by point basis; we substitute the
coordinates of each point for x and y and if the point fails to satisfy
either inequality; it is invisible. It would be quite inappropriate to clip
pictures by converting all picture elements into points and using
these inequalities; the clipping process would take far too long and
would leave the picture in a form no longer suitable for a line
drawing display. We must attempt to clip larger elements of the
picture. This involves developing more powerful clipping algorithms
that can be determine the visible and invisible portions of such
picture elements.
1
Computer graphics 2018-2019 2D Viewing and Clipping
Clipping window
Point clipping
Where xmin, xmax, ymin, ymax are the positions of the edges of the
window.
Line clipping
Lines that do not intersect the clipping window are either completely
inside the window or completely outside the window.
On the other hand a line that intersects the clipping window is
divided by the intersection point (s) into segments that are either
inside or outside the window. The following algorithm provide
efficient way to decide the relationship between an arbitrary line and
the clipping window to find intersection point (s).
2
Computer graphics 2018-2019 2D Viewing and Clipping
The Cohen–Sutherland algorithm
The Cohen–Sutherland algorithm is a computer-graphics algorithm
used for line clipping.
Nine regions are created, eight "outside" regions and one "inside"
region.
For a given line extreme point (x, y), we can quickly find its region's
four bit code. Four bit code can be computed by comparing x and y
with four values (x_min, x_max, y_min and y_max).
Top
3
Computer graphics 2018-2019 2D Viewing and Clipping
4
Computer graphics 2018-2019 2D Viewing and Clipping
Intersection points
y = y1 + m(x – x1)
Where the x value is set either to xmin or to xmax, and the slop of the
line is calculated as
x = x1 + (y – y1) / m
Not
y = y1 + m(x – x1)
x = x1 + (y – y1) / m
5
Computer graphics 2018-2019 2D Viewing and Clipping
Example1 :
Apply the Cohen Sutherland line clipping algorithm to clip the line
segment with coordinates (30,60) and (60,25) against the window
with (Xmin,Ymin)= (10,10) and (Xmax,Ymax)= (50,50).
Solution
AB 1000 AND
0010
m=(25-60)/(60-30)
=-35/30
=-1.16
Then ,We fined the coordinate of intersection point from line A A-.
= 30+(50-60)/-1.16
=30+-10/-1.16
= 30+8.6
=38.6
6
Computer graphics 2018-2019 2D Viewing and Clipping
The boundary line BB- is vertical ,so xmax=x=50 and calculate y value
from this:
y = y1 + m(x – x1)
=25+(-1.16)(50-60)
= 25+11.6
=36.6
7
Computer graphics 2018-2019 2D Viewing and Clipping
Example2:
Window is defined A(20,20),B(90,20),C(90,70),D(20,70)
Find visible portion of
line1 :P1(10,30),P2(80,90)
Line2: Q1(20,10) , Q2(70,60)
using Cohen Sutherland line clipping algorithm.
Solution
1000
0000
=(90-30)/(80-10)
=60/70
=0.8
8
Computer graphics 2018-2019 2D Viewing and Clipping
y = y1 + m(x – x1)
=30+0.8(20-10)
=30+8=38
The boundary line P1P1- is horizontal ,so ymax=y=70 and find x from
this equation:
x = x1 + (y – y1) / m
=80+(70-90)/0.8
=80+(-20)/0.8
=80+(-25)=55
9
Computer graphics 2018-2019 2D Viewing and Clipping
Fined the slop of second line Q1Q2
=(60-10)/(70-20)=50/50=1
x = x1 + (y – y1) / m
=20+(20-10)/1
=20+10=30
11
Computer graphics 2018-2019 2D Viewing and Clipping
Example3:
Solution:
Clip bit code
AB 0000 AND
0000
CD 1000 AND
0110
EF 0001 AND
0101
11
Computer graphics 2018-2019 2D Viewing and Clipping
Find slop for line CD as follow:
=(4-9)/(11-7)
=-5/4=-1.25
The boundary line CC- is horizontal ,so Ymax=y=8 and find x as follow:
x = x1 + (y – y1) / m
= 7+(8-9)/-1.25
=7+-1/-1.25
7+0.8=7.8
the boundary line DD- is vertical ,so xmax=x=10 and find y as follow:
y = y1 + m(x – x1)
=4+(-1.25)(10-11)
=4+1.25
=5.25
12
Computer graphics 2018-2019 2D Viewing and Clipping
Example4:
Window is defined A(10,20),B(20,20),C(20,10),D(10,10) Find
visible portion of line P(15,15),Q(5,5) using Cohen Sutherland
line clipping algorithm.
Solution
PQ 0000 AND
0101
=(5-15)/(5-15)
=-10/-10=1
x = x1 + (y – y1) / m
=15+(10-15)/1
=15-5
= 10
13