You know that DDA algorithm is an incremental scan conversion method which performs calculations at each step using the results from the preceding step. Here we are going to discover an accurate and efficient raster line generating algorithm, the Bresenham's line-drawing algorithm.
This algorithm was developed by Jack E. Bresenham in 1962 at IBM.
The function given below handles all lines and implements the complete Bresenham's algorithm.
function line(x0, x1, y0, y1)
boolean steep := abs(y1 - y0) > abs(x1 - x0)
if steep then
swap(x0, y0)
swap(x1, y1)
if x0 > x1 then
swap(x0, x1)
swap(y0, y1)
int deltax := x1 - x0
int deltay := abs(y1 - y0)
real error := 0
real deltaerr := deltay / deltax
int y := y0
if y0 < y1 then ystep := 1 else ystep := -1
for x from x0 to x1
if steep then plot(y,x) else plot(x,y)
error := error + deltaerr
if error ? 0.5
y := y + ystep
error := error - 1.0
4. INTRODUCTION
• Raster line-generating algorithm
• Developed by Bresenham
• Scan conversion takes place
using only incremental integer
calculations
• Accurate and efficient than DDA
5. DERIVATION
• Starting from the left endpoint (x0, y0) of a given line,
we step to each successive column (x position) and plot
the pixel whose scan-line y value is closest to the line
path.
• At sample positions 𝑥 𝑘 + 1 the vertical separations
from the line are labelled 𝑑 𝑢𝑝𝑝𝑒𝑟 and 𝑑𝑙𝑜𝑤𝑒𝑟
• y coordinate on the line at 𝑥 𝑘 + 1 is,
𝑦 = 𝑚 𝑥 𝑘 + 1 + 𝑏
• so,
𝑑 𝑢𝑝𝑝𝑒𝑟 = 𝑦 − 𝑦 𝑘 = 𝑚 𝑥 𝑘 + 1 + 𝑏 − 𝑦 𝑘
𝑑𝑙𝑜𝑤𝑒𝑟 = 𝑦 𝑘 + 1 − 𝑦 = 𝑦 𝑘 + 1 − 𝑚 𝑥 𝑘 + 1 + 𝑏
𝑑𝑙𝑜𝑤𝑒𝑟
𝑑 𝑢𝑝𝑝𝑒𝑟
𝑥 𝑘 + 1
𝑦 𝑘 + 1
𝑦
𝑦 𝑘
6. DERIVATION
• It can be used to make decision about which pixel is closer to the line
• This decision is based on the difference between the two pixel positions,
𝑑 𝑢𝑝𝑝𝑒𝑟 − 𝑑𝑙𝑜𝑤𝑒𝑟 = 2𝑚 𝑥 𝑘 + 1 − 2𝑦 𝑘 + 2𝑏 − 1
• By substituting 𝑚 = ∆𝑦/∆𝑥 and both are differences of end points,
∆𝑥 𝑑 𝑢𝑝𝑝𝑒𝑟 − 𝑑𝑙𝑜𝑤𝑒𝑟 = ∆𝑥 2
∆𝑦
∆𝑥
𝑥 𝑘 + 1 − 2𝑦 𝑘 + 2𝑏 − 1
= 2∆𝑦. 𝑥 𝑘 − 2∆𝑥. 𝑦 𝑘 + 2∆𝑦 + ∆𝑥(2𝑏 − 1)
= 2∆𝑦. 𝑥 𝑘 − 2∆𝑥. 𝑦 𝑘 + 𝐶
7. DERIVATION
• Now, a decision parameter 𝑃𝑘 for the 𝑘th step along a line,
𝑃𝑘 = ∆𝑥 𝑑 𝑢𝑝𝑝𝑒𝑟 − 𝑑𝑙𝑜𝑤𝑒𝑟
= 2∆𝑦. 𝑥 𝑘 − 2∆𝑥. 𝑦 𝑘 + 𝐶
• The sign of 𝑃𝑘 is same as that of 𝑑 𝑢𝑝𝑝𝑒𝑟 − 𝑑𝑙𝑜𝑤𝑒𝑟
• If 𝑃𝑘 is –ve then we choose the lower pixel i.e. 𝑦 𝑘 only, otherwise we choose the upper
pixel i.e. 𝑦 𝑘 + 1
• So, for 𝑃𝑘 + 1 at step 𝑘 + 1,
𝑃𝑘+1 = 2∆𝑦. 𝑥 𝑘+1 − 2∆𝑥. 𝑦 𝑘+1 + 𝐶
• Subtracting 𝑃𝑘,
𝑃𝑘+1 − 𝑃𝑘 = 2∆𝑦(𝑥 𝑘+1 − 𝑥 𝑘) − 2∆𝑥(𝑦 𝑘+1 − 𝑦 𝑘) + 𝐶
8. DERIVATION
• 𝑥 𝑘+1 is same as 𝑥 𝑘 + 1 so,
𝑃𝑘+1 = 𝑃𝑘 + 2∆𝑦 − 2∆𝑥(𝑦 𝑘+1 − 𝑦 𝑘)
• Here, 𝑦 𝑘+1 − 𝑦 𝑘 is either 0 or 1 depending on the sign of 𝑃𝑘
• If 𝑃𝑘 < 0, the next point to plot is (𝑥 𝑘 + 1, 𝑦 𝑘) and new value of 𝑃 is,
𝑃𝑘+1 = 𝑃𝑘 + 2∆𝑦
• If 𝑃𝑘 > 0, the next point to plot is (𝑥 𝑘 + 1, 𝑦 𝑘 + 1) and new value of 𝑃 is,
𝑃𝑘+1 = 𝑃𝑘 + 2∆𝑦 − 2∆𝑥
• The first decision parameter 𝑃0 is evaluated at (𝑥0, 𝑦0) is,
𝑃0 = 2∆𝑦 − ∆𝑥