Professional Documents
Culture Documents
Bresenham's Algorithm
Bresenham's Algorithm
(Here pixel (1,2), (3,1) and (5,5) are illuminated and others are
non-illuminated)
1
A line from pixel (2,2) to (7,5) will be shown like this on the
screen.
The slope of a line plays a major role in the line equation that's
why Bresenham line drawing algorithm calculates the equation
according to the slope of the line.
The slope of the line can be greater than 1 (m>1) or less than or
equal to 1 (m<=1).
Now enough talking let's derive the equations.
Derivation:
2
Let's say we want to draw a line on the screen.
so, to draw a line we have to calculate the points or pixels to be
illuminated on the screen.
Y=mx+c ....(1)
Here m is the slope of Line and 3
C is y-Intercept of line
The slope can also be written as
.....(2)
First we calculate the slope of the line if slope is less than 1, then
X will always be incremented.
so at (m<=1)
we calculate the d1 which is distance between intersection
point y to the pixelyk
So, d1 = y-yk
d1 = mxk+1 +c -yk By using ....(1)
Similarly d2 is the distance between pixel yk+1 and intersection point y.
d2 = yk+1-y
d2 = yk+1-(mxk+1+c) By using ....(1)
Note: here x is always incrementing so we can write xk+1 as xk +1 and
here yk+1 is next pixel so we can write it as yk+1.
subtracting d2 from d1
d1-d2 = m(xk+1) +c -yk – [yk+1-(mxk+1+c)]
= m(xk+1)+c -yk – yk-1+m(xk+1)+c
d1-d2 = 2m(xk+1)-2yk+2c-1 .....(3)
dx(d1-d2) = 2dy(xk+1)-2dx(yk)+2dx(c)-dx
P0 =2dy-dx
But if the slope of line is greater then 1 (m>1).
then our Y coordinate will always be incremented and we have to
choose between xk or xk+1.
So, our Line equation will be:
Yk+1 = m(x)+c
dy(d1-d2) = 2dxyk+2dx-2dxc-2dyxk-dy