Lecture 05
Lecture 05
Lecture 05
Line Drawing Techniques
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 ( xi + 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 = 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)