Chapter 2
Chapter 2
Chapter 2
1.In computer graphics, a digital differential analyzer (DDA) is hardware or software used for
interpolation of variables over an interval between start and end point. DDAs are used for
rasterization of lines, triangles and polygons
2.In any 2-Dimensional plane if we connect two points (x0, y0) and (x1, y1), we get a line
segment.
3.In the case of computer graphics we can not directly join any two coordinate points, for that
we should calculate intermediate points coordinate and put a pixel for each intermediate
point, of the desired color
4.With the help of functions like putpixel(x, y, K) in C, where (x,y) is our co-ordinate and K denotes some color.
5.For using graphics functions, our system output screen is treated as a coordinate system where the coordinates of the top-left corner is
(0, 0) and as we move down our y-ordinate increases and as we move right our x-ordinate increases for any point (x, y).
6.Now, for generating any line segment we need intermediate points and for calculating them we can use a basic algorithm called
DDA(Digital differential analyzer) line generating algorithm.
DDA Algorithm:
1 Step : Consider one point of the line as (X0,Y0) and the second point of the line as (X1,Y1).
2 step:// calculate dx , dy
dx = X1 - X0;
dy = Y1 - Y0;
// Driver program
int main()
{
int gd = DETECT, gm;
1st Problem:
Explain DDA line drawing algorithm. For line segment between (2, 2) and (6, 6) :
we need (3, 3) (4, 4) and (5, 5) as our intermediate Points.
Answer:
Step1: suppose we want to draw a line from (2,2) to (6,6) So
x1=2, y1=2,
X2=6, y2=6
Step2:Therefore
Dx=(x2-x1)=6-2=4
Dy=(y2-y1)=6-2=4
Step3:As |Dx|=|Dy| the line is of gentle slope category
steps=abs(Dx) =4
2.Problem :-
Draw DDA line algorithm. For the following segments (1,1) to (5,3) for rasterize this line.
Answer: suppose we want to draw a line from (1,1) to (5,3)
Step1:
So x1=1,y1=1,
x2=5,y2=3
Given
Step 1: x1=1 , y1=1 ,x2=5, y2=6
Step 2: Dx=x1-x2=5-1=4
Dy=y1-y2=6-1=5
Step 3:
Since |Dy|>|Dx| the line is of steep slope category
∴ Steps= abs(Dy)=5
Step 4:
Xinc = Dx/steps=4/5=0.8
Yinc =Dy/steps=5/5=1
Step 5:
As we know the first point, let’s plot it as(1,1)
Xnew=Xold+Xinc= 1+0.8 =1.8
Ynew=Yold+Yinc= 1+1 =2
1.The line does not pass through all raster points (pixel).It passes through raster point(0,0) and subsequently crosses three pixels.
2.It is seen that the intercept of line with the line x=1 is closer to the line y=0 ,i.e. pixel(1,0) than to the line y=1 i.e. pixel(1,1).
Chapter 02 Computer Graphics Compiled by Dashrath Kale 3
3.Hence ,in this case,the raster point at (1,0) better
represents the path line than that at(1,1).
4.The intercept of line with the line x=2 is closer to the line
y=1, i.e. pixel(2,1) than to the line y =0,i.e. Pixel (2,0).Hence
the raster point at(2,1) better represents the path of line as
shown in the above figure.
5.in the mathematical terms error or decision variables is
defined as
E= DB - DA or DA - DB
6.Let us define e=DB-DA.now if e>0 , then it implies that DB
> DA i.e. the pixel above the line closer to the true line.
7.If DB < DA (i.e.e<0) then we can say that the pixel below
the line is closer to the true line.
Thus by checking only the sign of error term it is possible to determine the better pixel to represent the line path.
8.The error term is initially set as
e= 2Δy - Δx
Where Δy= y2 - y1 ,and Δx= x2 - x1
9.Then according to value of e following actions are taken
if (e ≥ 0)
{
y=y+1
e=e-2*Δx
}
x=x+1
e=e+2 * Δy
10. When e ≥ 0 error is initialized with e= e-2 Δx. This is continued till error is negative.in each iteration y is incremented by 1.
When e<0, error is initialized to e= e+2 Δy. In both cases x is incremented by 1.
1.Read the line end points (x1,y1) and (x2,y2) such that they are not equal.[if equal then plot that point and exit]
2. Δx = |x2 - x1| and Δy = |y2-y1|
3. [initialize starting point]
x=x1
y=y1
4.e= 2 * Δy - Δx
[Initialize value of decision variable or error to compensate for nonzero intercepts]
5.i= 1 [Initialization counter]
6. Plot (x,y)
7. if (e ≥ 0)
{
y=y+1
e=e-2* Δx
}
else
{
e=e+2*Δy
}
x= x+1
8. i= i+1
9.if (i ≤ Δx) then go to step 6.
10. Stop.
#include<stdio.h>
//#include<conio.h>
#include<graphics.h>
#include<math.h>
int main()
{
int x,y,dx,dy ,x1=100,y1=100,x2=200,y2=200,e;
int gd=DETECT,gm;
int i;
//clrscr();
initgraph(&gd,&gm,NULL);
//Read Two end point
/*printf("Enter the value of x1:\t");
delay(200);
if(e>=0)
{
y=y+1;
e=e-2*dy;
}
else{
e=e+2*dy;
}
x=x+1;
i=i+1;
}while (i<=dx);
getch();
closegraph();
}
Advantage:
Disadvantage:
1. 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.
1.Problem: - Consider the line from (5, 5) to (13, 9). Use Bresenham's algorithm to rasterize the line.
Solution :-
Evaluating steps 1 through 4 in the Bresenham’s algorithm we have,
Given: -
step 1 :- x1 = 5 , y1 = 5 and x2 = 13 , y2 = 9 .
step 2 :- Δx = | 13 - 5| =8 Δy = | 9 - 5 | = 4
step 3 :- x = 5 , y = 5
step 4 :- e = 2 * Δy - Δx = 2 * 4 - 8 = 0.
Tabulating the results of each iteration in the step 5 through 10.
Step5:
When e ≥ 0 error is initialized with e= e-2 Δy. This is continued till error is negative.in each iteration y is incremented
by 1. When e<0, error is initialized to e= e+2 Δy. In both case x is incremented by 1
2..Plot a line by using Bresenham's line generating algorithm from (1, 1) to (5, 3).
Solution:-
Step1 :x0,y0 = 1,1 and x1,y1= 5,3
Step2:calculate Δx and Δy
Δx=x2-x1 & Δy=y2-y1
Δx=5-1=4 & Δy=3-1=2
Step 5:- When e ≥ 0 error is initialized with e= e-2 Δy. This is continued till error is negative.in each iteration y is incremented by 1. When
e<0, error is initialized to e= e+2 Δy. In both case x is incremented by 1
8.Function of Circle at S
F(S) = (xk+1)2 + (yk-1)2 – r2 (Negative) Here the value of F(S) will be Negative because S is in-side the circle that makes (xk+1)2 +
2 2
(yk-1) Less than r
9.Now we need a decision parameter which help us decide the next pixel
Say Dk And , Dk = F(N)+F(S)
Here either we will get the positive or negative value of Dk
10.So if Dk < 0 :-
That means the negative F(S) is bigger than the positive F(N), that implies Point N is closer to the circle than point S. So we will
select pixel N as our next pixel.
Chapter 02 Computer Graphics Compiled by Dashrath Kale 7
11.if Dk > 0:-
That means positive F(N) is bigger and S is more closer as F(S) is smaller. So we will Select S as our next pixel.
12.Now lets find Dk
Dk = (xk+1)2 + (yk)2 – r2 + (xk+1)2 + (yk-1)2 – r2
(replacing xk+1 with xk + 1 and yk-1 with yk -1)
= (xk + 1) + (yk) – r + (xk + 1)2 + (yk -1)2 – r2
2 2 2
15.Now to find out the decision parameter of next pixel i.e. Dk+1
We need to find
Dk+1 - Dk = (ii) - (i)
= 2(xk+2)2 + (yk+1)2 + (yk+1 -1)2 – 2r2 - [2(xk + 1)2 + (yk)2 + (yk -1)2 – 2r2]
= 2(xk)2 + 8xk + 8 + (yk+1)2 + (yk+1)2 - 2yk+1 + 1 - 2r2 - [2(xk )2 - 4xk – 2 - (yk)2 - (yk)2 + 2yk - 1 + 2r2]
= 4xk + 2(yk+1)2 - 2yk+1 - 2(yk)2 - 2yk + 6
Dk+1 = Dk + 4xk + 2(yk+1)2 - 2yk+1 - 2(yk)2 - 2yk + 6 ----- (iii)
16.If (Dk < 0) then we will choose the N point as discussed.
i.e. (xk+1, yk) that means our next x coordinate is xk+1 and next y coordinate is yk i.e. yk+1 = yk, putting yk in (iii) in replace of yk+1
Now, Dk+1 = Dk + 4xk + 2(yk)2 - 2yk - 2(yk)2 - 2yk + 6
= Dk + 4xk + 6
17.If (Dk > 0) then we will choose S point. i.e. (xk+1, yk-1)
that means our next x coordinate is xk+1 and next y coordinate is yk i.e. yk+1 = yk-1 putting yk-1 in (iii) i n replace of yk+1
now,
Dk+1 = Dk + 4xk + 2(yk-1)2 - 2yk-1 - 2(yk)2 - 2yk + 6
Now we know
yk-1 = yk – 1
Therefore,
Dk+1 = Dk + 4xk + 2(yk -1)2 - 2(yk -1) - 2(yk)2 - 2yk + 6
= Dk + 4xk + 2(yk) 2 + 2 - 4yk - 2yk +2 - 2(yk)2 - 2yk + 6
= Dk + 4xk - 4yk + 10
= Dk + 4(xk - yk) + 10
Example.
1.calculate the pixel position along the circle path with radius r= 10 centered on the origin using
Answer:-
What is a Polygon?:
1.Polygons can be regular or irregular. They can be simple or complex, convex or concave.
2.The word "polygon" means "many-angled," from Greek. To be a polygon, a flat, closed shape must use only line segments to create its
sides. So a circle or any shape that has a curve is not a polygon.
3.The three identifying properties of any polygon are that the polygon is:
1. A two-dimensional shape
2. Closing in a space (having an interior and exterior)
3. Made with straight sides
4.Types of Polygons
Let's take a look at the vast array of shapes that are polygons and go into detail.
Inside-outside Test
This method is also known as counting number method. While filling an object, we often need to identify whether a particular point is
inside the object or outside it. There are two methods by which we can identify whether a particular point is inside an object or outside.
Odd-Even Rule
Nonzero winding number rule
1.Odd-Even Rule
In this technique, we will count the edge crossing along the line from any point x,y to infinity. If
the number of interactions is odd, then the point x,y is an interior point; and if the number of
interactions is even, then the point x,y is an exterior point. The following example depicts this
concept.
From the above figure, we can see that from the point x,y the number of interaction points on the
left side is 5 and on the right side is 3. From both ends, the number of interaction points is odd, so
the point is considered within the object.
Algorithm :
void boundaryFill4(int x, int y, int fill_color,int boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill4(x + 1, y, fill_color, boundary_color);
boundaryFill4(x, y + 1, fill_color, boundary_color);
boundaryFill4(x - 1, y, fill_color, boundary_color);
boundaryFill4(x, y - 1, fill_color, boundary_color);
}
}
// C Implementation for Boundary Filling Algorithm Function for 4 connected Pixels
#include <graphics.h>
void boundaryFill4(int x, int y, int fill_color,int boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill4(x + 1, y, fill_color, boundary_color);
boundaryFill4(x, y + 1, fill_color, boundary_color);
boundaryFill4(x - 1, y, fill_color, boundary_color);
boundaryFill4(x, y - 1, fill_color, boundary_color);
} }
int main()
{
int gd = DETECT, gm;
Algorithm:-
void boundaryFill8(int x, int y, int fill_color,int boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill8(x + 1, y, fill_color, boundary_color);
boundaryFill8(x, y + 1, fill_color, boundary_color);
boundaryFill8(x - 1, y, fill_color, boundary_color);
boundaryFill8(x, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y + 1, fill_color, boundary_color);
boundaryFill8(x + 1, y - 1, fill_color, boundary_color);
boundaryFill8(x + 1, y + 1, fill_color, boundary_color);
}
}
// C Implementation for Boundary Filling Algorithm Function for 8 connected Pixels
1 Flood fill colors an entire area in an enclosed Here area gets colored with pixels of a chosen color as
figure through interconnected pixels using a boundary this giving the technique its name
single color
2 So, Flood Fill is one in which all connected Boundary Fill is very similar with the difference being the
pixels of a selected color get replaced by a fill program stopping when a given color boundary is found.
color.
3 A flood fill may use an unpredictable amount Boundary fill is usually more complicated but it is a linear
of memory to finish because it isn't known algorithm and doesn't require recursion
how many sub-fills will be spawned
Chapter 02 Computer Graphics Compiled by Dashrath Kale 16
4 Time Consuming It is less time Consuming
Step 3 − Sort the intersection point in the increasing order of X coordinate i.e. p0,p1,
p1,p2, and p2,p3.
Step 4 − Fill all those pairs of coordinates that are inside polygons and ignore the
alternate pairs.
Scan Conversion
1.It is a process of representing graphics objects as a collection of pixels.
2.The graphics objects are continuous. The pixels used are discrete. Each pixel can have either on or off state.
3.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.
4. 0 is represented by pixel off. 1 is represented using pixel on. Using this ability, graphics computers represent pictures having discrete
dots
5.Any model of graphics can be reproduced with a dense matrix of dots or points. Most human beings think of graphics objects as points,
lines, circles, ellipses. For generating graphical objects, many algorithms have been developed.
FrameBuffer:
1.In raster scan displays a special area of memory is dedicated to graphics only. This memory area is called a frame buffer.
Chapter 02 Computer Graphics Compiled by Dashrath Kale 17
2.It holds the set of intensity values for all the screen points. The stored intensity values are retrieved from the frame buffer and displayed
on the screen one row (scan line) at a time.
3.Usually, a frame buffer is implemented using rotating random access semiconductor memory.
4.However, frame buffers also can be implemented using shift registers. Conceptually, shift register is operated as first-in-first-out (FIFO)
fashion, i.e. similar to queue.
5.We know that, when the queue is full and if we want to add a new data bit then the first data bit is pushed out from the bottom and
then the new data bit is added at the top.
6.Here, the data ejected out of the queue can be interpreted as the intensity of a pixel on a scan line.
7.one shift register is required per pixel on a scan line and the length of shift register in bits is equal to the number of scan lines.
8.Here, there are 8 pixels per scan line and there are in all 5 scan lines. The synchronization between the output of the shift register and
the video scan rate is maintained; data corresponding to a particular scan line is displayed correctly.
1. Point
2. Line
3. Sector
4. Arc
5. Ellipse
6. Rectangle
7. Polygon
8. Characters
9. Filled Regions
3.Bitmap Method:
1.This is the third method of character generation.
2.Also known as dot matrix because in this method characters are represented by an array
of dots in the matrix form.
3.It’s a two dimensional array having columns and rows: 5x7 as shown in fig.
4.7x 9 and 9x 13 arrays are also used.
5.Higher resolution devices may use character array 100x100.