0% found this document useful (0 votes)
36 views32 pages

CG Module 4 2D Viewing and Clipping

The document covers the concepts of two-dimensional viewing and clipping in computer graphics, focusing on the implementation of algorithms for clipping and viewing transformations. It explains normalization and workstation transformations, along with the Cohen-Sutherland and Liang-Barsky line clipping algorithms. The document also provides step-by-step procedures for determining visible line segments within specified clipping windows.

Uploaded by

printopia06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views32 pages

CG Module 4 2D Viewing and Clipping

The document covers the concepts of two-dimensional viewing and clipping in computer graphics, focusing on the implementation of algorithms for clipping and viewing transformations. It explains normalization and workstation transformations, along with the Cohen-Sutherland and Liang-Barsky line clipping algorithms. The document also provides step-by-step procedures for determining visible line segments within specified clipping windows.

Uploaded by

printopia06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Computer Graphics

Module - 4
Two-Dimensional Viewing and Clipping
CSC305

By Prof. Sunil Katkar


Department of Computer Engineering, VCET
Module -4 Two-Dimensional Viewing and Clipping

Objective
To emphasize on implementation aspect of Computer Graphics
Algorithms.

Outcome
At the end of the course student will be able to:
apply line and polygon clipping algorithms on 2D graphical objects.
Two-Dimensional Viewing and Clipping
Viewing Transformation
The process of selecting and viewing the picture with different views
is called windowing.
A process which divides each element of the picture into its visible
and invisible portions, allowing the invisible portion to be discarded
is called clipping.
The picture is stored in the computer memory using world
coordinate system.
The picture is displayed on the display device it is measured in
physical device coordinate system.
The viewing transformation which maps picture coordinates in the
WCS to display coordinates in PDCS is performed by the the
transformation -
• Normalization transformation(N)
• Workstation transformation(W)
Normalization Transformation
To make our programs to be device independent,
• we define the picture coordinates in some units other than pixels.
• we use the interpreter to convert these coordinates to appropriate
pixel values for the particular display device.
The device independent units are called the normalized device
coordinates.
Normalization Transformation
The interpreter uses a simple linear formula to convert the
normalized device coordinates to the actual device coordinates.
x = xn * xw
y = yn * y w
Where, x = Actual device x-coordinate
y = Actual device y-coordinate
xn = Normalized x-coordinate
yn = Normalized y-coordinate
xw = Width of actual screen in pixels.
yw = Height of actual screen in pixels.
The transformation which maps the world coordinates to normalized
device coordinate is called normalization transformation.
Workstation Transformation
An area selected in world coordinate system is called WINDOW.
Window defines what is to be viewed.
An area on a display on a device to which a window is mapped is
called a VIEWPORT.
Viewport defines where it is to be displayed.
Workstation Transformation
The window define in world coordinates is first transformed into the
normalized device coordinates.
The normalized window is then transformed into the viewport
coordinates.
This window to viewport coordinate transformation is known as
workstation transformation.
The viewing transformation is the combination of normalization
transformation and workstation transformations -
V = N. W
Window to Viewport Transformation
Steps -
• The object together with its window is translated until the lower
left corner of the window is at the origin.
• Object and window are scaled until the window has the
dimensions of the viewport.
• Translate the viewport to its correct position on the screen.
Window to Viewport Transformation
 workstation transformation is given as
W = T . S . T-1
1 0 0

T= 0 1 0

- xwmin - ywmin 1

sx 0 0

S= 0 sy 0

0 0 1

where, sx = (xvmax - xvmin ) / (xwmax - xwmin )


sy = (yvmax - yvmin) / (ywmax - ywmin)
Window to Viewport Transformation
1 0 0

T-1 = 0 1 0

xvmin yvmin 1
The overall transformation matrix for W is given as
W = T . S . T-1
sx 0 0
W= 0 sy 0

xvmin - xwmin sx yvmin - ywmin sy 1


Window to Viewport Transformation
Find the normalization transformation window to viewport, with
window, lower left corner at (1, 1), and upper right corner at (3, 5)
onto a viewport with lower left corner at (0, 0) and upper right
corner at (0.5, 0.5).

0.25 0 0

W= 0 0.125 0

- 0.25 - 0.125 1
2D Clipping
The procedure that identifies the portions of a picture that are either
inside or outside of a specified region of space is referred to as
clipping.
The region against which an object is to be clipped is called clip
window or clipping window.
Line Clipping
Consider the line segment with endpoints A(x1, y1) and B(x2, y2)
1. Any point p(x, y) is inside the window if all the following
inequalities are satisfied -
xwmin ≤ x ≤ xwmax
ywmin ≤ y ≤ ywmax
2. If both the endpoints of a line segment are within the window then
the line segment is visible.
3. If the line segment satisfies any one of the following condition
then the line segment is not visible.
x1, x2 < xwmin
x1, x2 > xwmax
y1, y2 < ywmin
y1, y2 > ywmax
Line Clipping
4. If the line segment is neither of category 2 or 3, then it is a
clipping candidate.

