LESSON 3
Bresenham’s Line Algorithm
• It is an efficient raster line generation algorithm.
• It can be adapted to display circles and other curves.
• The algorithm
– After plotting a pixel position (xk, yk) , what is the next pixel
to plot?
• Consider lines with positive slope.
• For a positive slope, 0 < m < 1 and line is starting from left to
right.
• After plotting a pixel position (xk, yk) we have two choices for next
pixel:
– (xk +1, yk)
– (xk +1, yk+1)
• At position xk +1, we pay attention to the intersection of the
vertical pixel and the mathematical line path.
• At position xk +1, we label vertical pixel separations from the
mathematical line path as dlower , dupper.
• The y coordinate on the mathematical line at xk+1 is calculated as
y = m(xk +1)+ b
then
dlower = y − yk
= m (xk +1) + b − yk
and
dupper =(yk +1) − y
= yk +1− m(xk +1)− b
• To determine which of the two pixels is closest to the line path, we
set an efficient test based on the difference between the two pixel
separations
dlower - dupper = 2m (xk +1) − 2yk + 2b - 1
= 2 (Δy / Δx) (xk +1) − 2yk + 2b - 1
• Consider a decision parameter pk such that
pk = Δx (dlower - dupper )
= 2Δy.xk − 2Δx.yk + c
• where
c = 2Δy + Δx(2b −1)
• Since Δx > 0, Comparing (dlower and dupper ), would tell which
pixel is closer to the line path; is it yk or yk + 1
• If (dlower < dupper )
– Then pk is negative
• Hence plot lower pixel.
– Otherwise
• Plot the upper pixel.
• We can obtain the values of successive decision parameter as
follows:
pk = 2Δy.xk − 2Δx.yk + c
pk+1=2Δy.xk+1−2Δx.yk+1+c
• Subtracting these two equations
pk+1− pk = 2Δy (xk+1 − xk) − 2Δx ( yk+1 − yk)
• But xk+1 − xk = 1, Therefore
pk+1 = pk +2Δy − 2Δx (yk+1 − yk)
• ( yk+1 − yk) is either 0 or 1, depending on the sign of p k (plotting
lower or upper pixel).
• The recursive calculation of pk is performed at integer x position,
starting at the left endpoint.
• p0 can be evaluated as:
p0 = 2Δy − Δx
Bresenham’s Line-Drawing Algorithm for m < 1
1. Input the two line end points and store the left end point in (x0 , y0
).
2. Load (x0 , y0 ) into the frame buffer; that is, plot the first point.
3. Calculate the constants Δx, Δy, 2Δy, and 2Δy − 2Δx, and obtain the
starting value for the decision parameter as
p0 = 2Δy − Δx
1. At each xk along the line, starting at k = 0 , perform the following
test: If pk < 0,the next point to plot is (xk +1, yk and
pk+1=pk+2Δy
Otherwise, the next point to plot is (xk +1, yk +1) and
pk+1=pk+2Δy−2Δx
5. Repeat step 4, Δx−1 times.
Bresenham’s coding
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd,gm;
clrscr();
printf("\n\n\tEnter the co-ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("\n\n\tEnter the co-ordinates of second point : ");
scanf("%d %d",&x2,&y2);
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");
putpixel(x,y,WHITE);
while(x <= x2)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}
Summary
• The constants 2Δy and 2Δy − 2Δx are calculated once for each line
to be scan converted.
• Hence the arithmetic involves only integer addition and subtraction
of these two constants.
Example
• To illustrate the algorithm, we digitize the line with endpoints
(20,10) and (30,18). This line has slope of 0.8, with
Δx = 10
Δy =8
• The initial decision parameter has the value
p0 = 2Δy − Δx = 6
• and the increments for calculating successive decision parameters
are
2 Δy = 16
2 Δy - 2 Δx = -4
• We plot the initial point (x0 , y0)=(20,10) and determine successive
pixel positions along the line path from the decision parameter as
Loading frame buffer
• When straight line segments and other objects are scan converted
for display with raster system, frame buffer position must be
calculated. A setpixel procedure accomplishes storage of intensity
values of pixels in corresponding addresses in the buffer array.
Line functions
• Graphics system polyline generation
• Coordinate reference in polyline function are stated as absolute
values e.g (30 50), (60, 10) and so on.
• Other graphics system state as relative e.g (30,50), (30, -40)