Computer Graphics Line Drawing Techniques
Computer Graphics Line Drawing Techniques
Computer Graphics Line Drawing Techniques
Lecture 04
Line Drawing Techniques
Rahmat Ullah Niazi
What is Line
The algorithm will take two points P1 and P2 and draw line
between them whereas each point consists of x,y
coordinates.
dx = p2.x – p1. x
dy = p2.y – p1. y
m = dy / dx
x = p1.x
y = p1.y
b=y–m*x
if |m| < 1
for counter = p1.x to p2.x
drawPixel (x, y)
x=x+1
y=m*x+b
else
for counter = p1.y to p2.y
drawPixel (x, y)
y=y+1
x=(y–b)/m
Discussion on algorithm:
quite simple and easy to understand
but involves a lot of mathematical calculations 24
We have another algorithm that works fine in all directions
and involves less calculations; mostly only additions.
DDA Algorithm
Now very simple to say that step is the total number of pixels
required for a line.
Next step is to find xIncrement and yIncrement:
xIncrement = dx/step
yIncrement = dy/step
Next a loop is required that will run ‘step’ times.
d2 = yi + 1 – y
= yi + 1 – m ( x i + 1 ) – b
pi = dx (d1-d2)
pi = dx (2m * (xi+1) + 2b – 2yi –1 )
pi = 2dy (xi+1) – 2dx yi + dx (2b–1 ) ------
---------------- (i)
pi = 2 dy xi – 2 dx yi + k ---------------- (ii)
where k = 2 dy + dx (2b-1)
Then we can calculate pi+1 in terms of pi without any xi , yi or
k.
pi+1 = 2 dy xi+1 – 2 dx yi+1 + k
pi+1 = 2 dy (xi + 1) – 2 dx yi+1 + k since xi+1= xi + 1
pi+1 = 2 dy xi + 2 dy- 2 dx yi+1 + k ---
------------ (iii)
Now subtracting (ii) from (iii), we get
pi+1 – pi = 2 dy – 2 dx (yi+1 – yi )
pi+1 = pi + 2 dy – 2 dx (yi+1 – yi )
If the next point is: (xi+1,yi) then
d1 < d2 => d1 – d2<0
=> pi<0
=> pi+1 = pi + 2 dy
If the next point is: (xi+1,yi+1) then
d1>d2 => d1 – d2>0
=> pi>0
=> pi+1= pi + 2 dy – 2 dx
The pi is our decision variable, and calculated using integer
arithmetic from pre-computed constants and its previous
value. Now a question is remaining; i.e. how to calculate
initial value of pi? For that, we use equation (i) and put
values (x1, y1)
pi = 2 dy (x1+1) – 2 dx y1 + dx (2b – 1)
where b = y – m x implies that
pi = 2 dy x1 +2 dy – 2 dx y1
+ dx ( 2 (y1 – mx1) – 1)
pi = 2 dy x1 + 2 dy – 2 dx y1 + 2 dx y1
– 2 dy x1 – dx
pi = 2 dy x1 + 2 dy – 2 dx y1 + 2 dx y1
– 2 dy x1 – dx
there are certain figures that will cancel each other (shown
in pairs of different colour)
pi = 2 dy - dx
dx = x2-x1
dy = y2-y1
p = 2dy-dx
c1 = 2dy
c2 = 2(dy-dx)
x = x1
y = y1
plot (x, y, colour)
while (x < x2)
x++
if (p < 0)
p = p + c1
else
p = p + c2
y++
plot (x,y,colour)
Improving Performance
addr(X, Y) = addr(0,0)
+ Y rows * (Xm + 1)
+X (all in bytes)