Line Clipping Algorithm


 Cohen-Sutherland Line Clipping Algorithm
 Liang-Barsky Line Clipping Algorithm
Line Clipping
Cohen-Sutherland Line Clipping
* Algorithm has nine regions and
* Uses 4-bit code to indicate which of
the 9-regions contains the endpoint
of a line.
* These codes identify the location of
the point relative to the boundaries of
the clipping rectangle
* Each bit position in the region code is used to indicate one of the four
relative coordinate positions of the point with respect to the clipping
window - to the left, right, top or bottom.
* The rightmost bit is the first bit and the bits are set to ‘1’ based on
following rules
Set Bit-1 - if the end point is to the left of the window
Set Bit-2 - if the end point is to the right of the window
Set Bit-3 - if the end point is to the below the window
Set Bit-4 - if the end point is to the above the window
Otherwise, the bit is set to Zero
Cohen-Sutherland Line Clipping Algorithm
1. Read two end points of the line say P1(x1, y1), P2(x2, y2).
2. Read two corners (left-top and right-bottom) of the window.
3. Assign the 4-bit code to each endpoints of the line segment i. e.
B4B3B2B1
If x < xwmin then B1= 1 else B1 = 0.
If x < xwmax then B2= 1 else B2 = 0.
If y < ywmin then B3= 1 else B3 = 0.
If y < ywmax then B4= 1 else B4 = 0
The code is determine according to which of the following 9-regions
of the plane the endpoint lies.
Cohen-Sutherland Line Clipping Algorithm
4. a) If both the endpoint codes are 0000 then the line is visible.
Display the line segment.
Stop.
b) If logical AND of the endpoint codes are not 0000 then the
line segment is not visible.
Discard the line segment.
Stop.
c) If logical AND of the endpoint codes is 0000 then the
line segment is clipping candidate.
5. Determine the intersecting boundaries -
If B1= 1 intersect with x = xwmin.
If B2= 1 intersect with x = xwmax.
If B3= 1 intersect with y = ywmin.
If B4= 1 intersect with y = ywmax.
Cohen-Sutherland Line Clipping Algorithm
6. Determine the intersecting point coordinate (x’, y’) -
The equation of line passing through P1(x1, y1), P2(x2, y2). and
(x’, y’) is
(x’- x1)/(x2 - x1) = (y’- y1) / (y2 - y1)
(x’- x1) = [(x2 - x1) / (y2 - y1)] * (y’- y1)
(x’- x1) = 1 / m * (y’- y1)
x’ = x1 + [1 / m * (y’- y1)] .............1
Here y’ = ywmin or y’ = ywmax
Similarly y’ = y1 + m * (x’- x1) ...........2
x’ = xwmin or x’ = xwmax
where m = (y2 - y1) / (x2 - x1)
7. Go to step 3
8. Draw the remaining line segments.
9. Stop
Find the clipping coordinates of the line joining A(-1, 5), and B(3, 8).
Given xwmin = -3, xwmax= 2 , ywmin = 1, ywmax = 6.
Code for A(-1, 5)
B4B3B2B1 = 0000
 Point A is visible
Similarly, Code for B(3, 8)
B4B3B2B1 = 1010
 Point B is not visible.
Now
0000 AND 1010 = 0000
 Line AB is clipping candidate.
Now Intersecting boundary
Code for B(3, 8) = 1010
If B2 = 1, then intersect with x = xwmax
If B4 = 1, then intersect with y = ywmax
Here we select x = xwmax = 2 is a clipping boundary.
For intersecting point coordinates
x’ = xwmax = 2
y’ = y1 + m * (x’- x1)
m = (y2 - y1) / (x2 - x1) = (8 - 5) / (3 + 1) = 3/4
y’ = 5 + 3/4 * (2 + 1) = 29/4
 I’(x’, y’) = I’(2, 29/4)
 Now AI’ is the clipped line segment.
Code for A(-1, 5) is
B4B3B2B1 = 0000
 Point A is visible
Similarly, Code for I’(2, 29/4) is
B4B3B2B1 = 1000
 Point I’ is not visible.
Now
0000 AND 1000 = 0000
 Line AI’ is clipping candidate.
Now Intersecting boundary
Code for I’(2, 29/4) = 1000
If B4 = 1, then intersect with y = ywmax
Here we select y = ywmax = 6 is a clipping boundary.
For intersecting point coordinates
y’ = ywmax = 6
x’ = x1 + [1 / m * (y’- y1)]
x’ = -1 + 4/3 * (6- 5) = 1/3
 I’’(x’, y’) = I’(1/3, 6)
 Now AI’’ is the clipped line segment.
Code for A(-1, 5) is
B4B3B2B1 = 0000
 Point A is visible
Similarly, Code for I’’(1/3, 6) is
B4B3B2B1 = 0000
 Point I’’ is visible.
Now
0000 AND 0000 = 0000
 Line segment AI’’ is visible.
