0 ratings0% found this document useful (0 votes) 44 views43 pagesCH 3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
COMPUTER GRAPHICS
Disclaimer
This document is part of teaching materials for COMPUTER GRAPHICS
under the Pokhara University syllabus for Bachelors in computer and IT
Engineering (BE-CMP and BE-IT).This document does not cover all
aspect of learning COMPUTER GRAPHICS, nor are these be taken as
primary source of information. As the core textbooks and reference books
for learning the subject has already been specified and provided to the
students, students are encouraged to learn from the original sources
because this document cannot be used as a substitute for prescribed
textbooks...
Various text books as well as freely available material from internet were
consulted for preparing this document. Contents in This document are
copyrighted to the instructor and authors of original texts where
applicable.
©2021, MUKUNDA PAUDEL
1
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paude!
paudelmuku@[Link]
Chapter-2 Scan Conversion Algorithm
Output Primitive:
Initial phase of computer science> computing and textual form printing
Later on > graphics evolved> drawing of different objects like line, circle,
ellipse, rectangle, spline curve ete.
Hence, these graphical components are called output primitives.
Output primitives are the geometric structure such as straight-line segments and
polygon areas used to describe the shapes and color of the objects.
In case of the two-dimensional algorithm, we mostly deal with the line, circle,
ellipse etc. as they are the basic output primitives.
ga Point and a Straight
Pixel is a unit square area identified by the coordinate of its lower left
corner.
“ Each pixel on the display surface has a finite size depending on the screen
resolution & hence a pixel can’t represent a single mathematical number.
Origin of the reference coordinate system being located of the lower left
corner of the display surface.
The each pixel is accused by non-negative integer coordinate pair(x, y).
‘The x values start at the origin &increase from left to right along a scan line
& y values start at the bottom & increase upwards.
2
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
A
+ In the above diagram the coordinate of pixel A:0,0 .B:1.4 , C
+ A coding position (4. 2, 7. 2) is represented by C whereas (1.
represented by B.
+ In order to half a pixel on the sereen we need to round off the coordinate to a
nearest integer.
.7.C:4,7.
4.2) is
4 Line drawing is accomplished by calculating intermediate point coordinates
along the line path between two given end points.
> Screen pixel are referred with integer values, plotted positions may only
approximate the calculate coordinates, what is pixel which are intensified are
those which lie very close to the line path.
Ina high resolution system the adjacent pixels are so closely spread that the
approximated line pixels lie very close to actual line path and hence the
plotted lines appear to be much smooth-almost like straight line drown on
paper.
+ In low resolution system the same approximation technique causes to
display with stair step appearance that is not smooth.
Line Drawing Algorithm
‘The equation of a straight line is Y=-mX +b
Where m representing slope of the line and b as the y
intercept
Consider the two end points of a line segment are (x1, y1) & (x2, y2) then the
straight line can be written as
3
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
yo-V1
=x, +b
p2 ris L. ceseeeeeeen (1)
We can determine the volume for the slope ‘m’ & y intercept ‘b” with the
following calculation.
ye-ya
X21
b=y, —mxy,
>y, =mx,+b
For any given x interval Ax along a line, we can compute the corresponding y
interval Ay from the above equation
m= => Ay = mAx
oy
Ax
Similarly, we can obtained the x interval Ax corresponding to a specified Ay as
Ay =%
m
For a line with slope magnitude |\m\1
4
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
Ay can be set proportional to a small vertical deflection voltage with
the corresponding horizontal deflection voltage set proportional to Ax
calculate from the equation
For a line with
Then Ax= Ay and vertical & horizontal deflection voltages are equal.
In each case a smooth line with slope ‘m’ is generated between specified end point.
DDA Algorithm (Digital Differential Analyzer)
% DDA is
an-conversion line algorithm base on either Ax or Ay.
The process of representing continuous graphics objects as a collection of discrete
pixels is called scan conversion. The graphics objects are continuous. The pixels
used are discrete.
We sample the line at unit interval in one direction (x if Ax is greater than
Ay, otherwise in y direction) and determine the corresponding integer values
nearest the line path for the other coordinate
5
, BE-Software, BCA, [Link]. CSIT (TU, PU, PoU, KU, MWU, FWU)
For BE-Computer and Electronics, BE-Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
Algorithm:
Consider the line having positive slope
(812)
46)
The equation of the line is given,
y=mx+b. i)
m= (y2-yl)/(x2-x1) ween ii)
For any interval Ax, corresponding interval is given by Ay =[Link].
Case I: If |m| < 1, we sample at unit x interval
ie Ax=1
Xue = xt]
Then we compute each successive y-values, by setting Ay =m
Yeer= Yet m
The calculated y value must be rounded to the nearest integer
Case I: If jm|> 1, we sample at unit y-interval
ie Ay = 1 and compute each successive x-values,
yest=ye+1
Therefore, | = [Link], and Ax = I/m (since, m=Ay/Ax and Ay=1).
6
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, PoU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
Xmen = xk D/Am
The above equations are under the assumption that lines are to be processed from
left endpoints (x,, yx) to right endpoints (x+1, yer).
Advantages
DDA algorithm is a faster method for calculating pixel position then the
equation of a pixel position. Y = mx + b
% Simplex and fast method
Disadvantages
+ Accumulation of round of error is successive addition of the floating point
increments is used to find the pixel position but it take lot of time to compute
the pixel position.
+ Poor end point accuracy
Problem: 01:
igitized the line with end points (0, 0) and (4, 5) using DDA.
Solution:
Given,
Starting Point: P(x:, yi) = (0, 0)
Ending Point: Q (x2, y2) = (4, 5)
A
Now Slope m= — =
Ax x2 2y
= (5-0) / (5-0) = (5/4) = 1.25 i.e. Slope |m|>1,
From DDA algorithm we have
Yio =ykt |
Xie = xk + (1/m)
Henee,
7
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, PoU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
x iY; X-Plot Y-Plot (X,Y)
0 0 0 0 (0.0)
08 1 1 1 (1)
16 2 2 2 (2.2)
2.4 3 2 3 (2,3)
3.2 4 3 4 (3.4)
4 5 4 5 (4,5)
Problem: 02: Consider a
rasterize this line.
e from M (2, 1) to P (8, 3). Using DDA algorithm,
Solution:
Given,
Starting Point: M (x:, yi) = (2, 1)
Ending Point: P (x2, y2) = (8, 3)
Now,
AY _ Ye-¥1
Ax X24
Slope m: = (3-1) / (8-2) = (1/3) = 0.333 ive. slope |m|<1
From DDA algorithm we have
Xr =Xe+1
Yior=Ye+m
Hence,
8
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWU)Computer Graphics
BY: Mukunda Paudel
paudelmuku@[Link]
x Y X-Plot_| Y-Plot__| (X.Y)
2 1 2 1 21)
3 (2*4) | 1.33 (1+0.33) 3 1 (3.1)
4 (3+1) | 1.66 (1.33+0.33) 4 2 (4.2)
5 1.993 5 2 (5,2)
6 2,326 6 2 (6.2)
7 2.659 7 3 (7,3)
8 2.999 8 3 (8,3)
Q1. Consider a line from A (0, 0) to B (5, 5). Using DDA algorithm, rasterize this line.
Q2. Digitized the line with end points M (3, 7) and N (8, 3) using DDA.
Bresenham’s Line Drawing Algorithm
9
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, PoU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, PoU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
2
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, PoU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
2B
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, PoU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
ee
=z
fo)
=
a
fo]
o
S
14
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, PoU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWComputer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
16
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWComputer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWComputer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
19
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, PoU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWComputer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWComputer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
22
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWComputer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
OR, Puri= Pa +2 Xi +1
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWComputer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
24
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, PoU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
Pu +2* Xue +1
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWComputer Graphics BY: Mukunda Paudel
paudelmuku@[Link]Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
Filled Area Primitives
+ To provide more realistic view on the various objects filling of color is one
of the best way.
+ For filling a picture or object with color’s, we have 2 different algorithmic
approaches
1. Area filling scan line algorithm
2. Seed Fill Algorithm
> Sced fill Algorithm is further categorized in 2 different algorithm
2.1 Boundary Fill Algorithm
2.2Flood Fill Algorithm
Instead of Using filling algorithms such as Flood fill algorithm, Boundary fill
algorithm and scanline polygon fill algorithm, Programmatically we can color the
objects by Using inbuilt graphics functions such as floodfill(, settillstyle() we can
fill the object with color’s directly without using any filling algorithm in C
program.
Area filling sean line algorithm
+ Also called scan line polygon fill algorithm
% The basic scan-line algorithm is as follows:
1. find the intersections of the scan line with all edges of the polygon
2. Sort the intersections by increasing x coordinate (i.e. short in ascending
order of X coordinate)
3. Make pair of two x coordinate
4, Fill in all pixels between pairs of intersections that lie interior to the polygon
“ The scan-line polygon-filling algorithm involves
v the horizontal scanning of the polygon from its lowermost to its topmost
vertex,
37
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
¥ identifying which edges intersect the scan-line, and
¥ Finally drawing the interior horizontal lines with the specified fill color.
For example
10 Scan Line
2 8 MO M2 se ae
In this polygon ,
Scan line cuts the polygon edges in x= 2, x=6, x=12 and x=15.
Now sort the x cordinate in ascending order we get ~ [2,6,12,15]
‘Then make a pair of sorted pixel as (2,6) and (12,15)
Finally fill the two pair (2,6) and (12,15) with a color of your choice.
1. Special case: If the scan line intersects in any vertex point
Scan line y'
Scanline y
38
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
As shown in above polygon, scan line Y’ intersects in the vertex 2.
Similarly next scan line intersects in vertex 3.
In this case, if all the vertices that are connected in intersected vertex are in
same direction of scan line then repeat the vertex value and pair with other
intersecting point.
As M and N are two vertex that are connected with vertex 2. M and N
both are in same direction of scan line y’ (i.e. both M, N are in up direction
of scan line or on top). In this case repeat the value of vertex 2 during sorting
x direction value in ascending order and make two pair as (1, 2) and (2, 1).
Then fill color.
As>P and Q are two vertex that are connected with vertex 3. P and Q both
are in opposite direction of second scan line (i.e. P in above the scan line and
Q in below of scan line). In this case don’t repeat the value of vertex 3
during sorting x direction value in ascending order. Just take it once and
make pair with other intersecting point of polygon boundary. Then fill color
Boundary Fill Algorithm
4 Also called Edge Fill Algorithm
An alternative approach for filling an area is to start at a point inside the area
and “paint” the interior, point by point, out to the boundary
This is a particularly useful technique for filling areas with irregular borders
The algorithm makes the following assumptions
one interior pixel is known, and
Pixels in boundary are known
Boundary pixels are filled with a single color
¥ Ifthe boundary of some region is specified in a single color, we can fill the
interior of a region, pixel by pixel, until the boundary color is encountered.
Y Basically, a boundary-fill algorithm starts from an interior point (x, y) and
sets the neighboring points to the desired color
v This procedure continues until all pixels are processed up to the designated
boundary for the area.
Y Boundary fill algorithm is recursive in nature.
39
, BE-Software, BCA, [Link]. CSIT (TU, PU, PoU, KU, MWU, FWU)
For BE-Computer and Electronics, BE-Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
There are two methods for processing neighboring pixels from a current point
1. 4-Connected
¥ Also called 4-neighbouring points method
¥ These are the pixel positions that are right, left, above, and below the
CGD
Ceri)
4-connected approach
¥ In4 connected approach, we can fill an object in only 4 directions.
We have 4 possibilities for proceeding to next pixel from current
pixel.
Function for 4 connected approach:
Void boundary_fill (int x, int y, int fill_color, int boundary_color)
if ((getpixel(x, y) != boundary_color) && (getpixel(x, y) != fill_color))
{
delay (10);
putpixel(x, y, fill_color);
boundary fill(x +1, y, fill_color, boundary color);
boundary_fill(x - 1, y, fill_color, boundary_color);
boundary_fill(x, y + I, fill_color, boundary_color);
boundary_fill(x, y - 1, fill color, boundary. color);
40
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
2, 8-Connected
Y Also called 8-neighbouring points method
Y This method is used to fill more complex figures.
¥ These are the pixel positions that are right, left, above, below and four
diagonal points of the current pixel.
S-connected approach
¥ In8 connected approach, we can fill an object in 8 directions. We
have 8 possibilities for proceeding to next pixel from current pixel
Function for 8 connected approach:
void boundary_fill(int x, int y, int fill_color, int boundary_color)
(
{
if ((getpixel(x, y) != boundary color) && (getpixel(x, y) != fill_color))
t
delay(10);
putpixel(x, y, fill_color);
boundary fill(x * 1, y, fill_color, boundary_color);
boundary_fill(x , y+, fill color, boundary color);
boundary_fill(x+i, y +1, fill_color, boundary_color);
boundary_fill(x-1, y - 1, fill_color, boundary color);
boundary_fill(x-I, y, fill_color, boundary_color);
boundary_fill(x , y-I, fill color, boundary. color);
boundary_fill(x-1, y + 1, fill_color, boundary_color);
boundary_fill(x+1, y - 1, fill_color, boundary_color);
aa
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, PoU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
Algorithm
1, Start from an interior point.
2. If the current pixel is not already filled and if it is not an edge point, then set the
pixel with the fill color, and store its neighboring pixels (4 or 8-connected). Store
only neighboring pixel that is not already filled and is not an edge point.
3. Select the next pixel from the stack, and continue with step 2.
Flood Fill Algorithm
+ Sometimes it is required to fill in an area that is not defined within a single
color boundary.
In such cases we can fill areas by replacing a specified interior color instead
of searching for a boundary color.
This approach is called a flood-fill algorithm. Like boundary fill algorithm,
here we start with some seed and examine the neighbouring pixels.
However, here pixels are checked for a specified interior color instead of
boundary color and they are replaced by new color.
% Using either a 4-connected or 8-connected approach, we can step through pixel
positions until all interior point have been filled.
The following procedure illustrates the recursive method for filling 4-
connected region using flood-fill algorithm.
yoo
Procedure:
Void flood-fill(x, y, new-color, old-color)
{
if(getpixel (x,y) =
{
delay(10);
old-color)
putpixel (x, y, new-color)
flood-fill (x + 1, y, new-color, old -color);
flood-fill (x, y + 1, new -color, old -color);
42
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWU)Computer Graphics BY: Mukunda Paudel
paudelmuku@[Link]
flood-fill (x - 1, y, new -color, old -color);
flood-fill (x, y - 1, new -color, old-color);
3
}
In above program ‘getpivel()' function gives the color of .specified pixel and
‘putpixel()' function draws the pixel with specified color.
Alg im insight
1. We start from a specified interior pixel (x, y) and reassign all pixel values that
are currently set to a given interior color with the desired fill color.
2. If the area has more than one interior color, we can first reassign pixel values so
that all interior pixels have the same color.
3. Using cither 4-connected or 8-connected approach, we then step through pixel
positions until all interior pixels have been repainted,
Difference between Boundary fill and Flood Fill
Boundary Fill Algorithm Flood Fill Algorithm
1. Area filling is started inside a | Area filling is started from a point and
point with ina boundary region | it replaces the old color with the new
and fill the region with in the | color
specified color until it reaches
the the boundary.
2. Itis used in interactive packages | It is used when we cannot specify the
where we can specify the region | region boundary
boundary
3._Itis less time consuming Itconsumes more time
4. It searches for boundary. It searches for old color
5. Write algorithmic program Write algorithmic program yourself
ourself from abobe.. from abobe..
43
For BE-Computer and Electronics, BE-IT, BE-Software, BCA, [Link]. CSIT (TU, PU, POU, KU, MWU, FWU)