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βπ¦ β βπ₯