Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

STUDY MATERIAL FOR B.C.

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.

Advantage of developing algorithms for scan conversion:


1. Algorithms can generate graphics objects at a faster rate.
2. Using algorithms memory can be used efficiently.
3. Algorithms can develop a higher level of graphical objects.

Examples of objects which can be scan converted


1. Point
2. Line
3. Sector
4. Arc
5. Ellipse
6. Rectangle
7. Polygon
8. Characters
9. Filled Regions

The process of converting is also called as rasterization. The algorithms implementation


varies from one computer system to another computer system. Some algorithms are
implemented using the software. Some are performed using hardware or firmware. Some are
performed using various combinations of hardware, firmware, and software.

DDA Line Algorithm

DDA stands for Digital Differential Analyzer. It is an incremental method of scan


conversion of line. In this method calculation is performed at each step but by using results of
previous steps. Digital Differential Analyzer algorithm is the simple line generation algorithm
which is explained step by step here.Suppose at step i, the pixels is (xi,yi)

The line of equation for step i


yi=mxi+b......................equation 1
Next value will be
yi+1=mxi+1+b.................equation 2

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

Step6: If ABS (dx) > ABS (dy)


Then step = abs (dx) Else
Step7:xinc=dx/step
yinc=dy/step
assign x = x1
assign y = y1
Step8: Set pixel (x, y)
Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round (y))
Step10: Repeat step 9 until x = x2
Step11: End Algorithm
Example: If a line is drawn from (2, 3) to (6, 15) with use of DDA. How many points will needed to
generate such line?
Solution: P1 (2,3) P11 (6,15)
x1=2
y1=3
x2= 6
y2=15
dx = 6 - 2 = 4
dy = 15 - 3 = 12

m=

For calculating next value of x takes x = x +

Page 24 of 82
STUDY MATERIAL FOR B.C.A
COMPUTER GRAPHICS
SEMESTER -VI, ACADEMIC YEAR 2020 - 21

Bresenham's Line Algorithm


This algorithm is used for scan converting a line. It was developed by Bresenham. It is
an efficient method because it involves only integer addition, subtractions, and multiplication
operations. These operations can be performed very rapidly so lines can be generated
quickly.In this method, next pixel selected is that one who has the least distance from true line.

The method works as follows:


Assume a pixel P1'(x1',y1'),then select subsequent pixels as we work our way to the night,
one pixel position at a time in the horizontal direction toward P2'(x2',y2').
Once a pixel in choose at any step.

Page 25 of 82
STUDY MATERIAL FOR B.C.A
COMPUTER GRAPHICS
SEMESTER -VI, ACADEMIC YEAR 2020 - 21

The next pixel is


1. Either the one to its right (lower-bound for the line)
2. One top its right and up (upper-bound for the line). The line is best approximated by
those pixels that fall the least distance from the path between P1',P2'.To chooses the
next onebetween the bottom pixel S and top pixel T.

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

The actual y coordinates of the line at x = xi+1is


y=mxi+1+b

The distance from S to the actual line in y direction


s = y-yi
The distance from T to the actual line in y direction
t = (yi+1)-y
Now consider the difference between these 2 distance values
s-t
When (s-t) <0 ⟹ s < t
The closest pixel is S
When (s-t) ≥0 ⟹ s < t
The closest pixel is T
This difference is
s-t = (y-yi)-[(yi+1)-y]

= 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

DDA Algorithm Bresenyhham's Line Algorithm

1. DDA Algorithm use floating point, i.e., 1. Bresenham's Line Algorithm use fixed
RealArithmetic. point, i.e., Integer Arithmetic

2. DDA Algorithms uses multiplication &division 2. Bresenham's Line Algorithm uses


its operation onlysubtraction and addition its operation

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

to & each y coordinate is found by evaluating for each step of x.

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

Step6: Go to step (ii).

Circle Generation Algorithm


Drawing a circle on the screen is a little complex than drawing a line. Thereare two
popularalgorithms for generating a circle −Bresenham’s Algorithm and Midpoint Circle
Algorithm. These algorithms are based on the idea of determining the subsequent points
required to draw the circle. Let us discuss the algorithms in detail:

The equation of circle is X2+Y2=r2,X2+Y2=r2, where r is radius.

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.

This can be decided by the decision parameter d.


➢ If d <= 0, then NX+1,YX+1,Y is to be chosen as next pixel.
➢ If d > 0, then SX+1,Y−1X+1,Y−1 is to be chosen as the next pixel.

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

Draw Circle Method(X, Y, P, Q).


Call Putpixel (X + P, Y + Q).
Call Putpixel (X - P, Y + Q).
Call Putpixel (X + P, Y - Q).
Call Putpixel (X - P, Y - Q).
Call Putpixel (X + Q, Y + P).
Call Putpixel (X - Q, Y + P).
Call Putpixel (X + Q, Y - P).
Call Putpixel (X - Q, Y - P).

Flood Fill Algorithm


Sometimes we come across an object where we want to fill the area and its boundary
with different colors. We can paint such objects with a specified interior color instead of
searching for particular boundary color as in boundary filling algorithm.

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.

Boundary Fill Algorithm


The boundary fill algorithm works as its name. This algorithm picks a point inside an
object and starts to fill until it hits the boundary of the object. The color of the boundary and
the color that we fill should be different for this algorithm to work.
In this algorithm, we assume that color of the boundary is same for the entire object. The
boundary fill algorithm can be implemented by 4-connected pixels or 8-connected pixels.

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

FloodFill (seedx – 1, seedy, fcol, dcol)


FloodFill (seedx + 1, seedy, fcol, dcol)
FloodFill (seedx, seedy - 1, fcol, dcol)
FloodFill (seedx, seedy + 1, fcol, dcol)
FloodFill (seedx – 1, seedy + 1, fcol, dcol)
FloodFill (seedx + 1, seedy + 1, fcol, dcol)
FloodFill (seedx + 1, seedy - 1, fcol, dcol)
FloodFill (seedx – 1, seedy - 1, fcol, dcol)

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.

Winding Number Rule


This method is also used with the simple polygons to test the given point isinterior or
not. It can be simply understood with the help of a pin and a rubber band. Fix up the pin on one
of the edge of the polygon and tie-up the rubber bandin it and then stretch the rubber band
along the edges of the polygon.

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

You might also like