CG Unit Iv
CG Unit Iv
Computer Graphics
UNIT-IV
UNIT- 4 Contents
2D Viewing:
1. View Port Basics
3. Viewing Functions
Algorithms
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.
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
Department of CSE
Subject: Computer Graphics III-II CSE –A1
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
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
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
Department of CSE
Subject: Computer Graphics III-II CSE –A1
1. It calculates end-points very quickly and rejects and accepts lines quickly.
2. It can clip pictures much large than screen size.
Department of CSE
Subject: Computer Graphics III-II CSE –A1
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
Example:
Department of CSE
Subject: Computer Graphics III-II CSE –A1
Department of CSE
Subject: Computer Graphics III-II CSE –A1
If
pk = 0
then
{The line is parallel to the window}
If
qk < 0
then
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
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