Chapter-Iv Windowing - Clipping
Chapter-Iv Windowing - Clipping
CGR-22318
UNIT-IV
WINDOWING AND CLIPPING
Ms. S.A.Salunkhe
03-11-2020
POINTS TO BE COVERED
• INTRODUCTION
• LINE CLIPPING
• CYRUSBECK ALGORITHM
• POLYGON CLIPPING
03-11-2020
INTRODUCTION
• Formal mechanism for displaying of a picture on an output
device.
• CLIPPING
• The process which divides each element of the picture into its
visible and invisible portions, allowing the invisible portion
to be discarded is called as clipping.
03-11-2020
Picture
Picture element
element
outside
inside
window.
window
Needs to be clipped
Clipped picture
element inside
window
03-11-2020
WINDOW TO VIEWPORT
• TRANSFORMATION
The picture element is stored in computer memory using any
convenient Cartesian co-ordinate system referred to as WORLD
COORDINATE SYSTEMS (WCS).
• The mapping of the coordinates of the points and lines that form
the picture into the appropriate physical device coordinate is called
as viewing transformation.
03-11-2020
WINDOW TO VIEWPORT
• TRANSFORMATION
THE VIEWING TRANSFORMATION PIPELINE
03-11-2020
WINDOW TO VIEWPORT
TRANSFORMATION
The mapping of a part of a WCS to PDCS is
referred to as a
viewing transformation.
Viewport
Window YWmax
YWmax
YWmin YWmin
03-11-2020
WINDOW TO VIEWPORT
• TRANSFORMATION
The 2-dimensional viewing transformation pipeline
NVC using
Construct WC
Convert WC to window Convert NVC
Scene Using MC
VC viewport to DC
transformation
specification
MC WC VC NVC DC
• MC-MODELING COORDINATES
• WC – WORLD COORDINATES
• VC – VIEWING COORDINATES
• NVC – NORMALIZED VIEWING
COORDINATES
03-11-2020
• DC – DEVICE COORDINATES
WINDOW TO VIEWPORT
• TRANSFORMATION
The position of the viewport can also be changed.
Y world
Window 1
Viewport
Y0
X world 0
03-11-2020 X0 1
CLIPPING
• The correct way to select visible information for display is to use
clipping
03-11-2020
CLIPPING
• LINE CLIPPING
• The lines are said to be interior to the clipping window and
hence visible if both the end points are interior to the
window.
03-11-2020
CLIPPING
• If both end points of a line are
• Completely right of
• Completely left of
WINDOW
• Completely above
• Completely below
• WORKING
• Divide view plane into 9 regions defined by viewport edges
• Assign each region a 4-bit outcode:
0000
0 001 WINDOW 0010
y
min
101
x x
min max
03-11-2020 0
0100
COHEN-SUTHERLAND LINE CLIPPING
ALGORITHM
y
min
101
x x
min max
03-11-2020 0
0100
COHEN-SUTHERLAND LINE CLIPPING
ALGORITHM
• Set bits with simple tests
x > xmax y < ymin etc.
03-11-2020
COHEN-SUTHERLAND LINE CLIPPING
ALGORITHM
• Intersect line with edge (how?)
E
D
C
D
C
03-11-2020
COHEN-SUTHERLAND LINE CLIPPING
ALGORITHM
• EXAMPLE
• Consider the clipping window and the lines shown in figure. Find the
region code for each end point and identify the line is completely visible,
partially visible or completely visible.
03-11-2020
COHEN-SUTHERLAND LINE CLIPPING
ALGORITHM
• SOLUTION
1000
1001 P9 1010
y
max P4 P2
P1 P10
0001 0000
P8
P6
P3 0010
P5
y
min x x
min max
0101 0100 P7 0110
03-11-2020
COHEN-SUTHERLAND LINE CLIPPING
ALGORITHM
LOGICAL
LINE END POINT CODES RESULT
ANDing
Completely
P3P4 0001 0001 0001
Invisible
03-11-2020
COHEN-SUTHERLAND SUBDIVISION LINE
CLIPPING ALGORITHM
• WORKING
• The Cohen Sutherland algorithm begins the clipping process for a partially
visible line by comparing an outside endpoint to a clipping boundary to
determine how much of the line can be discarded.
• Then the remaining part of the line is checked against the other
boundaries, and the process is continued until either the line is totally
discarded or a section is found inside the window.
03-11-2020
03-11-2020
COHEN-SUTHERLAND SUBDIVISION LINE
CLIPPING ALGORITHM
• The intersection points with a clipping boundary can be calculated using
the slope-intercept form of the line equation.
• The equation for line Passing through Points P1 (x1, y1) and P2 (x2, y2) is
𝑦 = 𝑚 𝑥 − 𝑥1 + 𝑦1 𝑜𝑟 𝑦 = 𝑚 𝑥 − 𝑥2 + 𝑦2
𝑦 2 − 𝑦1
𝑤ℎ𝑒𝑟𝑒, 𝑚= 𝑥 −𝑥 (slope of the line)
2 1
03-11-2020
COHEN-SUTHERLAND SUBDIVISION LINE
CLIPPING ALGORITHM
• Therefore, the intersections with the clipping boundaries of the window
are given as:
1
• Top: 𝑦 𝑇, 𝑥 = 𝑥 1 + 𝑦𝑇 − 𝑦1; 𝑚≠0
𝑚
1
• Bottom: 𝑦𝐵, 𝑥 = 𝑥1 + 𝑦 𝐵 − 𝑦1 ; 𝑚≠0
𝑚
03-11-2020
ALGORITHMS STEPS
1. Read two end Points of the line say P1 (x1, y1) and P2 (x2, y2).
2. Read two corners (left-top and right-bottom) of the window, say (Wx1, Wy1 and Wx2, Wy2).
3. Assign the region codes for two end points P1 and P2 using following steps :
• Initialize code with bits 0000
– Set Bit 1 - If (x < Wx1)
– Set Bit 2 - If (x > Wx2)
– Set Bit 3 - If (y < Wy2)
– Set Bit 4 - If (y > Wy1)
4. Check for visibility of line P1 P2
– a) If region codes for both endpoints P1 and P2 are zero then the line is completely visible. Hence draw the line
and go to step 9.
– b) If region codes for endpoints are not zero and the logical ANDing of them is also nonzero then the line is
completely invisible, so reject the line and go to step 9.
– c) If region codes for two endpoints do not satisfy the conditions in 4a. and 4b. the line is partially visible.
5. Determine the intersecting edge of the clipping window by inspecting the region codes of two
endpoints.
– a) If region codes for both the end points are non-zero, find intersection points P’1 and P’2 with boundary edges
of clipping window with respect to point P1 and point P2,respectively
– b) If region code for any one end Point is non-zero then find intersection Point P’1 or P’2 with the boundary edge
of the clipping window with respect to it.
6. Divide the line segments considering intersection points.
7. Reject the Line segment if any one end Point of it appears outsides the clipping window.
8. Draw the remaining line segments.
9. Stop.
03-11-2020
COHEN-SUTHERLAND SUBDIVISION LINE CLIPPING
ALGORITHM
• EXAMPLE - 1
• Clip two lines P1 (40,15) - P2 (75,45) and P3 (70,20) – P4 (100,10) against a
window A (50,10) , B (80,10) , C (80,40) & D (50,40).
x 1000 x
min max
P2
1001 1010
y
max
y
min
0101 0100 0110
COHEN-SUTHERLAND SUBDIVISION LINE CLIPPING
ALGORITHM
• SOLUTION
• Line 1 : P1 (40,15) - P2 (75,45)
𝑥𝐿, 𝑦 = 𝑚 𝑥𝐿 − 𝑥1+ 𝑦1
𝑦 2 − 𝑦1 6
= 50 − 40 + 15 = 𝟐𝟑. 𝟓𝟕 I1 =
𝑚= 𝑥 −𝑥 7
2 1
50,23.57
45 − 15 𝟔
= = 𝑦𝑇, 𝑥 = 𝑥1 + 1/𝑚 + (𝑦𝑇 − 𝑦1)
75 − 40 𝟕 I2 = (690.6,40)
= 40 + 1.16 + (40 − 15) = 𝟔𝟗. 𝟏𝟔
COHEN-SUTHERLAND SUBDIVISION LINE CLIPPING
ALGORITHM
• EXAMPLE - 2
• Clip two lines P1 (40,15) - P2 (75,45) and P3 (70,20) – P4 (100,10) against a
window A (50,10) , B (80,10) , C (80,40) & D (50,40).
x 1000 x
min max
1001 1010
y
max
P3
0001 0000 0010
y
min
0101 0100 0110
P4
COHEN-SUTHERLAND SUBDIVISION LINE CLIPPING
ALGORITHM
• SOLUTION
• Line 2 : P3 (70,20) – P4 (100,10)
𝑦 − 𝑦1 𝑥𝑅, 𝑦 = 𝑚 𝑥𝑅 − 𝑥1 + 𝑦1
𝑚= 2
𝑥2 − 𝑥1 −1
= 80 − 70 + 20 I = 80,16.66
10 − 20 −𝟏 3
= =
100 − 70 𝟑 = 𝟏𝟔. 𝟔𝟔
COHEN-SUTHERLAND SUBDIVISION LINE CLIPPING
ALGORITHM
• Clip a line P1 (-13,5) - P2 (17,11) against a window having its lower left
corner at (-8,-4) & upper right corner at (12,8).
x 1000 x
max
min
1010
1001
P2
y
max
P1
0000 0010
0001
y
min
0101 0100 0110
COHEN-SUTHERLAND SUBDIVISION LINE CLIPPING
ALGORITHM
• SOLUTION
• Line 1 : P1 (-13,5) - P2 (17,11)
𝑥𝐿, 𝑦 = 𝑚 𝑥𝐿 − 𝑥1 + 𝑦1
𝑦 2 − 𝑦1 1
= −8 − (−13) + 5 = 𝟔 I1 = −8,
𝑚= 𝑥 −𝑥 5
2 1
6
11 − 5 𝟏
= = 𝑦𝑇, 𝑥 = 𝑥1 + 1/𝑚 + (𝑦𝑇 − 𝑦1)
17 + 13 𝟓 I2 = (2, 8)
= −13 + 5 + (8 − 5) = 𝟐
CYRUSBECK
ALGORITHM
• This algorithm is applicable for non rectangular
clipping
window.
𝑃 𝑡 = 𝑃1 + 𝑃2 − 𝑃1 𝑡 0 < t < 1
𝑖𝑓 𝑡 = 0 → = 𝑃1
𝑃 𝑡
= 𝑃2
𝑖𝑓 𝑡 = 1 → 𝑃 𝑡
03-11-2020
CYRUSBECK
ALGORITHM• DOT PRODUCT
𝒏. [𝑷 𝒕 − 𝒇]
Y
Boundary Point
f
• 𝑖𝑓 < 0 → 𝑃 𝑡 − 𝑓 𝑖𝑠 𝑒𝑥𝑡𝑒𝑟𝑖𝑜𝑟
R
n Convex Region
• 𝑖𝑓 > 0 → 𝑃 𝑡 − 𝑓 𝑖𝑠 𝑖𝑛𝑡𝑒𝑟𝑖𝑜𝑟
X
0
• 𝑖𝑓 = 0 → 𝑃 𝑡 − 𝑓
𝑖𝑠 𝑝𝑎𝑟𝑎𝑙𝑙𝑒𝑙 𝑡𝑜 𝑏𝑜𝑢𝑛𝑑𝑎𝑟𝑦
03-11-2020
CYRUSBECK
ALGORITHM
Y
P2
n.[P(t)-f]>0
n.[P(t)-f]<0
n.[P(t)-f]=0
n
P1
X
0
03-11-2020
CYRUSBECK
• SubstituteALGORITHM
value of P(t) in the dot product
𝑛. [𝑃1 + P2 − P1 . t − f = 0
𝑛. 𝑃1 − 𝑓+ 𝑛. 𝑃2 − 𝑃1 . 𝑡 = 0
−𝑛. 𝑊
𝑡=
𝑛. 𝐷
03-11-2020
CYRUSBECK
• When we get value of 𝑛. 𝐷 < 0 , then we get upper limit value
ALGORITHM
(tU)
6
V3
5 V5
4 P2(6,3)
3
V6
2 V2
P1(-2,1) 1
V1
X
0
03-11-2020
-2 1 2 3 4 5 6
• FOR EDGE – V1,V2 • 𝑡𝐿 = −𝑛.𝑊
𝑛.𝐷
• D = P2 – P1 = [6 3]-[-2 1] = [8 2]
• = − −4
• f = (3,0) 10
4
• W = P1 – f = [-2 1]-[3 0] = [-5 1] •
10
=
• n = [1 1] • = 𝟎.
• W.n & D.n 𝟒
• D.n > 0 = tL
Y
V4
6
V3
5 V5
4 P2(6,3)
3
V6
2 V2
P1(-2,1) 1
V1
X
0
-2 1 2 3 4 5 6
EDGE n f W n.W n.D t t
L (max) U (min)
V1 V2 [1 1] [3 0] [-5 1] -4 10 0.4 -
V6 V1 [-1 1] [3 0] [-5 1] 6 -6 - 1
CYRUSBECK
• ALGORITHM
The MAXIMUM LOWER LIMIT (tL) = 0.4
03-11-2020
LIANG BARSKY
ALGORITHM
• Liang-Barsky has 4 inequalities with two parameters p & q as
𝑡𝑝𝑖 ≤ 𝑞𝑖
𝑝1 = −∆𝑥, 𝑞1 = 𝑥1 − 𝑥𝑤𝑚𝑖𝑛
𝑝2 = ∆𝑥, 𝑞2 = 𝑥𝑤𝑚𝑎𝑥 − 𝑥1
𝑝3 = −∆𝑦, 𝑞3 = 𝑦1 − 𝑦𝑤𝑚𝑖𝑛
𝑝4 = ∆𝑦, 𝑞4 = 𝑥𝑤𝑚𝑎𝑥 − 𝑦1
03-11-2020
LIANG BARSKY
• ALGORITHM
Following observations canbe easily made from
above
definitions of parameters p and q
𝑞
𝑡=
𝑝
• t1 and t2 that defines that part of the line that lies within the clip
rectangle
• t1 = determined by checking the rectangle edges for which the line
proceeds from the outside to the inside (p < 0).
• Now, if t1 > t2, the line is completely outside the clipping window
and it can be rejected.
• Solution : Here,
• X1 = 20 Xwmin = 40
• Y1 = 20 Ywmin = 40
• X2 = 80 Xwmax = 100
• Y2 = 110 Ywmax = 90
• P1 = -∆x = -(x2 – x1) = - (80 -20) = -60
• q1 = X1 – Xwmin = 20 – 40 = -20
• q3 = Y1 – Ywmin = 20 – 40 = -20
• q4 = Xwmax – Y1= 90 – 20 = 70
• q1 / p1 = -20 / -60 = 0.33
• q2 / p2 = 80 / 60 = 1.33
• q4 / p4 = 70 / 90 = 0.778
• XX1 = X1 + t1 ∆x = 20 + 0.33 x 60 = 40
• YY1 = Y1 + t1 ∆y = 20 + 0.33 x 90 = 50
• YY2 = Y1 + t2 ∆y = 20 + 0.778 x 90 = 90
MIDPOINT SUBDIVISION ALGORITHM
• The Cohen-Sutherland subdivision algorithm requires the
calculation of the intersection of the line with the window
edge.
P3 P4 P3
P1 P1 P1
P5 P2 P5 P5
P4 P4 P4 P3
P3 P3
P6 P6
P7
P1 P5
P7
(g)
POLYGON CLIPPING
• A polygon is noting but collection of lines.
LINE
a–b
c–d
d–e
f–g
Are included in the polygon which is not
desired.
SUTHERLAND – HODGEMAN POLYGON CLIPPING
• The Sutherland-Hodgman algorithm performs a clipping of a
polygon against each window edge in turn.
• It consists of 4 cases:
CASE-I : If the first vertex of the edge is outside the window and
second vertex is inside then the intersection point of the polygon
edge with the window boundary and the second vertex are
added to the output vertex list.
SAVE {V2}
SUTHERLAND – HODGEMAN POLYGON CLIPPING
CASE-III : If first vertex of the edge is inside the window and
second vertex is outside then only the intersection point of the
polygon edge with the window boundary is added to the output
vertex list.
SAVE {V’1}
SUTHERLAND – HODGEMAN POLYGON CLIPPING
CASE-IV : If both vertices of the edge are outside the window,
then nothing is added to the output vertex list.
SAVE {NULL}
SUTHERLAND – HODGEMAN POLYGON CLIPPING
• EXAMPLE
• For a polygon and clipping window as shown in figure give the
list of vertices after each boundary clipping.
SUTHERLAND – HODGEMAN POLYGON CLIPPING
• SOLUTION
Left Clipped:
Output Vertex =
{V’1, V2,
V’2,V3,V’3,V4,V’4}
SUTHERLAND – HODGEMAN POLYGON CLIPPING
Top Clipped:
Output Vertex =
{V’1, V2,
V’2,V’3,V4,V’4}
Right Clipped:
Output Vertex =
{V’1, V2,
V’2,V’3,V4,V’4}
SUTHERLAND – HODGEMAN POLYGON CLIPPING
Bottom Clipped:
Output Vertex =
{V’1, V2,
V’2,V’3,V4,V’4}
{V’1, V2,
V’2,V’3,V4,V’4}