Professional Documents
Culture Documents
DDA Line Drawing Algorithm-2
DDA Line Drawing Algorithm-2
DDA Line Drawing Algorithm-2
Lecture 3
In computer graphics, we need to represent continuous graphics objects using
discrete pixels. This process is known as scan conversion. Every graphics system
must transform the primitives like lines, circles, and ellipses into a collection of
pixels.
Line drawing algorithms are used to draw a line in discrete graphical media.
There are three line drawing algorithms in computer graphics.
1.DDA algorithm (Digital Differential Analyzer)
2.Midpoint algorithm
3.Bresenham’s line algorithm
The Line Equation
Finding the next pixel to draw a line
Now we can simply calculate the next Y coordinate value by adding slope to the
current y coordinate.
Continue until X = X2
Case 2: When |m|>1
Similar to case 1, Start with,
X = X1, Y = Y1
But in this case, instead of X, we increment the Y value by one.
So, when we assign this to the slope equation, we can get the current X value.
Continue until Y = Y2
Note: Since we are dealing with pixels, we need integer values as coordinates. We
need to round off fraction values.
Pseudo code for DDA Algorithm
Step 1: first declare all variables
int X1, X2, Y1, Y2
float dx, dy, xn, yn, m
Step 2: dx=x2-x1
dy=y2-y1
Step 3: m=dy/dx
Step 4: if(m>1)
xn=x1 + 1/m
yn = y1 + 1
Elseif
xn = x1 + 1
Step 5: x1 = xn; y1 = yn
yn = y1 + m
Now (xn,yn) became (x1,y1) and calculate next (xn,yn)
Step 5: x1 = xn; y1 = yn
Step 6: Exit
Note:
If X1 > X2, take (X2, Y2) as the starting point and (X1, Y1) as the end point
Pseudo code for Bresenham’s Algorithm
Step 1: Let starting Coord = (X0, Y0)
Ending Coord = (Xn, Yn)
Step 2: Calculate dx=Xn-X0; dy=Yn-Y0
Step 3: Calculate Decision Parameter Pk=2dy-dx
Step 4: Consider a current point is (Xk, Yk) and the next point is (Xk+1, Yk+1), then find the next point
which is depending on the value of decision parameter Pk.
Step 4.1: If Pk<0, then Pk+1=Pk+2dy
Xk+1=Xk + 1; Yk+1=Yk
Step 4.2: If Pk>=0, then Pk+1=Pk+2dy-2dx
Xk+1=Xk + 1; Yk+1=Yk + 1
Step 5: Repeat Step 4 until end point is reached or iteration equals to (dx-1) times
Step 6: Exit
Draw a line using Bresenham’s line drawing algorithm for (10,5) and (20,15)
Step 1: Let starting Coord = (X0, Y0) = (10,5)
Ending Coord = (Xn, Yn) = (20,15)
Step 2: Calculate dx=Xn-X0; dy=Yn-Y0
Step 3: Calculate Decision Parameter Pk
Step 4: Consider a current point is (Xk, Yk) and the next point is (Xk+1, Yk+1), then find the next point
which is depending on the value of decision parameter Pk.
Step 4.1: If Pk<0, then Pk+1=Pk+2dy
Xk+1=Xk + 1; Yk+1=Yk
Step 4.2: If Pk>=0, then Pk+1=Pk+2dy-2dx
Xk+1=Xk + 1; Yk+1=Yk + 1
Step 5: Repeat Step 4 until end point is reached or iteration equals to (dx-1) times
Step 6: Exit
Example
Step3: pk = 1
Let’s plot the line from (9, 18) to (14,
pk = 2∆y — ∆x pk >0
22)
pk = 8–5 = 3 pk+1 = pk + 2∆y — 2∆x
Step1:
Step4: pk+1 = 1 + 8–2*5 = -1
Start coordinate (x0, y0) = (9, 18)
pk = 3 xk+1 = xk +1 = 10 + 1 = 11
End coordinate (xn, yn) = (14, 22)
pk>0 yk+1 = yk + 1 = 19 + 1 = 20
Step2:
pk+1 = pk + 2∆y — 2∆x pk = -1
∆x = xn — x0
pk+1 = 3 + 8–2*5 = 1 pk+1 = pk + 2∆y
∆x = 14–9 = 5
xk+1 = xk +1 = 9 + 1 = 10 pk = -1 + 8 = 7
2∆y = 4*2 = 8
Then draw a Line
Advantages
1.A fast incremental
algorithm (compared to
DDA).
2.use only integer
calculations.
3.can be implemented using
hardware. (Because it does
not use multiplication and
division)
Thank You!