Computer Graphics Dr. Soumi Ghosh
Computer Graphics Dr. Soumi Ghosh
But in the case of computer graphics we can not directly join any two
coordinate points, for that we should calculate intermediate point’s
coordinate and put a pixel for each intermediate point, of the desired
color with help of functions like putpixel(x, y, K) , where (x,y) is our co-
ordinate and K denotes some color.
DDA Algorithm
∆x> ∆y P2
P1
∆y> ∆x
P2
Digital Differential Algorithm(DDA)
• Scan
Steps
P1 ( xa,ya) and P2 (xb,yb) are the two end points.
P1 P2
(xa,ya) (xb,yb)
DDA ALGORITHM
Step 1 − Get the input of two end points (X0,Y0)(X0,Y0) and (X1,Y1)(X1,Y1).
dx = X1 - X0 dy = Y1 - Y0
Step 3 − Based on the calculated difference in step-2, you need to identify the
number of steps to put pixel.
If dx > dy, then you need more steps in x coordinate; otherwise in y coordinate.
}
DDA Example
• Suppose we want to draw a
line starting at pixel (2,3) and
numsteps = 12 – 2 = 10
ending at pixel (12,8).
xinc = 10/10 = 1.0 X1-X0= 10
// assume that slope is gentle M<=1 yinc = 5/10 = 0.5 Y1- y0= 5
DDA(float x0, float x1, float y0, float y1)
{ Iteration x y Round(x) Round(y)
float x, y; 0 2 3 2 3
float xinc, yinc;
int numsteps; 1 3 3.5 3 4
2 4 4 4 4
numsteps = Round(x1) – Round(x0);
xinc = (x1 – x0) / numsteps; 3 5 4.5 5 5
yinc = (y1 – y0) / numsteps; 4 6 5 6 5
x = x0;
y = y0; 5 7 5.5 7 6
putpixel(Round(x),Round(y) 6 8 6 8 6
);
7 9 6.5 9 7
for (int i=0; i<numsteps; i++) { 8 10 7 10 7
x += xinc;
y += yinc; 9 11 7.5 11 8
putpixel(Round(x),Round(y)); 10 12 8 12 8
}}
DDA Example
• Suppose we want to draw a
line starting at pixel (15,4) and
numsteps =
ending at pixel (20,17).
xinc = X1-X0=
// assume that slope is gentle M>1 yinc = Y1- y0=
DDA(float x0, float x1, float y0, float y1)
{ Iteration x Round(x Round(y)
float x, y; )
y 0
float xinc, yinc;
int numsteps; 1
2
numsteps = Round(y1) – Round(y0);
xinc = (x1 – x0) / numsteps; 3
yinc = (y1 – y0) / numsteps; 4
x = x0;
y = y0; 5
putpixel(Round(x),Round(y) 6
);
7
for (int i=0; i<numsteps; i++) { 8
x += xinc;
y += yinc; 9
putpixel(Round(x),Round(y)); 10
}}
DDA Example
• Suppose we want to draw a
line starting at pixel (6,3) and
numsteps = ?
ending at pixel (8,10).
xinc = ? X1-X0=
// assume that slope is gentle M>1 yinc = ? Y1-
DDA(float x0, float x1, float y0, float y1) y0=
{ Iteration x Round(y)
float x, y; Round(x
y 0
float xinc, yinc; )
int numsteps; 1
2
numsteps = Round(y1) – Round(y0);
xinc = (x1 – x0) / numsteps; 3
yinc = (y1 – y0) / numsteps; 4
x = x0;
y = y0; 5
putpixel(Round(x),Round(y) 6
);
7
for (int i=0; i<numsteps; i++) { 8
x += xinc;
y += yinc; 9
putpixel(Round(x),Round(y)); 10
}}
DDA Example
• Suppose we want to draw a
line starting at pixel (2,3) and
numsteps = ?
ending at pixel (5,12).
xinc = ? X1-X0=
yinc = ? Y1-
y0=
Iteration x y Round(x) Round(y)
0
1
2
3
4
5
6
7
8
9
10
DDA Algorithm (continued)
Y_inc
X_inc
Remember! The algorithm and derivation above assumes slopes are less
than or equal to 1. for other slopes we need to adjust the algorithm slightly.
Bresenham’s Algorithm Example
Where m<=1
• Suppose we want to draw a line starting at dx = 12 – 2 = 10 2dy = 10
pixel (2,3) and ending at pixel (12,8). dy = 8 – 3 = 5 2dy – 2dx = -10
p0 = 2dy – dx = 0
t p P(x) P(y)
Algorithm 0 0 2 3
1. Input the two line endpoints and 1 -10 3 4
store left endpoint as (x0,y0)
2. Pre-calculate the values dx, dy, 2 0 4 4
2dy and 2dy -dx 3 -10 5 5
3. Color pixel (x0,y0)
4 0 6 5
4. Let p0 = 2dy –dx
5. At each xk along the line, 5 -10 7 6
starting with k=0: 6 0 8 6
If pk<0, then the next point to plot is (xk + 1,yk), 7 -10 9 7
and pk+1 = pk + 2dy (Down pixel will be selected)
8 0 10 7
Otherwise, the next point to plot is (xk + 1, yk + 1),
and pk+1 = pk + 2dy – 2dx (Upper pixel will be 9 -10 11 8
selected) 10 0 12 8
6. Repeat Step-4 dx times
Anti-aliasing and
filtering
techniques
Anti-aliasing
• Anti-aliasing is a method of fooling the eye
that a jagged edge is really smooth.
• Due to low resolution aliasing effect will
occur, which can be removed by increasing
the screen resolution.
Question-
90% 65%
15% 50%
Thank you!!