CG r16 Unit 1 Notes
CG r16 Unit 1 Notes
OUTPUT PRIMITIVES
1.1 INTRODUCTION TO COMPUTER GRAPHICS
“Computer Graphics is a study of tools, methods and techniques used to create and display
images on a screen.”
Applications of Computer Graphics
(i) Computer-Aided Design
(ii) Presentation Graphics
(iii)Computer Art
(iv) Entertainment
(v) Education and Training
(vi) Visualization
(vii) Image Processing
(viii) Graphical User Interfaces
Use of Computer Graphics
(i) Data Presentation
(ii) Scientific Visualization
(iii) Cartography and surveying
(iv) Simulation and Animation
(v) Graphics arts and advertising
(vi) Condition Monitoring
(vii) Office automation and documentation systems
(viii) Electronic printing and publishing
(ix) Design and Analysis
(x) Manufacturing
(xi) Robotics
(xii) Video Games
1.2 TYPES OF GRAPHICS/CLASSIFICATION OF GRAPHICS
Depending on the type of dimensionality, computer graphics are classified into two types
(i) Two-Dimensional Graphics(2D)
(ii) Three-Dimensional Graphics (3D)
1
Depending on kind of picture, computer graphics are classified into two types
(i) Symbolic
(ii) Realistic
Depending on the type of interaction, computer graphics are classified into two types
(i) Interactive Computer Graphics
(ii) Non- Interactive Computer Graphics
Interactive Computer Graphics/Controllable/Active/Dynamic Graphics
It is a form of graphics in which observers have control over images i.e observers can create
and manipulate images with the help of input devices to get simulated images on the screen.
Examples of Interactive graphics include Video games etc.
Non- Interactive Computer Graphics/Static/Passive Graphics/Non-Controllable
It is a form of graphics in which observers have no control over images i.e observers can only
create an image and display images on the screen.
Examples of Non-Interactive graphics include Logo of Doordarshan news, movies/films,
Screen savers etc.
Figure: Logo of Doordarshan news
2
(4) With interactive graphics user can also control the animation by adjusting the speed, the
portion of the total scene in view, the geometric relationship of the objects in the scene to
one another, the amount of detail shown and so on.
(5) The interactive graphics provides tool called motion dynamics. With this tool user can
move and tumble objects with respect to a stationary observer, or he can make objects
stationary and the viewer moving around them.
1.3 OUTPUT PRIMITIVES
Graphical objects can be defined by a set of basic geometric structures such as straight line
segments, points, circles, rectangles, conic sections, quadric surfaces, polygons, spline
curves, poly-lines, ellipse etc.
These basic geometric structures are referred to as output primitives.
The output primitives included in syllabus are:
(i) Points
(ii) Lines
(iii)Circles
(iv) Ellipse
1.3.1 Points
Generally points are represented on a Cartesian coordinate system which is shown in the
figure given below:
y
(3, 3)
3
(2, 2)
2
1
(1, 1)
x
0 1 2 3
3
Points are represented as pixels in computer graphics which is shown in the figure given
below:
3
(2, 2)
2
(1, 1)
1
x
0 1 2 3
Figure: Representation of (1, 1) and (2, 2) on a raster screen.
Figure: Representation of group of points to form a character “H‟ on a raster screen.
1.3.2 Lines
Line drawing is accomplished by calculating intermediate positions along the line path
between two specified endpoint positions.
An output device is then directed to fill in these positions between the endpoints.
Digital devices display a straight line segment by plotting discrete points between the two
endpoints.
Screen locations are referenced with integer values, so plotted positions may only
approximate actual line positions between two specified endpoints.
A computed line position of (10.48, 20.51), for example, would be converted to pixel
position (10,21).
4
Thus rounding of coordinate values to integers causes lines to be displayed with a stair step
appearance ("the jaggies" or Aliasing), as represented in Figure given below.
Figure: Stair-step effect (jaggies) produced when a line is generated as a series of pixel positions.
Jaggies are stair like lines that appear where there should be smooth straight lines or curves.
Jaggies are a rigid, non-straight line or edge of an image or graphic.
Causes for jaggies:
(1) Low resolution
(2) Jaggies often occur when a bit-mapped image is converted to a different resolution.
How to reduce jaggies:
We can reduce jaggies by using different graphic techniques called
(1) Anti-Aliasing
(2) Smoothing
(3) Using high resolution graphical display systems.
Line Drawing Algorithms
There are various line drawing algorithms in computer graphics.
The two line drawing algorithms included in syllabus are
(i) DDA (Digital differential Analyzer)Algorithm
(ii) Bresenham‟s Line Drawing Algorithm
Every Line drawing Algorithm contains four sections
(i) Derivation
(ii) Pseudo code
(iii)Implementation Code
(iv) Solved Examples
5
Note: Implementation code for line drawing algorithms is not included in this material and it is
not mandatory to learn Implementation code in C-language.
DDA Algorithm (derivation)
The digital differential analyzer (DDA) algorithm is an incremental scan-conversion method.
DDA algorithm uses linear interpolation method.
Let us interpolate the given line whose coordinates are (x0, y0) and (xk+1, yk+1), which is
shown in the figure given below.
y (xk+1, yk+1)
yk+1
(x1, y1)
y1
(x0, y0)
y0
x
x0 x1 xk+1
The slope of the line whose endpoints are (x0, y0) and (xk+1, yk+1) is
=> y1=y0+1
We know that slope of the line segment (x0, y0) and (x1, y1) is equal to slope of the line
6
=> x1=(1/m) + x0
If (x0, y0) is initial point on the line, then the next point (x1, y1) on the line is given as
x1= (1/m) + x0
y1= y0+1
Similarly if (x1, y1) is initial point on the line, then the next point (x2, y2) on the line is
given as
x2= (1/m) + x1
y2= y1+1
Similarly if (xk, yk) is initial point on the line, then the next point (xk+1, yk+1) on the line is
given as
xk+1= (1/m) + xk
yk+1= yk+1
x1= x0 + 1
y1= y0+m
7
Similarly if (x1, y1) is initial point on the line, then the next point (x2, y2) on the line is
given as
x2= x1+1
y2= y1+m
Similarly if (xk, yk) is initial point on the line, then the next point (xk+1, yk+1) on the line is
given as
xk+1= xk + 1
yk+1= yk+m
Else
Step 5:
Xincrement=∆x/n
Yincrement=∆y/n
Step 6: Plot first point(ROUND(x), ROUND(y))
Step 7: Repeat the following steps „n‟ times starting from „1‟
x = x + Xincrement
y = y + Yincrement
Plot the point(ROUND(x), ROUND(y))
Step 8: Algorithm end
8
DDA Algorithm (SOLVED EXAMPLES)
(1) Draw a line starting at pixel (2, 3) and ending at pixel (12, 8) using DDA algorithm and plot
the points on a Cartesian graph.
(2) Plot a line with end points (2, 2) and (8, 10) using DDA algorithm and plot the points on a
Cartesian graph.
SOLUTION:
Step 1: (x1, y1) = (2, 2) and (x2, y2) = (8, 10)
Step 2: ∆x= 8-2=6, ∆y=10-2= 8
Step 3: Initialize x= 2, y= 2
Step 4: n= 8
Step 5: Xincrement=6/8= 0.75 , Yincrement=8/8= 1
Step 6: Plot first point(2, 2)
Step 7: Representing step 7 in a tabular form
Iteration/
X Y PLOT(ROUND(x), ROUND(y))
Loop
1 2.75 3 Plot(3,3)
2 3.50 4 Plot(4,4)
3 4.25 5 Plot(4,5)
4 5.00 6 Plot(5,6)
5 5.75 7 Plot(6,7)
6 6.50 8 Plot(7,8)
7 7.25 9 Plot(7,9)
8 8.00 10 Plot(8,10)
If (xk , yk) is the initial point on the line then the next possible pixels on the line may be
(xk+1 , yk ) and (xk+1, yk+1).
Let us consider a pixel between (xk+1 , yk ) and (xk+1, yk+1) as (xk+1 , y ).
Let us consider the distance between (xk+1 , yk ) and (xk+1 , y ) is dlower and the distance
between (xk+1 , y ) and (xk+1, yk+1) is dupper which is shown in the figure given below
Figure: Distance between pixel positions and the line y coordinate at sampling position xk + 1.
The equation of a line which is passing through a point (xk+1 , y ) and making an intercept of
b with y-axis is given as
y=m (xk +1) + b
10
Then,
dlower =y- yk
=m(xk +1)+b- yk
and dupper =( yk +1)-y
= yk +1-m(xk +1)-b
The difference between these two separations is
dlower - dupper =2m(xk +1) - 2yk +2b - 1
Solution:
Step1: x1=1, y1=2, x2=9, y2=6
Step2: ∆x= |x2-x1|=8
∆y= |y2-y1|=4
2∆y=8
2∆y -2∆x=-8
Step 3: x=x1 => x=1
y=y1 => y=2
12
Step 6: Representing steps 7 & 8 in tabular form and stopping algorithm
Y-AXIS
2
X-AXIS
1
1
1 2 3 4 5 6 7 8 9
FIG: Plotting a line with endpoints (1, 2) and (9,6) on a Cartesian graph
13
(iv) Bresenham algorithm can draw circles and curves with much more accuracy than DDA.
(v) DDA uses multiplication and division of equation but Bresenham algorithm uses
subtraction and addition only.
(vi) Due to the use of only addition, subtraction and bit shifting Bresenhams algorithm is
faster than DDA in producing the line.
(vii) Fixed point DDA algorithms are generally superior to Bresenhams algoritm on modern
computers.
Digital Differential Analyzer Bresenham’s Line Drawing Algorithm
Line Drawing Algorithm
Arithmetic DDA algorithm uses floating Bresenham‟s algorithm uses fixed
points i.e. Real Arithmetic. points i.e. Integer Arithmetic.
Operations DDA algorithm Bresenham‟s algorithm uses
uses multiplication and division in its only subtraction and addition in its
operations. operations.
Speed DDA algorithm is rather slowly than Bresenham‟s algorithm is faster than DDA
Bresenham‟s algorithm in line algorithm in line drawing because it
drawing because it uses real performs only addition and subtraction in
arithmetic (floating-point its calculation and uses only integer
operations). arithmetic so it runs significantly faster.
Accuracy DDA algorithm is not as accurate Bresenham‟s algorithm is more efficient
& and efficient as Bresenham‟s and much accurate than DDA algorithm.
Efficiency algorithm.
Drawing DDA algorithm can draw circles and Bresenham‟s algorithm can draw circles
curves but that are not as accurate as and curves with much more accuracy than
Bresenham‟s algorithm. DDA algorithm.
Round-Off DDA algorithm round off the Bresenham‟s algorithm does not round
coordinates to integer that is nearest off but takes the incremental value in its
to the line. operation.
Expensive DDA algorithm uses an enormous Bresenham‟s algorithm is less expensive
number of floating-point than DDA algorithm as it uses only addition
multiplications so it is expensive. and subtraction.
14
1.3.3 Circle Drawing Algorithms
The two famous circle generation algorithms are
(i) Bresenham‟s Circle algorithm
(ii) Midpoint Circle Algorithm
MID-POINT CIRCLE ALGORITHM (DERIVATION)
The equation of a circle whose center is (0, 0) and radius „r‟ can be given as
x2 + y2 = r2
The equation of a circle whose center is (xc , yc) and radius „r‟ can be given as
(x-xc)2 + (y-yc) 2 = r2
Let us define a circle function whose center is (0, 0) and radius „r‟ as
2 2 2
F(x, y) = x + y - r
Any point (x, y) on the boundary of the circle with radius „r‟ satisfies the equation f(x, y)=0.
If f(x,y)= < 0; point (x, y) is inside circle boundary
=0; point (x, y) is on circle boundary
>0; point (x, y) is outside circle boundary
Let us consider that the initial point on the circle is (xk , yk).
If (xk , yk) is the initial point on the circle, then the next possible pixels on the circle may be
(xk+1 , yk ) and (xk+1, yk-1) which is shown in the figure given below.
yk-1
yk-2
yk-3
x
xk+1 xk+2 xk+3 xk+4
xk
15
As the name of algorithm itself tells that, we have to find the midpoint of (xk+1 , yk ) and
2 2 2
Pk = xk + 1 + 2xk + (yk - 1/2) – r
2 2 2
Similarly, Pk+1 = (xk+1 + 1) + (yk+1 - 1/2) – r
If Pk >= 0 then the circle passes through the point (xk+1 , yk-1).
If Pk < 0 then the circle passes through the point (xk+1 , yk).
MID-POINT CIRCLE ALGORITHM (PSEUDO CODE)
Step 1: Input radius „r‟ and circle center (xc , yc ), and obtain the first point on the circumference
of a circle centered on the origin as (x , y ) = ( 0 , r )
Step 2: Calculate the initial value of the decision parameter as p=(5/4)-r
Step 3: Repeat steps 4 and 5 until the condition x ˂=y is false and stop algorithm.
Step 4: Plot (xc + x , yc + y)
Plot (xc - x , yc + y)
Plot (xc + x , yc - y)
Plot (xc - x , yc - y)
Plot (xc + y , yc + x)
Plot (xc - y , yc + x)
Plot (xc + y , yc - x)
Plot (xc - y , yc - x)
Step 5: Perform the following test:
if p < 0
p= p+2x+3
x= x +1, y = y
16
Otherwise
p= p + 2 ( x - y ) + 5
x= x + 1 , y= y – 1
MID-POINT CIRCLE ALGORITHM (SOLVED EXAMPLES)
(1) Plot a circle centered at (5, 5) having a radius of 5 units using mid-point circle algorithm.
Solution:
Step 1: r=5, xc=5, yc=5, x=0, y=5
Step 2: p = 1 – r = 1 – 5 = -4
Step 3: Representing Step 4 and step 5 in tabular form.
17
Advantages of Mid-Point Circle Algorithm
(1) The midpoint method is used for deriving efficient scan-conversion algorithms to draw
geometric curves on raster displays.
(2)The method is general and is used to transform the nonparametric equation f(x,y) = 0, which
describes the curve, into an algorithms that draws the curve.
Disadvantages of Mid-Point Circle Algorithm
(1) Time consumption is high.
(2) The distance between the pixels is not equal so we won‟t get smooth circle.
1.3.4 Ellipse Generation Algorithms
The famous ellipse generation algorithm is Midpoint Ellipse Generation algorithm.
MID-POINT ELLIPSE ALGORITHM (DERIVATION)
The equation of ellipse centered at (xc , yc ) with semi-major axis rx , and semi-minor axis ry
is given as
((x -xc)/rx)2 + ((y -yc)/ry)2 = 1
Using polar coordinates r and Ө, we can also describe the ellipse in standard position with
the parametric equations:
x = xc + rx cosӨ
y = yc + ry sinӨ
We define an ellipse function with (xc , yc) = (0,0) as
F(x,y)= ry2 x2 + rx2 y2 - rx2 ry2
Which has the following properties:
if f(x,y) = < 0; point (x, y) is inside ellipse boundary
=0; point (x, y) is on ellipse boundary
>0; point (x, y) is outside ellipse boundary
Let us consider one quarter of an ellipse. The curve is divided into two regions which is
shown in the figure given below.
18
In region I, the slope on the curve is greater than –1, while in region II less than –1.
In region I (dy/dx > –1)
A prediction function or decision parameter Pk in region 1 can be defined as
2 2 2 2 2 2
Pk = ry (xk +1) + rx (yk -1/2) - rx ry
19
Step 6: Calculate initial value of decision parameter in region 2 as
p = ry2 (x + ½)2 +rx2 (y-1)2 - rx2 ry2
where x,y are the values obtained in the last iteration of step 4.
Step 7: Repeat steps 8 and 9 until the condition y > 0 is false and stop algorithm.
Step 8: if p > 0
x = x, y = y - 1
p=p-2rx2 y + rx2
else
x = x + 1, y = y - 1
p=p+2 ry2 x-2rx2 y + rx2
Step 9: Plot (xc + x , yc + y)
Plot (xc - x , yc + y)
Plot (xc + x , yc - y)
Plot (xc - x , yc - y)
MID-POINT ELLIPSE ALGORITHM (SOLVED EXAMPLES)
(1) Plot an ellipse with rx=8 and ry=6 passing through origin. Plot an ellipse on Cartesian graph.
Solution:
Step 1: rx=8, ry=6, xc=0, yc= 0, x=0, y=6
Plot (0,6) (0,-6) (0,-6) (0,6)
Step 2: p = -332
Step 3: Repeating steps 3, 4 and 5 in tabular form
20
Step 6: x=7, y=3
P= -23
Step 7: Representing step 7, 8 and 9 in tabular form
(2) Plot an ellipse with semi major axis=4 and semi minor axis=2 passing through origin. Plot
the ellipse on a Cartesian graph.
Solution:
Step 1: rx=4, ry=2, xc=0, yc= 0, x=0, y=2
Plot (0,2) (0,-2) (0,-2) (0,2)
Step 2: p = -24
Step 3: Repeating steps 3, 4 and 5 in tabular form
21
1.4 ATTRIBUTES OF OUTPUT PRIMITIVES
A primitive or output primitive or graphic primitive is defined as any basic shape or
geometrical structure that can be drawn easily.
The different primitives are
(i) Point
(ii) Line
(iii) Circle
(iv) Ellipse
(v) Polygon
(vi) Curve etc
Every primitive is associated with attributes.
In general, any parameter that affects the way a primitive is to be displayed is referred to as
an attribute parameter.
There are two types of attribute parameters or attributes
(i) Basic or Fundamental Attributes
(ii) Special condition Attributes
Basic Attributes
They are the attributes that determine the fundamental characteristics of a primitive.
(or)
They are the attributes that control the basic display properties of primitives, without regard
for special situations.
Example: Color, style, type, size and width etc
Special Condition Attributes
They are the attributes that specify how the primitive is to be displayed under special
conditions.
Examples: Depth information for three-dimensional viewing and visibility or detectability
options for interactive object-selection programs.
Note: In our syllabus, we study only basic attributes of primitives.
1.4.1 POINT ATTRIBUTES
Basically, we can set two attributes for points:
(i) Color
(ii) Size.
22
Color components are set with RGB values or an index into a color table.
For a raster system, point size is an integer multiple of the pixel size, so that a large point is
displayed as a square block of pixels.
The following figures represent different sizes for a pixel or point.
Figure: Pixel of size=1 (standard size) Figure: Pixel of size=2
2 units
1 unit
1 unit 2 units
23
For raster implementations, a standard-width line is generated with single pixels at each
sample position which is shown in the figure given below.
Figure: Standard width-line (width of line is one)
We display a double-width line by generating a parallel line above the original line path
which is shown in the figure given below.
Figure: A line whose width is two (width=2)
24
A thick line on a raster screen is produced when the width of the line is greater than one.
(width > 1).
A thin line on a raster screen is produced when the width of the line is less than one. (width <
1).
(iii)Line Color
Color of a line can be set with RGB values or an index into a color table.
(iv) Line Cap
We can adjust the shape of the line ends to give them a better appearance by adding line
caps.
There are three types of line caps
(i) Butt Caps
(ii) Round caps
(iii) Projecting square caps
Butt Caps:
It is a line cap which has square ends that are perpendicular to the line path.
Round Cap:
It is a line cap obtained by adding a filled semicircle to each butt cap.
The circular arcs are centered at the middle of the thick line and have a diameter equal to the
line thickness.
Projecting square cap:
It is a cap where we simply extend the line and add butt caps that are positioned one-half of
the line width beyond the specified endpoints.
The following diagram shows different line caps
Thick lines drawn with (a) butt caps, (b) round caps, and (c) projecting square caps.
25
(v) Pen and Brush Strokes (or) Options
We can directly select different pen and brush styles.
Options in this category include shape, size, and pattern for the pen or brush.
Some example pen and brush shapes are given in Fig.
26
The characters in a selected font can also be displayed with assorted underlining styles (solid,
dotted, double), in boldface, in italics, and in OUTLINE or shadow styles.
(ii) Size:
We can adjust text size by scaling the overall dimensions (height and width) of characters or
by scaling only the character width.
Character size is specified by printers and compositors in points, where 1 point is 0.013837
inch (or approximately 1/72 inch).
Point measurements specify the size of the body of a character which is shown in the figure
given below.
The distance between the bottom line and the top line of the character body is the same for all
characters in a particular size and typeface, but the body width may vary.
Character height is defined as the distance between the baseline and the cap line of
characters.
Kerned characters, such as „f‟ and „j‟ in above Fig., typically extend beyond the character-
body limits, and letters with descenders („g‟, „j‟, „p‟, „q‟, „y‟) extend below the baseline.
(iii) Orientation:
The orientation for a character string can be set according to the direction of a character up
vector.
Text is then displayed so that the orientation of characters from baseline to capline is in the
direction of the up vector.
27
For example, with the direction of the up vector at 45̊ , text would be displayed as shown in
Figure: Direction of the up vector (a) controls the orientation of displayed text (b).
(iv) Text Path
It is useful in many applications to be able to arrange character strings vertically or
horizontally.
Examples of this are given in Fig. given below.
28
Figure: A text string displayed with the four text-path options: left, right, up, and down.
(v) Alignment:
This attribute specifies how text is to be displayed with respect to a reference position.
The following figure shows common alignment positions for horizontal and vertical text
labels.
29
1.4.4 CHARACTER (OR) MARKER ATTRIBUTES
We control the appearance of displayed characters with attributes such as font or typeface,
size, color, alignment and orientation.
(i) Alignment:
This attribute specifies how text is to be displayed with respect to a reference position.
For example, individual characters could be aligned according to the base lines or the
character centers.
The following figure illustrates typical character positions for horizontal and vertical
alignments.
30
1.5 TWO DIMENSIONAL GEOMETRIC TRANSFORMATIONS
Changes in orientation, size, and shape are accomplished with geometric transformations that
alter the coordinate descriptions of objects.
The basic geometric transformations are translation, rotation, and scaling.
Other transformations that are often applied to objects include reflection and shear.
The 2-D Geometric transformations are:
(i) Translation
(ii) Rotation
(iii)Reflection
(iv) Scaling
(v) Shear transformation
1.5.1 Translation
Translation is a rigid-body transformation that moves objects without deformation.
A translation is applied to an object by repositioning it along a straight-line path from one
coordinate location to another which is shown in the figure given below:
Figure: Translating a point from position P(x,y) to position P'(x',y') with translation
vector T(tx, ty).
We translate a two-dimensional point by adding translation distances „tx‟ and „ty‟ to the
original coordinate position (x, y) to move the point to a new position ( x ' , y') by using
the following formula:
The translation distance pair ( tx , ty) is called a translation vector or shift vector.
31
We can express the translation equation as a single matrix equation by using column vectors
to represent coordinate positions and the translation vector as:
This allows us to write the two-dimensional translation equations in the matrix form:
P' = P + T
1.5.2 Rotation
A two-dimensional rotation is applied to an object by repositioning it along a circular path in
the xy plane.
To generate a rotation, we specify a rotation angle θ and the position (x, y) of the rotation
point (or pivot point) about which the object is to be rotated.
We first determine the transformation equations for rotation of a point position P(x,y) when
the pivot point is at the coordinate origin.
The angular and coordinate relationships of the original and transformed point positions are
shown in Fig. given below
Figure: Rotation of a point from position (x, y) to position (x', y ') through an angle θ relative to
the coordinate origin. The original angular displacement of the point from the x axis is Φ.
In this figure, „r’ is the constant distance of the point from the origin, angle θ is the original
angular position of the point from the horizontal, and Φ is the rotation angle.
32
Using standard trigonometric identities, we can express the transformed coordinates in terms
of angles θ and Φ as
-------- (5-4)
The original coordinates of the point in polar coordinates are
-------- (5-5)
Substituting expressions 5-5 into 5-4, we obtain the transformation equations for rotating a
point at position (x, y) through an angle θ about the origin:
--------- (5-6)
With the column-vector representation for coordinate positions, we can write the rotation
equations in the matrix form:
P' = R . P
where the rotation matrix is
1.5.3 Reflection
A reflection is a transformation that produces a mirror image of an object.
The mirror image for a two-dimensional reflection is generated relative to an axis of
reflection by rotating the object 180º about the reflection axis.
Reflection about the line y = 0, the x axis, is accomplished with the transformation matrix
33
Reflection about the line x = 0, the y axis, is accomplished with the transformation matrix
Reflection about both x-axis and y-axis is accomplished with the transformation matrix
If we chose the reflection axis as the diagonal line y = x , the reflection matrix is
1.5.4 Scaling
A scaling transformation alters the size of an object.
This operation can be carried out for polygons by multiplying the coordinate values (x, y) of
each vertex by scaling factors sx, and sy, to produce the transformed coordinates (x', y') as:
x' = x . Sx y' = y . Sy
Scaling factor Sx, scales objects in the x direction, while Sy scales in the y direction.
The transformation equations for above equation can also be written in the matrix form as
Any positive numeric values can be assigned to the scaling factors Sx and Sy.
Values less than 1 reduce the size of objects; values greater than 1 produce an enlargement.
34
Specifying a value of 1 for both Sx, and Sy, leaves the size of objects unchanged.
When Sx, and Sy, are assigned the same value, a uniform scaling is produced that maintains
relative object proportions.
Unequal values for Sx, and Sy, result in a differential scaling that is often used in design
applications, where pictures are constructed from a few basic shapes that can be adjusted by
scaling and positioning transformations.
Scaling factors with values less than 1 move objects closer to the coordinate origin, while
values greater than 1 move coordinate positions farther from the origin.
1.5.5 Shear transformation
A transformation that distorts the shape of an object such that the transformed shape appears
as if the object were composed of internal layers that had been caused to slide over each
other is called a shear.
Two common shearing transformations are those that shift coordinate „x’ values and those
that shift „y‟ values.
An x-direction shear relative to the x axis is produced with the transformation matrix
35
SOLVED EXAMPLES ON 2-D TRANSFORMATIONS
(1) A line is denoted by its end points (0, 0) and (3, 5) in a 2D graphic system. Translate the
original line by 2 units in x-direction and 3 units in negative y-direction.
Solution 1: (using formulas)
If P(x, y) is translated to a new position P'(x ', y') then the translated coordinates are obtained
by using the following formula:
x' = x + tx , y' = y + ty
3 5 1
Step 2: Define Transformation Matrix (in this case Translation Matrix in row vector form)
M= 1 0 0
0 1 0
2 -3 1
F2x3 = 2 -3 1
5 2 1
If the original coordinates of a line segment are (0, 0) and (3, 5), then the translated
coordinates of the line segment are given as (2, -3) and (5, 2).
36
Solution 3: (using column vector)
Step 1: Define Point Matrix in column form
P= 0 3
0 5
1 1
Step 2: Define Transformation Matrix (in this case Translation Matrix in column vector form)
M=
1 0 2
0 1 -3
0 0 1
F3x2= 2 5
3
-3 2
1 1
If the original coordinates of a line segment are (0, 0) and (3, 5), then the translated
coordinates of the line segment are given as (2, -3) and (5, 2).
(2) Consider the triangle with coordinates (9, 2) (20, 2) and (15, 5). Translate triangle by 5.50 in
negative x-direction and 3.75 in y-direction.
(3) A line is denoted by its end points (0, 0) and (3, 5) in a 2D graphic system. Rotate the line by
600 about the origin.
(4) Consider a square whose coordinates are (0, 0) (1, 0) (1, 1) and (0, 1). Perform shear
transformation with shx=2.
(5) Consider a square whose coordinates are (0, 0) (1, 0) (1, 1) and (0, 1). Perform shear
transformation with shy=2.
(6) Consider a square whose coordinates are (0, 0) (1, 0) (1, 1) and (0, 1). Perform shear
transformation with shx=1/2 and yref = -1 in x-direction.
(7) Consider a square whose coordinates are (0, 0) (1, 0) (1, 1) and (0, 1). Perform shear
transformation with shy=1/2 and xref = -1 in y-direction.
37
(8) A line is denoted by its end points (0, 0) and (3, 5) in a 2D graphic system. Scale the line by a
factor of 3.0 in x-direction and 2.0 in y-direction w.r.t (3,4).
(9) Reflect the point P(3,2) about (i) x-axis (ii) y-axis (iii) origin
1.6 TWO-DIMENSIONAL VIEWING
1.6.1 WINDOW AND VIEWPORT
A world-coordinate area selected for display is called a window.
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.
In computer graphics terminology, the term window originally referred to an area of a picture that is
selected for viewing.
Often, windows and viewports are rectangles in standard position, with the rectangle edges parallel to
the coordinate axes.
Other window or viewport geometries, such as general polygon shapes and circles, are used in some
applications, but these shapes take longer to process.
In general, the mapping of a part of a world-coordinate scene to device coordinates is referred to as a
viewing transformation.
Sometimes the two-dimensional viewing transformation is simply referred to as the window-to-
viewport transformation or the windowing transformation.
The following figure illustrates the mapping of a picture section that falls within a rectangular
window onto a designated rectangular viewport.
Figure: A viewing transformation using standard rectangles for the window and viewport.
38
1.6.2 TWO-DIMENSIONAL VIEWING PIPELINE
Viewing transformation involves several steps. They are
(i) First, we construct the scene in world coordinates using the output primitives and attributes.
(ii) 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.
(iii) Once the viewing reference frame is established, we can transform descriptions in world
coordinates to viewing coordinates.
(iv) 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.
(v) 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.
The process of viewing transformation is described in the figure given below.
39
In the following sections, we consider algorithms for clipping the following primitive types.
(i) Point clipping
(ii) Line Clipping (Straight-line segments)
(iii) Curve Clipping
(iv) Area Clipping or Polygon Clipping
(v) Text Clipping
1.7.1 Point clipping
The region against which an object is to be clipped is called a clipping window.
Let us consider a rectangular window or clipping window, where the edges of the clipping window
are (XWmin, XWmax, YWmin, YWmax) which is shown in the figure given below:
(XWmax , YWmax)
YWmax
YWmin
(XWmin , YWmin)
XWmin XWmax
The coordinates of the clipping window can be either the world-coordinate window boundaries or
viewport boundaries.
Assuming that the clip window is a rectangle in standard position, we save a point P = (X, Y) for
display if the following inequalities are satisfied:
XWmin<=X<=XWmax
YWmin<=Y<=YWmax
If any one of these four inequalities is not satisfied, the point is clipped (not saved for display).
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
modeled with particles (points) distributed in some region of the scene.
1.7.2 Line Clipping
The various Line clipping algorithms are
(i) Cohen-Sutherland Line Clipping
(ii) Cyrus-Beck Line Clipping
40
(iii) Liang-Barsky Line Clipping
(iv) Nicholl-Lee-Nicholl Line Clipping
Note: Liang-Barsky Line Clipping and Nicholl-Lee-Nicholl Line Clipping are not included in syllabus.
Cohen-Sutherland Line Clipping
This is one of the oldest and most popular line-clipping procedures.
Every line endpoint in a picture is assigned a four-digit binary code, called a region code that
identifies the location of the point relative to the boundaries of the clipping rectangle which is shown
in the figure given below.
Figure: Binary region codes assigned to line endpoints according to relative position with respect to 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 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: left
bit 2: right
bit 3: below
bit 4: above
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 trivially accept these lines.
A method that can be used to test lines for total clipping is to perform the LOGICAL AND operation
with both region codes. If the result is not 0000, the line is completely outside the clipping region.
Lines that cannot be identified as completely inside or completely outside a clip window by these
tests are checked for intersection with the window boundaries.
41
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) they coordinate of the intersection point with
a vertical boundary can be obtained with the calculation
y= y1+m(x- x1)
where the x value is set either to xwmin or to xwmax , and the slope of the line is calculated as
m = (y2- y1)/( x2-x1)
Similarly, if we are looking for the intersection with a horizontal boundary, the x coordinate can be
calculated as
X= x1 + (y- y1)/m
Where y set either to ywmin or to ywmax
1.7.3 Curve Clipping
Curve-clipping procedures will involve nonlinear equations, however, and this requires more
processing than for objects with linear boundaries.
The bounding rectangle for a circle or other curved object can be used first to test for overlap with a
rectangular clip window.
If the bounding rectangle for the object is completely inside the window, we save the object.
If the rectangle is determined to be completely outside the window, we discard the object.
In either case, there is no further computation necessary.
But if the bounding rectangle test fails, we can look for other computation-saving approaches.
For a circle, we can use the coordinate extents of individual quadrants and then octants for
preliminary testing before calculating curve-window intersections.
For an ellipse, we can test the coordinate extents of individual quadrants.
The following Figure illustrates circle clipping against a rectangular window.
42
(i) Sutherland-Hodgeman Polygon Clipping
(ii) Weiler-Atherton Polygon Clipping
Note: Weiler-Atherton Polygon Clipping is not included in syllabus.
Let us discuss these clipping algorithms in detail.
Sutherland-Hodgeman Polygon Clipping
We can correctly clip a polygon by processing the polygon boundary as a whole against each window
edge.
This could be accomplished by processing all polygon vertices against each clip rectangle boundary
in turn.
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 be successively passed to a right boundary clipper, a bottom
boundary clipper, and a top boundary clipper, as shown in the Figure given below.
43
Figure: Successive processing of pairs of polygon vertices against the left window boundary.
Once all vertices have been processed for one clip window boundary, the output list of vertices is
clipped against the next window boundary.
We illustrate this method by processing the area which is shown in the figure given below against the
left window boundary.
44
Figure: Clipping a polygon against the left boundary of a window starting with vertex 1.
Vertices 1 and 2 are found to be on the outside of the boundary, and they are not saved.
Moving along to vertex 3 from vertex 2, we calculate the intersection point 1' and save both the
intersection point 1' and vertex 3.
Vertices 3 and 4 are determined to be inside, and they also are saved.
Vertices 4 and 5 are determined to be inside, and they also are saved.
Moving along to vertex 6 from vertex 5, we calculate the intersection point 5' and save both the
intersection point 5' and vertex 5.
The sixth and final vertex is outside, and they are not saved.
Using the five saved points, we would repeat the process for the next window boundary.
1.7.5 Text Clipping
There are several techniques that can be used to provide text clipping in a graphics package.
They are
(i) All-or-none string-clipping strategy
(ii) All-or-none character-clipping strategy
(iii) Clipping components of individual characters
Let us discuss these strategies in detail.
All-or-none string-clipping strategy
The simplest method for processing character strings relative to a window boundary is to use the all-
or-none string-clipping strategy shown in Fig given below.
Figure: Text clipping using a bounding rectangle about the entire string.
If all of the string is inside a clip window, we keep it. Otherwise, the string is discarded.
This procedure is implemented by considering a bounding rectangle around the text pattern.
The boundary positions of the rectangle are then compared to the window boundaries, and the string
is rejected if there is any overlap.
All-or-none character-clipping strategy
An alternative to rejecting an entire character string that overlaps a window boundary is to use the all-
or-none character-clipping strategy.
45
Here we discard only those characters that are not completely inside the window which is shown in
the figure given below.
Figure: Text clipping using a bounding rectangle about individual characters.
In this case, the boundary limits of individual characters are compared to the window.
Any character that either overlaps or is outside a window boundary is clipped.
Clipping components of individual characters
A final method for handling text clipping is to clip the components of individual characters.
If an individual character overlaps a clip window boundary, we clip off the parts of the character that
are outside the window which is shown in the figure given below.
46