0% found this document useful (0 votes)
33 views19 pages

CG Unit Iv

The document covers the fundamentals of 2D viewing in computer graphics, including viewport basics, window-to-viewport coordinate transformations, and various clipping algorithms such as Cohen-Sutherland and Liang-Barsky. It explains the concepts of viewing transformations, clipping operations, and the processes involved in mapping world coordinates to device coordinates. Additionally, it discusses polygon clipping using the Sutherland-Hodgeman algorithm, highlighting the handling of both convex and concave polygons.
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)
33 views19 pages

CG Unit Iv

The document covers the fundamentals of 2D viewing in computer graphics, including viewport basics, window-to-viewport coordinate transformations, and various clipping algorithms such as Cohen-Sutherland and Liang-Barsky. It explains the concepts of viewing transformations, clipping operations, and the processes involved in mapping world coordinates to device coordinates. Additionally, it discusses polygon clipping using the Sutherland-Hodgeman algorithm, highlighting the handling of both convex and concave polygons.
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/ 19

Subject: Computer Graphics III-II CSE –A1

Computer Graphics
UNIT-IV

UNIT- 4 Contents
2D Viewing:
1. View Port Basics

2. Window to view port Co-ordinate Transformations

3. Viewing Functions

4. Clipping Operations and Cohen-Sutherland Line clipping

Algorithms

5. Liang-Barsky line clipping algorithm and Sutherland-Hodge man

Polygon Clipping algorithm

2D Viewing Basics:
Typically, a graphics package allows a user to specify which part of a defined picture is to be
display& and where that part is to be placed on the display device.
For a two-dimensional picture, a view is selected by specifying a subarea of the total picture
area. A user can select a single area for display, or several areas could be selected for
simultaneous display or for an animated panning sequence across a scene.
Transformations from world to device coordinates involve translation, rotation, and scaling
operations, as well as procedures for deleting those parts of the picture that are outside the
limits of a selected display area.
Viewing Pipeline:
Window: A world-coordinate area selected for display is called a window. In Computer
Graphics terminology, the term window originally referred to an area of a picture that is
selected for viewing.
Viewport: An area on a display device to which a window is mapped is called a viewport.
The window defines what is to be viewed; the viewport defines where it is to be displayed.
Windows and viewports are rectangles in standard position, with the rectangle edges parallel
to the coordinate axes.

Viewing Transformation: The mapping of a part of a world-coordinate scene to device


coordinates is referred to as a viewing transformation. The two-dimensional viewing
transformation is simply referred to as the window-to-viewport transformation or the
windowing transformation.

we carry out the viewing transformation in several steps, as indicated in Fig. 6-2.

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Figure 6-3 i1lustratt.s a rotated viewing-coordinate reference frame and the mapping to
normalized coordinates.

Viewing Coordinate Reference Frame: This coordinate system provides the reference
frame for specifying the world coordinate window.
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
specifying world vector V that defines the viewing y, direction. Vector V is called the view up
vector. Vector V is called the view up vector.

Given V, we can calculate the components of unit vectors v = (vx,vy) and u = (ux,uy) for the
viewing y0, and x0, 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. The composite two-dimensional
transformation to convert world coordinates to viewing coordinate is

Figure 6-4 illustrates the steps in this coordinate transformation.

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Window to Viewport Coordinate Transformation:


Once object descriptions have been transferred to the viewing reference frame, we choose the
window extents in viewing coordinates and select the viewport limits in normalized coordinates
Figure 6.3 illustrates this one.
Object descriptions are then transferred to normalized device coordinates. We do this using a
transformation that maintains the same relative placement of objects in normalized space as
they had in viewing coordinates.
If a coordinate position is at the centre of the viewing window, for instance, it will be displayed
at the centre of the viewport.

Figure 6-5 illustrates the window-to-viewport mapping. A point at position (xw,yw) in the
window 1s mapped into position (xv,yv) in the associated viewport.

To maintain the same relative placement in the viewport as in the window, we require that

Department of CSE
Subject: Computer Graphics III-II CSE –A1

This conversion is performed with the following sequence of transformations:

From normalized coordinates, object descriptions are mapped to the various display devices.
Any number of output devices can be open in a particular application, and another window-to-
viewport transformation can be performed for each open output device. This mapping, called
the workstation transformation. This was illustrated in Figure 6.6.

Department of CSE
Subject: Computer Graphics III-II CSE –A1

