DDA Line Drawing Algorithm-2

You might also like

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

DDA Line drawing algorithm

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

We need two endpoints, P and Q, to draw a line on


a raster screen. We are starting from the P coordinate
and find the next pixels until we reach the endpoint Q.
There are three special scenarios when we draw lines.
Case 1: Draw a Horizontal Line: m = 0

The horizontal line has the same Y coordinate value. In


this scenario, we only need to consider X coordinate
value changes. We first draw pixel P and increment only
the X coordinate value by 1 to get the next pixel.
Case 2: Draw a Vertical Line: m is undefined
Vertical lines have the same X coordinates values and different
Y coordinate values. Similar to the horizontal line draw, we first
draw the initial pixel (P), and this time we increment Y
coordinate values by 1 to get the next pixel until we reach
endpoint Q.

Case 3: Draw a Diagonal Line: m = 1


The final special scenario is to draw a diagonal line, where the
slope equals 1. To get the next pixel in diagonal line we need to
increment both X and Y coordinate values by 1.
DDA Algorithm
DDA stands for Digital Differential Analyzer. This is an incremental line algorithm, the
calculation of each step is based on the results of the previous steps.
The algorithm has 2 cases based on slope value.
Let’s take (X1, Y1), and (X2, Y2) as our endpoints.

Case 1: When |m|<1


Assume and X1 < X2
Start with,
X = X1, Y = Y1
As we know, the slope of the line can be determined using,
In this case, we always keep the difference in X coordinate values to 1.

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

yk+1 = yk + 1 = 18 + 1 = 19 xk+1 = xk +1 = 11+1 = 12


∆y = yn — y0
yk+1 = yk = 20
∆y = 22–18 = 4

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!

You might also like