Bresebham Line Drawing Intro
Bresebham Line Drawing Intro
The Bresenham algorithm is another incremental scan conversion algorithm. The big
advantage of this algorithm is that, it uses only integer calculations. Moving across the x
axis in unit intervals and at each step choose between two different y coordinates.
For example, as shown in the following illustration, from position (2, 3) you need to choose
between (3, 3) and (3, 4). You would like the point that is closer to the original line.
At sample position Xk+1, the vertical separations from the mathematical line are labelled
as dupper and dlower.
From the above illustration, the y coordinate on the mathematical line at xk+1 is –
Y = m(Xk+1) + b
Dlower = y−yk
= m(Xk+1)+b−Yk
and
dupper = (yk+1)−y
= Yk+1−m(Xk+1)−b
You can use these to make a simple decision about which pixel is closer to the
mathematical line. This simple decision is based on the difference between the two pixel
positions.
dlower−dupper = 2m(xk+1)−2yk+2b−1
Let us substitute m with dy/dx where dx and dy are the differences between the end-
points.
dx (dlower−dupper) = dx (2dy/dx(xk+1)−2yk+2b−1)
= 2dy.xk−2dx.yk+2dy+dx (2b−1)
= 2dy.xk−2dx.yk+C
So, a decision parameter Pk for the kth step along a line is given by −
Pk = dx(dlower−dupper)
= 2dy.xk−2dx.yk+C
If pk is negative, then choose the lower pixel, otherwise choose the upper pixel.
Remember, the coordinate changes occur along the x axis in unit steps, so you can do
everything with integer calculations. At step k+1, the decision parameter is given as −
pk+1 = 2dy.xk+1−2dx.yk+1+C
pk+1−pk = 2dy(xk+1−xk)−2dx(yk+1−yk)
pk+1 = pk+2dy−2dx(yk+1−yk)
{as for Pk +ive, we choose yk+1 = Yk +1 so this will be 1, and for Pk –ive, we choose yk+1 =yk so this will
be 0}
=“ + dx (2 (y-mx) -1)
= “ + 2y dx -2x dy –dx
Pk= 2 dy –dx
Now, keeping in mind all the above points and calculations, here is the Bresenham Line
Drawing algorithm for slope m < 1 −
Step 1 − Input the two end-points of line, storing the left end-point in (x0,y0).
Step 3 − Calculate the constants dx, dy, 2dy, and (2dy – 2dx) and get the first value for the
decision parameter as −
p0 = 2dy−dx
Step 4 − At each Xk along the line, starting at k = 0, perform the following test −
pk+1 = pk+2dy
Otherwise,
pk+1 = pk+2dy−2dx
if Pk <0, then Xk+1 =Xk +1 and Yk+1 = Yk so next point is (Xk+1, Yk)
For m > 1, increment y each time and increment x according to the value of p.
After solving, the equation for decision parameter Pk will be very similar, just the x and y in
the equation gets interchanged.