TWO-DIMENSIONAL VIEWING FUNCTIONS

Example:

Department of CSE
Subject: Computer Graphics III-II CSE –A1

CLIPPING OPERATIONS:
Generally, any procedure that identifies those portions of a picture that are either inside or
outside of a specified region of space is referred to as a clipping algorithm, or simply
clipping.
The region against which an object is to clipped is called a clip window
Applications of Clipping
• extracting part of a defined scene for viewing;
• identifying visible surfaces in three-dimensional views;
• antialiasing line segments or object boundaries;
• creating objects using solid-modeling procedures;
• displaying a multiwindow environment; and
• drawing and painting operations that allow parts of a picture to be selected for
copying, moving, erasing, or duplicating
We consider algorithms for clipping the following primitive types
1. Point Clipping
2. Line Clipping (straight-line segments)
3. Area Clipping (polygons)
4. Curve Clipping
5. Text Clipping
Line and polygon clipping routines are standard components of graphics packages, but many
packages accommodate curved objects, particularly spline curves and conics, such as circles
and ellipses.
Another way to handle curved objects is to approximate them with straight-line segments and
apply the line- or polygon clipping procedure.

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Although point clipping is applied less often than line or polygon clipping, some applications
may require a point clipping procedure. For example, point clipping can be applied to scenes
involving explosions or sea foam that are modelled with particles (points) distributed in some
region of the scene.

LINE CLIPPING:
A line clipping procedure involves several parts.
1. First, we can test a given line segment to determine whether it lies completely inside
the clipping window.
2. If it does not, we try to determine whether it lies completely outside the window.
3. Finally, if we cannot identify a line as completely inside or completely outside, we must
perform intersection calculations with one or more clipping boundaries.

Department of CSE
Subject: Computer Graphics III-II CSE –A1

COHEN -SUTHERLAND OUTCODE LINE CLIPPING ALGORITHM


Key Point: The Cohen-Sutherland Line Clipping Algorithm is a computer graphics technique
used to clip lines to a rectangular viewport. It assigns region codes to endpoints of the line,
performs logical operations, and efficiently determines whether the line is entirely inside,
outside, or intersecting the viewport.
Generally, the method speeds up the processing of line segments by performing initial tests
that reduce the number of intersections that must he calculated.
Every line end-point in a picture is assigned a four-digit binary code, called a region code
Regions are set up in reference to the boundaries as shown in Fig. 6-8.
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 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 can be correlated with the bit positions as
bit 1 is set at left of the window
bit 2 is set at right of the window
bit 3 is set at below of the window
bit 4: is set at above of the window

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Following figure show lines of various types

Line AB is the visible case


Line OP is an invisible case
Line PQ is an invisible line
Line IJ are clipping candidates
Line MN are clipping candidate
Line CD are clipping candidate

Advantage of Cohen Sutherland Line Clipping:

1. It calculates end-points very quickly and rejects and accepts lines quickly.
2. It can clip pictures much large than screen size.

Algorithm of Cohen Sutherland Line Clipping:

Step1: Calculate positions of both endpoints of the line

Step2: Perform OR operation on both of these end-points

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Step3: If the OR operation gives 0000


Then
line is considered to be visible
else
Perform AND operation on both endpoints
If And ≠ 0000
then the line is invisible
else
And=0000
Line is considered the clipped case.

Step4: If a line is clipped case, find an intersection with boundaries of the window
m=(y2-y1 )(x2-x1)

(a) If bit 1 is "1" line intersects with left boundary of rectangle window
y3=y1+m(X-X1)
where X = Xwmin
where Xwmin is the minimum value of X co-ordinate of window

(b) If bit 2 is "1" line intersect with right boundary


y3=y1+m(X-X1)
where X = Xwmax
where X more is maximum value of X co-ordinate of the window

(c) If bit 3 is "1" line intersects with bottom boundary


X3=X1+(y-y1)/m
where y = ywmin
ywmin is the minimum value of Y co-ordinate of the window

(d) If bit 4 is "1" line intersects with the top boundary


X3=X1+(y-y1)/m
where y = ywmax
ywmax is the maximum value of Y co-ordinate of the window

Example:

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Department of CSE
Subject: Computer Graphics III-II CSE –A1

LIANG-BARSKY LINE CLIPPING ALGORITHM

If
pk = 0
then
{The line is parallel to the window}
If
qk < 0
then