Find the clipping coordinates of the line joining A(-1, 5), and B(3, 8).
Given xwmin = -3, xwmax= 2 , ywmin = 1, ywmax = 6.
Find the clipping coordinates of the line joining A(40, 15), and B(75, 45).
Given xwmin = 50, xwmax= 80 , ywmin = 10, ywmax = 40.
Code for A(40, 15)
B4B3B2B1 = 0001
 Point A is not visible
Similarly, Code for B(75, 45)
B4B3B2B1 = 1000
 Point B is not visible.
Now
0001 AND 1000 = 0000
 Line AB is clipping candidate.
Now Intersecting boundary
Code for A(40, 15) = 0001
If B1 = 1, then intersect with x = xwmin
Here we select x = xwmin is a clipping boundary.
For intersecting point coordinates
x’ = xwmin = 50
y’ = y1 + m * (x’- x1)
m = (y2 - y1) / (x2 - x1) = (45 - 15) / (75 - 40) = 0.8571
y’ = 15 + 0.8571 * (50 - 40) = 23.571
 I’(x’, y’) = I’(50, 23.571)
 Now I’B is the clipped line segment.
 Code for I’(50, 23.571) is
 B4B3B2B1 = 0000
 Point I’ is visible
 Similarly, Code for B(75, 45) is
 B4B3B2B1 = 1000
 Point B is not visible.
Now
0000 AND 1000 = 0000
 Line I’B is clipping candidate.
Now Intersecting boundary
Code for B(75, 45) = 1000
If B4 = 1, then intersect with y = ywmax
Here we select y = ywmax is a clipping boundary.
For intersecting point coordinates
y’ = ywmax = 40
x’ = x1 + [1 / m * (y’- y1)]
x’ = 40 + 1.1667 * (40 - 15) = 69.1675
 I’’(x’, y’) = I’’(69.1675, 40)
 Now I’I’’ is the clipped line segment
Code for I’(50, 23.571) is
B4B3B2B1 = 0000
 Point I’ is visible
Code for I’’(69.1675, 40) is
B4B3B2B1 = 0000
 Point I’’ is visible
 Line segment I’I’’ is visible.
How the line between (2, 2), and (12, 9) is clipped against window with
(xwmin, ywmin) = (4, 4) and (xwmax, ywmax) = (9, 8).
Liang-Barsky Line Clipping Algorithm
1. Read two end points of the line say P1(x1, y1), P2(x2, y2).
2. Read two corners (left-top and right-bottom) of the window.
3. Calculate pk and qk for k = 1, 2, 3, 4
p 1 = - x q1 = x1 - xwmin
p2 = x q2 = xwmax - x1
p 3 = - y q3 = y1 - ywmin
p 4 = y q4 = ywmax - y1
4. If pk = 0 then
{ the line is parallel to kth boundary
If qk < 0 then
the line is outside the boundary
Discard the line segment
Stop
If qk ≥ 0 then
the line is inside the parallel boundary
}
Liang-Barsky Line Clipping Algorithm
5. Calculate rk = qk / pk for k = 1, 2, 3, 4
6. Determine u1 for all pk < 0 from the set consisting {rk , 0}
select rk for all pk < 0
then u1 = {rk , 0}max
Determine u2 for all pk > 0 from the set consisting {rk , 1}
select rk for all pk > 0
then u2 = {rk , 1}min
7. If u1 > u2 then
{ the line is completely outside the boundary
Discard the line segment
stop
}
Liang-Barsky Line Clipping Algorithm
8. Calculate the endpoints of the clipped line segment
x’ = x1 + u1x
y’ = y1 + u1y
 I1(x’, y’)
Similarly
x’’ = x1 + u2x
y’’ = y1 + u2y
 I2(x’’, y’’)
9. Display the line segment I1I2
10. Stop
Find the clipping coordinates of the line joining A(7,5), and B(9,7)
using Liang-Barsky Line clipping algorithm against the window
having Xwmin = 4, Xwmax= 10 , Ywmin = 4, Ywmax = 9.
x = x2 - x1 = 9 - 7 = 2
y = y2 - y1 = 7 - 5 = 2
Calculate pk and qk , for k = 1, 2, 3, 4
p1 = - x = - 2 and q1 = x1 - xwmin = 7 - 4 = 3
p2 = x = 2 and q2 = xwmax - x1 = 10 - 7 = 3
p3 = - y = - 2 and q3 = y1 - ywmin = 5 - 4 = 1
p4 = y = 2 and q4 = ywmax - y1 = 9 - 5 = 4
As pk ≠ 0, Calculate rk = qk / pk , for k = 1, 2, 3, 4
For k =1, p1 < 0 → r1 = q1 / p1 = 3 / - 2 = - 3/2
For k =2, p2 < 0 → r2 = q2 / p2 = 3 / 2 = 3/2
For k =3, p3 < 0 → r3 = q3 / p3 = 1 / - 2 = - 1/2
For k =4, p4 < 0 → r 4 = q4 / p 4 = 4 / 2 = 2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy