Module 2
Module 2
Module 2
DDA 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
1
Computer Graphics Module II
2
Computer Graphics Module II
3
Computer Graphics Module II
float x_inc,y_inc,x,y;
x=x1; y=y1;
dx=abs(x2-x1); dy=abs(y2-y1);
if(dx>=dy)
step=dx;
else
step=dy;
x_inc=dx/step;
y_inc=dy/step;
setPixel(ROUND(x),ROUND(y))
for(k=0;k<steps;k++)
{
x=x+x_inc;
y=y+y_inc;
setPixel(ROUND(x),ROUND(y))
}
Example: A line has a starting point (9,18) and ending point (14,22). Apply
the DDA algorithm to plot a line.
4
Computer Graphics Module II
K xk yk ploat
9 18 (9,18)
1 10 18.8 (10,19)
2 11 19.6 (11,20)
3 12 20.4 (12,20)
4 13 21.2 (13,21)
5 14 22 (14,22)
d1 = y – yk = [m(xk + 1) + b ]– yk
d2 = (yk + 1) – y = yk + 1 -[m(xk + 1) – b]
The difference between these 2 separations is
d1-d2 = 2m(xk + 1) – 2 yk + 2b – 1
5
Computer Graphics Module II
A decision parameter pk for the kth step in the line algorithm can be
obtained by rearranging above equation so that it involves only integer
calculations Define
Pk = ▲x ( d1-d2) = 2 ▲y.xk -2▲ x.yk + c…………………..(1)
The sign of Pk is the same as the sign of d1-d2, since x > 0.
Parameter c is a constant and has the value 2▲ y + ▲x(2b-1)
If pixel at yk is closer to line-path than pixel at yk +1 (i.e, if d1 < d2) then pk is
negative. We plot lower pixel in such a case. Otherwise, upper pixel will be
plotted.
At step k + 1, the decision parameter can be evaluated as,
pk+1 = 2▲ y.xk+1 - 2 ▲x.yk+1 + c…………………………(2)
Taking the difference of pk+ 1 and pk we get the following.
pk+1 – pk = 2 ▲y(xk+1- xk) - 2▲x(yk+1 – yk)
But, xk+1 = xk +1, so that
pk+1 = pk + 2▲ y - 2 ▲ x(yk+1 – yk)
Where the term yk+1-yk is either 0 or 1, depending on the sign of parameter pk
The first parameter p0 is directly computed
p0 = 2▲ y - ▲x
ALGORITHM
1. Input the two end points and store the left end point in (x0,y0)
2. Load (x0,y0) into the frame buffer (plot the first point)
6
Computer Graphics Module II
7
Computer Graphics Module II
xk+1 =xk +1 = 9 + 1 = 10
yk+1 =yk +1 = 18 +1 = 19
Step 4: Now move to next step. We will calculate the coordinates until
we reach the end point of the line.
▲x -1 = 5 – 1 = 4
Step 5: Stop.
pk pk+1 xk+1 yk+1
9 18
3 1 10 19
1 -1 11 20
-1 7 12 20
7 5 13 21
5 3 14 22
8
Computer Graphics Module II
#include “device.h”
void drawline(int xa, int ya, int xb, int yb)
{ }
int dx, dy, p, x, y,xEnd; setPixel(x,y);
dx=abs(xb-xa);
dy=abs(yb-ya);
int twoDy=2*dy; while(x<xEnd)
int twoDyDx=2*(dy-dx); {
if(xa>xb) x++;
{ if(p<0)
x=xa; p=p+twoDy;
y=ya; else
xEnd=xa; {
} x++
else p=p+twoDyDx}
{ }
x=xb; setPixel(x,y);
y=yb; }
xEnd=xb; }
Advantages
● It is simple to implement because it only contains integers.
● It is quick and incremental
● It is fast to apply but not faster than the Digital Differential Analyzer (DDA)
algorithm.
● The pointing accuracy is higher than the DDA algorithm.
Disadvantages
● The Bresenham’s Line drawing algorithm only helps to draw the basic line.
● The resulted draw line is not smooth.
Midpoint Circle Algorithm
The midpoint circle drawing algorithm helps us to calculate the
complete perimeter points of a circle for the first octant. We can quickly
9
Computer Graphics Module II
find and calculate the points of other octants with the help of the first
octant points. The remaining points are the mirror reflection of the first
octant points.
This algorithm is used in computer graphics to define the coordinates
needed for rasterizing the circle. The midpoint circle drawing algorithm
helps us to perform the generalization of conic sections. Bresenham’s
circle drawing algorithm is also extracted from the midpoint circle
drawing algorithm. In the algorithm, we will use the 8-way symmetry
property.
In this algorithm, we define the unit interval and consider the nearest
point of the circle boundary in each step.
Let us assume we have a point a (p, q) on the boundary of the
circle and with r radius satisfying the equation fc (p, q) = 0
10
Computer Graphics Module II
Thus,
fcircle(x,y) < 0 if (x,y) is inside the circle boundary
fcircle(x,y) = 0 if (x,y) is on the circle boundary
fcircle(x,y) > 0 if (x,y) is outside the circle boundary
11
Computer Graphics Module II
12
Computer Graphics Module II
Program
class circlemidpoint(int xcenter, int ycenter,int radius)
{
int x=0;
int y=radius;
int p=1-radius;
void circleplot(int,int,int,int);
circleplot(xcenter, ycenter, x,y)
while(x<y)
{
x++;
if(p<0)
p=p+2*x+1;
else
{
y--;
p=p+2*(x-y)+1;
}
circleplot(xcenter, ycenter, x,y)
}
}
void circleplot(int xcenter, int ycenter,int x,int y)
{
setPixel(xcenter+x, ycenter+y);
setPixel(xcenter-x, ycenter+y);
setPixel(xcenter+x, ycenter-y);
13
Computer Graphics Module II
setPixel(xcenter-x, ycenter-y);
setPixel(xcenter+y, ycenter+x);
setPixel(xcenter-y, ycenter+x);
setPixel(xcenter+y, ycenter-x);
setPixel(xcenter-y, ycenter-x);
}
Example
1. Draw a circle with radius 10
P0=1-r
1-10= -9
2x0=0
2y0=20
14
Computer Graphics Module II
Character Generation
1) STROKE METHOD
2)BITMAP METHOD
Bitmap method is a called dot-matrix method as the name suggests this method
use array of bits for generating a character. These dots are the points for array whose
size is fixed. In bit matrix method when the dots is stored in the form of array the
value 1 in array represent the characters i.e. where the dots appear we represent that
position with numerical value 1 and the value where dots are not present is
represented by 0 in array. It is also called dot matrix because in this method characters
are represented by an array of dots in the matrix form. It is a two dimensional array
having columns and rows.
A 5x7 array is commonly used to represent characters. However 7x9 and 9x13 arrays
are also used. Higher resolution devices such as inkjet printer or laser printer may use
character arrays that are over 100x100.
15
Computer Graphics Module II
3) STARBUST METHOD:
In this method a fix pattern of line segments are used to generate characters. Out of
these 24 line segments, segments required to display for particular
character are highlighted. This method of character generation is called starbust
method because of its characteristic appearance
The starbust patterns for characters A and M. the patterns for particular characters are
stored in the form of 24 bit code, each bit representing one line segment. The bit is set
to one to highlight the line segment; otherwise it is set to zero. For example, 24-bit
code for Character A is 0011 0000
0011 1100 1110 0001 and for character M is 0000 0011 0000 1100 1111 0011.
This method of character generation has some disadvantages. They are
1. The 24-bits are required to represent a character. Hence more memory is required.
2. Requires code conversion software to display character from its 24- bitcode.
3. Character quality is poor. It is worst for curve shaped characters.
16