Module 3 - 2D Viewing & Clipping
Module 3 - 2D Viewing & Clipping
the term window refers to an area of a world co-ordinate scene that has been
selected for display picture that is selected for viewing
2D viewing Pipeline
• Some graphics packages that provide window and viewport operations allow
only standard rectangles,
• but a more general approach is to allow the rectangular window to have any
orientation.
• In this case, we carry out the viewing transformation in several steps, as
indicated in this figure
Viewing transformation pipeline (1)
• First, we construct the scene in world coordinates using the output
primitives and attributes
• Next. To obtain a particular orientation for the window, we can set up a
two-dimensional viewing-coordinate system in the world-coordinate
plane, and define a window in the viewing-coordinate system.
• The viewing coordinate reference frame is used to provide a method for
setting up arbitrary orientations for rectangular windows.
• Once the viewing reference frame is established, we can transform
descriptions in world coordinates to viewing coordinates.
Viewing transformation pipeline (1)
• We then define a viewport in normalized coordinates (in the range from 0
to 1 ) and map the viewing-coordinate description of the scene to
normalized coordinates.
• At the final step, all parts of the picture that lie outside the viewport are
clipped, and the contents of the viewport are transferred to device
coordinates.
Zooming and Panning effects
• By changing the position of the viewport, we can view objects at different
positions on the display area of an output device.
• Also, by varying the size of viewports, we can change the size and
proportions of displayed objects.
• We achieve zooming effects by successively mapping different-sized
windows on a fixed-size viewport.
• As the windows are made smaller, we zoom in on some part of a scene to
view details that are not shown with larger windows.
• Similarly, more overview is obtained by zooming out from a section of a
scene with successively larger windows.
• Panning effects are produced by moving a fixed-size window across the
various objects in a scene
VIEWING COORDINATE REFERENCE FRAME
• This coordinate system provides the reference frame for specifying the
world coordinate window
VIEWING COORDINATE REFERENCE FRAME
• We set up the viewing coordinate system using the following procedures:
• First, a viewing-coordinate origin is selected at some world position:
po = (x0, y0).
• Then we need to establish the orientation, or rotation, of this reference
frame.
• One way to do this is to specify a world vector v that defines the viewing
yv, direction.
• Vector v is called the view up vector.
VIEWING COORDINATE REFERENCE FRAME
• Given V, we can calculate the components of unit vectors v = (vx, vy) and
• U = (ux, uy) for the viewing yv, and xv, axes, respectively.
• These unit vectors are used to form the first and second rows of the
rotation matrix r that aligns the viewing xv yv axes with the world xw yw
axes.
• We obtain the matrix for converting world coordinate positions to viewing
• Coordinates as a two-step composite transformation:
• First, we translate the viewing origin to the world origin,
• then we rotate to align the two coordinate reference frames.
VIEWING COORDINATE REFERENCE FRAME
• the composite two-dimensional transformation to convert world
coordinates to viewing coordinate is
• Where T is the translation matrix that takes the viewing origin point po to the
world origin, and R is the rotation matrix that aligns the axes of the two
reference frames.
Clipping Operations
• Procedure that identifies those portions of a picture that are either
inside or outside of a specified region of space
• Region against which an object is to be clipped is called a clip window
Point Clipping
• The Clip window is a rectangle to the standard position, we
save a point P=(x,y) for display if the following inequalities are
satisfied:
xwmin<=x<=xwmax
ywmin<=y<=ywmax
18
ywmax
P( x, y)
ywmin
xwmin xwmax
p1 p6
Window
p7 p5
ywmin
p8
xwmin xwmax 20
Different cases for Line Clipping
1. Both endpoints of the line lie with in the clipping area.
This means that the line is included completely in the
clipping area, so that the whole line must be drawn.
B
B
A
A
Clip
rectangle
21
2. One end point of the line lies with in the other outside the clipping area.
It is necessary to determine the intersection point of the line with the
bounding rectangle of the clipping area. Only a part of the line should be
D
drawn.
D’ D’
B B C
C
A
A
Clip
rectangle
22
3. Both end points are located outside the clipping area and the line do not intersect the
clipping area. In the case , the line lies completely outside the clipping area and can be
neglected for the scene.
4. Both endpoints are located outside the clipping area and the line intersect the clipping
area. The two intersection points of the line with the clipping are must be determined.
Only the part of the line between these two intersection points should be drawn.
D
D’
D’
B
H
C
B C
F H'
J
A H'
G’ A
J’ G’
Clip G I’
rectangle I 23
This algorithm divides a 2D space into 9 parts, of which only the
middle part is
visible.
This method is used:
i. To save a line segment.
ii. To discard line segment
iii. To divide the line according to window co- ordinate.
Cohen Sutherland performs line clipping in two phases:
Phase1: Find visibility of line.
Phase2: clip the line falling in category 3 (candidate for Clipping).
24
COHEN-SUTHERLAND LINE CLIPPING ALGORITHM
LEFT RIGHT
y
1010
ywmax 1001 1000
TOP
window
ywmin BOTTOM
0101 0100 0110
xwmin x
xwmax
26
Figure :Bit Code for Cohen- Sutherland clipping
• Each bit position in region code is used to indicate one of four relative
coordinate positions of points with respect to clip window: to the left, right,
top or bottom.
• By numbering the bit positions in the region code as 1 through 4 from right to
left, the coordinate regions are corrected with bit positions as bit 1: left bit 2:
right bit 3: below bit4: above
• A value of 1 in any bit position indicates that the point is in that relative
position.
• Otherwise the bit position is set to 0.
• If a point is within the clipping rectangle the region code is 0000.
• A point that is below and to the left of the rectangle has a region code of
0101.
• region-code bit values can be determined with following two steps.
(1) Calculate differences between endpoint coordinates and
clipping boundaries.
(2) Use the resultant sign bit of each difference calculation
to set the corresponding value in the region code.
bit 1 is the sign bit of x – xwmin
bit 2 is the sign bit of xwmax - x
bit 3 is the sign bit of y – ywmin
bit 4 is the sign bit of ywmax - y.
• Once we have established region codes for all line endpoints, we can
quickly determine which lines are completely inside the clip window and
which are clearly outside.
• Any lines that are completely contained within the window boundaries
have a region code of 0000 for both endpoints, and we accept these lines.
• Any lines that have a 1 in the same bit position in the region codes for
each endpoint are completely outside the clipping rectangle, and we
reject these lines.
COHEN-SUTHERLAND LINE CLIPPING
ALGORITHM
• Step 1
Compute the regioncodes for the two end points of the segment
• Step 2
Enter a loop, within the loop check to see if both the regioncodes are
zero. Then enter the segment into the display file, exit the loop & return
COHEN-SUTHERLAND LINE CLIPPING
ALGORITHM
• Step 3
If both the region codes are not zero then take Logical AND of both
the regioncodes & check for non-zero result. If so , then reject the line,
exit the loop & return.
• Step 4
If the result is 0, then subdivide the line segment from intersection
point of line segment & clipping boundary.
• Step 5
Repeat steps (2), (3) & (4) for each segment.
• Intersection points with a clipping boundary can be calculated using the
slope-intercept form of the line equation.
• For a line with endpoint coordinates (x1,y1) and (x2,y2)
the y coordinate of the intersection point with a vertical
boundary can be calculated as
y =y1 +m (x-x1)
where x value is set either to xwmin or to xwmax and
slope of line is calculated as m = (y2- y1) / (x2- x1)
the x coordinate of the intersection point with a horizontal
boundary can be calculated as
x= x1 +( y- y1) / m
with y set to either to ywmin or to ywmax.
Example:
An alternative way to process a line in category 3 is based on binary search. The line is
divided at its midpoints into two shorter line segments. Each line in a category three is
divided again into shorter segments and categorized. This bisection and categorization
process continue until each line segment that spans across a window boundary reaches
a threshold for line size and all other segments are either in a category 1 (visible) or in
category 2 (Not visible.)
35
Mid point subdivision algorithm
• An alternative way to process a line in category 3 is based on binary
search.
• The line is divided at its midpoints into two shorter line segments.
• Each line in a category three is divided again into shorter segments and
categorized.
• This bisection and categorization process continue until each line
segment that spans across a window boundary reaches a threshold for
line size and all other segments are either in a category 1 (visible) or in
category 2 (Not visible.)
Mid-point subdivision algorithm
The mid points coordinates are (x m , y m) of a line joining the points(X1,Y1)and
(X2,Y2) are given by:
X m= X1+X2/2 and
Q
Y M= Y1+Y2/2
I2 3
4
2
I1
5
8
7
6
P
Figure: illustrates how midpoint subdivision is used to zoom in onto the two Intersection points I1 and I2 with 10 bisection.
37
Comparison b/w Cohen-Sutherland and Mid-point subdivision clipping
Algorithm
38