If we have non zero value of pk –


If
pk < 0
then
t1 = max (0, qk / pk)
If
pk > 0
then
Department of CSE
Subject: Computer Graphics III-II CSE –A1

t2 = min (1, qk / pk)

If t1 ˃ t2 then
Line is outside
Else
Line is considered as clipped case
Intersection point is (x,y), where x= x1+ t (∆𝒙), y=y1+t∆𝒚
Algorithm of Liang-Barsky Line Clipping (or Implementation)

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Example:
Let a rectangular window size with (5, 9). The points of the line are (4, 12) and (8, 8). Use the
Liang- Barsky algorithm to clip the line and find the intersection point.

Solution:
We have,
The initial point of the line (p1) = (4, 12)
The ending point of the line (p2) = (8, 8)
x1 = 4, x2 = 8
y1 = 12, y2 = 8
xwmin = 5, xwmax = 9
ywmin = 5, ywmax = 9
Step 1: We have to calculate the value of dx and dy
dx = x2– x1= 8-4 = 4
dy = y2– y1= 8-12 = -4
Step 2: Now, we will calculate-
p1 = -4 q1 = 4-5 = -1
p2 = 4 q2 = 9-4 = 5
p3 = 4 q3 = 12-5 = 7
p4 = -4 q4 = 9-12 = -3
Step 3: Now we will calculate t1 value–
If p1, p4 < 0
Then t1 =max (0, qk /pk)
=max (0, q1 /p1, q4 /p4)
=max (0, 1/4, 3/4)
t1 = 3/4
If p2, p3 > 0
Then t2 = min (1, qk /pk)
= min (1, q2 /p2, q3 /p3)

Department of CSE
Subject: Computer Graphics III-II CSE –A1

= min (1, 5/4, 7/4)


t2 = 1
Step 4: Now, we have to calculate the intersection point.
x = x1 + t1. ?x= 4+ 3/4 * 4 = 7
y = y1 + t1. ?y= 12+ 3/4 *(-4) = 9
The coordinates intersection point = (7, 9)

POLYGON CLIPPING: SUTHERLAND HODGEMAN POLYGON LINE CLIPPING


ALGORITHM
Key Point: It processes the polygon edges one by one against each boundary of the clipping
window and outputs the vertices of the clipped polygon.
We can correctly clip a polygon by processing the polygon boundary as a whole against each
window edge.
Beginning with the initial set of polygon vertices, we could first clip the polygon against the
left rectangle boundary to produce a new sequence of vertices. The new set of vertices could
then successively passed to a right boundary clipper, a bottom boundary clipper, and a top
boundary clipper, as in Fig. 6-19. At each step, a new sequence of output vertices is generated
and passed to the next window boundary clipper.

Department of CSE
Subject: Computer Graphics III-II CSE –A1

There are four possible cases when processing vertices in sequence around the perimeter of a
polygon. As each pair of adjacent polygon vertices is passed to a window boundary clipper, we
make the following tests:

(1) If the first vertex is outside the window boundary and the second vertex is inside, both the
intersection point of the polygon edge with the window boundary and the second vertex are
added to the output vertex list.
(2) If both input vertices are inside the window boundary, only the second vertex is added to
the output vertex list.
(3) li the first vertex is inside the window boundary and the second vertex is outside, only the
edge intersection with the window boundary is added to the output vertex list.
(4) If both input vertices are outside the window boundary, nothing is added to the output list.
These four cases are illustrated in Fig. 6-20 for successive pairs of polygon vertices. Once all
vertices have been processed for one clip window boundary, the output 11st of vertices is
clipped against the next window boundary.

Convex polygons are correctly clipped by the Sutherland-Hodgeman algorithm, but concave
polygons may be displayed with extraneous lines, as demonstrated in Fig. 6-24. This occurs
when the clipped polygon should have two or more separate sections. But since there is only
Department of CSE
Subject: Computer Graphics III-II CSE –A1

one output vertex list, the last vertex in the list is always joined to the first vertex. There are
several things we could do to correctly display concave polygons. For one, we could split the
concave polygon into two or more convex polygons and process each convex polygon
separately. Another possibility is to modify the Sutherland-Hodgeman approach to check the
final vertex list for multiple vertex points along any clip window boundary and correctly join
pairs of vertices.

Algorithm Steps:

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Department of CSE

You might also like