Unit 3
Unit 3
Unit 3
A
COMPUTER GRAPHICS
SEMESTER -VI, ACADEMIC YEAR 2020 - 21
UNIT - II
SCAN CONVERSION
Definition
It is a process of representing graphics objects a collection of pixels. The graphics objects
are continuous. The pixels used are discrete. Each pixel can have either on or off state.
The circuitry of the video display device of the computer is capable of converting binary
values (0, 1) into a pixel on and pixel off information. 0 is represented bypixel off. 1 is
represented using pixel on. Using this ability graphics computer represent picture having
discrete dots
Any model of graphics can be reproduced with a dense matrix of dots or points. Most
human beings think graphics objects as points, lines, circles, ellipses. For generating graphical
object, many algorithms have been developed.
Page 22 of 82
STUDY MATERIAL FOR B.C.A
COMPUTER GRAPHICS
SEMESTER -VI, ACADEMIC YEAR 2020 - 21
m=
yi+1-yi=∆y.......................equation 3
yi+1-xi=∆x......................equation 4
yi+1=yi+∆y
∆y=m∆x
yi+1=yi+m∆x
∆x=∆y/m
xi+1=xi+∆x
xi+1=xi+∆y/m
Case1: When |m|<1 then (assume that x1<x2)
x= x1,y=y1 set ∆x=1
yi+1=y1+m, x=x+1
Until x = x2
Case2: When |m|>1 then (assume that y1<y2)
x= x1,y=y1 set ∆y=1
xi+1= , y=y+1
Until y → y2
Advantage:
1. It is a faster method than method of using direct use of line equation.
2. This method does not use multiplication theorem.
3. It allows us to detect the change in the value of x and y ,so plotting of same point twice
is not possible.
4. This method gives overflow indication when a point is repositioned.
5. It is an easy method because each step involves just two additions.
Disadvantage:
1. It involves floating point additions rounding off is done. Accumulations of round off
error cause accumulation of error.
2. Rounding off operations and floating point operations consumes a lot of time.
3. It is more suitable for generating line using the software. But it is less suitedfor
hardware implementation.
DDA Algorithm:
Step1: Start Algorithm
Step2: Declare x1,y1,x2,y2,dx,dy,x,y as integer variables.
Step3: Enter value of x1,y1,x2,y2.
Step4: Calculate dx = x2-x1
Step5: Calculate dy = y2-y1
Page 23 of 82
STUDY MATERIAL FOR B.C.A
COMPUTER GRAPHICS
SEMESTER -VI, ACADEMIC YEAR 2020 - 21
m=
Page 24 of 82
STUDY MATERIAL FOR B.C.A
COMPUTER GRAPHICS
SEMESTER -VI, ACADEMIC YEAR 2020 - 21
Page 25 of 82
STUDY MATERIAL FOR B.C.A
COMPUTER GRAPHICS
SEMESTER -VI, ACADEMIC YEAR 2020 - 21
3. If S is chosen
We have xi+1=xi+1 and yi+1=yi
If T is chosen
We have xi+1=xi+1 and yi+1=yi+1
= 2y - 2yi -1
Bresenham's Line Algorithm:
Step1: Start Algorithm
Step2: Declare variable x1,x2,y1,y2,d,i1,i2,dx,dy
Step3: Enter value of x1,y1,x2,y2
Where x1,y1are coordinates of starting point
And x2,y2 are coordinates of Ending point
Step4: Calculate dx = x2-x1
Calculate dy = y2-y1
Calculate i1=2*dy
Page 26 of 82
STUDY MATERIAL FOR B.C.A
COMPUTER GRAPHICS
SEMESTER -VI, ACADEMIC YEAR 2020 - 21
Calculate i2=2*(dy-dx)
Calculate d=i1-dx
Step5: Consider (x, y) as starting point and xendas maximum possible value of x.
If dx < 0
Then x = x2
y = y2
xend=x1
If dx > 0
Then x = x1
y = y1
xend=x2
Step6: Generate point at (x,y)coordinates.
Step7: Check if whole line is generated.
If x > = xend
Stop.
Step8: Calculate co-ordinates of the next pixel
If d < 0
Thend = d + i1
If d ≥ 0
Then d = d + i2
Increment y = y + 1
Step9: Increment x = x + 1
Step10: Draw a point of latest (x, y) coordinates
Step11: Go to step 7
Step12: End of Algorithm
Example: Starting and Ending position of the line are (1, 1) and (8, 5). Find intermediate points.
Solution: x1=1
y1=1
x2=8
y2=5
dx= x2-x1=8-1=7
dy=y2-y1=5-1=4
I1=2* ∆y=2*4=8
I2=2*(∆y-∆x)=2*(4-7)=-6
d = I1-∆x=8-7=1
Page 27 of 82
STUDY MATERIAL FOR B.C.A
COMPUTER GRAPHICS
SEMESTER -VI, ACADEMIC YEAR 2020 - 21
x y d=d+I1 or I2
1 1 d+I2=1+(-6)=-5
2 2 d+I1=-5+8=3
3 2 d+I2=3+(-6)=-3
4 3 d+I1=-3+8=5
5 3 d+I2=5+(-6)=-1
6 4 d+I1=-1+8=7
7 4 d+I2=7+(-6)=1
8 5
Advantage:
1. It involves only integer arithmetic, so it is simple.
2. It avoids the generation of duplicate points.
3. It can be implemented using hardware because it does not use multiplicationand
division.
4. It is faster as compared to DDA (Digital Differential Analyzer) because it does not involve
floating point calculations like DDA Algorithm.
Disadvantage:
➢ This algorithm is meant for basic line drawing only Initializing is not a part of
Bresenham's line algorithm. So to draw smooth lines, you should want to look into a
different algorithm.
Page 28 of 82
STUDY MATERIAL FOR B.C.A
COMPUTER GRAPHICS
SEMESTER -VI, ACADEMIC YEAR 2020 - 21
1. DDA Algorithm use floating point, i.e., 1. Bresenham's Line Algorithm use fixed
RealArithmetic. point, i.e., Integer Arithmetic
3. DDA Algorithm is slowly than Bresenham'sLine 3. Bresenham's Algorithm is faster than DDA
Algorithm in line drawing because ituses real Algorithm in line because itinvolves only
arithmetic (Floating Point operation) addition & subtraction in its calculation and
uses only integer arithmetic.
4. DDA Algorithm is not accurate and efficient as 4. Bresenham's Line Algorithm is more
Bresenham's Line Algorithm. accurate and efficient at DDA Algorithm.
5.DDA Algorithm can draw circle and curves but 5. Bresenham's Line Algorithm can draw
are not accurate as Bresenham's Line Algorithm circle and curves with more accurate
than DDA Algorithm.
Polynomial Method:
The first method defines a circle with the second-order polynomial equation as shown in fig:
y2=r2-x2
Where x = the x coordinate
y = the y coordinate
r = the circle radius
With the method, each x coordinate in the sector, from 90° to 45°, is found by stepping x from 0
Page 29 of 82
STUDY MATERIAL FOR B.C.A
COMPUTER GRAPHICS
SEMESTER -VI, ACADEMIC YEAR 2020 - 21
Algorithm:
Step1: Set the initial variables
r = circle radius
(h, k) = coordinates of circle center
x=o
I = step size
xend=
Step2: Test to determine whether the entire circle has been scan-converted.
If x >xend then stop.
Step3: Compute y =
Step4: Plot the eight points found by symmetry concerning the center (h, k) at the current (x, y)
coordinates.
Plot (x + h, y +k) Plot (-x + h, -y + k)
Plot (y + h, x + k) Plot (-y + h, -x + k)
Plot (-y + h, x + k) Plot (y + h, -x + k)
Plot (-x + h, y + k) Plot (x + h, -y + k)
Step5: Increment x = x + i
Page 30 of 82
STUDY MATERIAL FOR B.C.A
COMPUTER GRAPHICS
SEMESTER -VI, ACADEMIC YEAR 2020 - 21
Bresenham’s Algorithm
We cannot display a continuous arc on the raster display. Instead, we have to choose
the nearest pixel position to complete the arc.
From the following illustration, you can see that we have put the pixel at X,YX,Y location
and now need to decide where to put the next pixel − at N X+1,YX+1,Y or at S X+1,Y−1X+1,Y−1.
Algorithm:
Step 1 - Get the coordinates of the center of the circle and radius, and store them in x,
yand R respectively. Set P=0 and Q=R.
Step 2 − Set decision parameter D = 3 – 2R.
Step 3 − Repeat through step-8 while P ≤ Q.
Step 4 − Call Draw Circle X,Y,P,QX,Y,P,Q.
Step 5 − Increment the value of P.
Step 6 − If D < 0 then D = D + 4P + 6.
Step 7 − Else Set R = R - 1, D = D + 4P−QP−Q + 10.
Step 8 − Call Draw Circle X,Y,P,QX,Y,P,Q.
Page 31 of 82
STUDY MATERIAL FOR B.C.A
COMPUTER GRAPHICS
SEMESTER -VI, ACADEMIC YEAR 2020 - 21
Instead of relying on the boundary of the object, it relies on the fill color. In other words,
it replaces the interior color of the object with the fill color. When no more pixels of the
original interior color exist, the algorithm is completed.
Once again, this algorithm relies on the Four-connect or Eight-connect method of filling
in the pixels. But instead of looking for the boundary color, it is looking for all adjacent pixels
that are a part of the interior.
4-Connected Polygon
In this technique 4-connected pixels are used as shown in the figure. We are putting the
pixels above, below, to the right, and to the left side of the current pixels and this process will
continue until we find a boundary with different color.
Page 32 of 82
STUDY MATERIAL FOR B.C.A
COMPUTER GRAPHICS
SEMESTER -VI, ACADEMIC YEAR 2020 - 21
Algorithm
Step 1 − Initialize the value of seed point seedx,seedyseedx,seedy, fcolor anddcol.
Step 2 − Define the boundary values of the polygon.
Step 3 − Check if the current seed point is of default color, then repeat the steps 4 and
5 till the boundary pixels reached. If get pixel (x,y) = dcol then repeat step 4 and 5
Step 4 − Change the default color with the fill color at the seed point.
SetPixel(seedx, seedy, fcol)
Step 5 − Recursively follow the procedure with four neighbourhood points.
FloodFill (seedx – 1, seedy, fcol, dcol)
FloodFill (seedx + 1, seedy, fcol, dcol)
FloodFill (seedx, seedy - 1, fcol, dcol)
FloodFill (seedx – 1, seedy + 1, fcol, dcol)
Step 6 − Exit
There is a problem with this technique. Consider the case as shown below where we
tried to fill the entire region. Here, the image is filled only partially. In such cases, 4-connected
pixels technique cannot be used.
8-Connected Polygon
In this technique 8-connected pixels are used as shown in the figure. We are putting
pixels above, below, right and left side of the current pixels as we weredoing in 4-connected
technique.
In addition to this, we are also putting pixels in diagonals so that entire area of the
current pixel is covered. This process will continue until we find a boundary with different color.
Page 33 of 82
STUDY MATERIAL FOR B.C.A
COMPUTER GRAPHICS
SEMESTER -VI, ACADEMIC YEAR 2020 - 21
Algorithm
Step 1 − Initialize the value of seed point seedx,seedyseedx,seedy, fcolorand dcol.
Step 2 − Define the boundary values of the polygon.
Step 3 − Check if the current seed point is of default color then repeat the steps 4 and 5 till the
boundary pixels reachedIf getpixel(x,y) = dcol then repeat step 4 and 5
Step 4 − Change the default color with the fill color at the seed point.
SetPixel(seedx, seedy, fcol)
Step 5 − Recursively follow the procedure with four neighbourhood points
Step 6 − Exit
The 4-connected pixel technique failed to fill the area as marked in the following figure
which won’t happen with the 8-connected technique.
Inside-outside Test
This method is also known as counting number method. While filling an object, we
often need to identify whether particular point is inside the object or outside it. There are two
methods by which we can identify whether particular point is inside an object or outside.
➢ Odd-Even Rule
➢ Nonzero winding number rule
Odd-Even Rule
In this technique, we count the edge crossing along the line from any point x,yx,y to
infinity. If the number of interactions is odd then the point x,yx,y is an interior point. If the
number of interactions is even then point x,yx,y is an exterior point. Here is the example to give
you the clear idea −
Page 34 of 82
STUDY MATERIAL FOR B.C.A
COMPUTER GRAPHICS
SEMESTER -VI, ACADEMIC YEAR 2020 - 21
From the above figure, we can see that from the point x,yx,y, the number of interactions
point on the left side is 5 and on the right side is 3. So the total number of interaction point is
8, which is odd. Hence, the point is considered within the object.
When all the edges of the polygon are covered by the rubber band, check out the pin
which has been fixed up at the point to be test. If we find at least one wind at the point
consider it within the polygon, else we can say that the point is not inside the polygon.
In another alternative method, give directions to all the edges of the polygon.Draw a
scan line from the point to be test towards the left most of X direction.
➢ Give the value 1 to all the edges which are going to upward direction and all other -1 as
direction values.
➢ Check the edge direction values from which the scan line is passing andsum up them.
➢ If the total sum of this direction value is non-zero, then this point to be tested is
an interior point, otherwise it is an exterior point.
➢ In the above figure, we sum up the direction values from which the scan lineis passing
then the total is 1 – 1 + 1 = 1; which is non-zero. So the point is said to be an interior
point.
Page 35 of 82