Graphics Lecture 03
Graphics Lecture 03
Graphics Lecture 03
1)
Lecture 3
Line - Drawing Algorithms
Bresenham
10
10 11 12 13
Illustrating Bresenham’s Approach
• For the pixel position
xk+1=xk+1, which one
yk+3 we should choose:
yk+2
• (xk+1,yk) or (xk+1, yk+1)
y=mx+b
yk+1
yk
• y=m(xk + 1)+b
yk+1
dupper
y
• dlower=y-yk
dlower
=m(xk + 1)+b-yk
yk
• dupper=(yk+1)-y
xk+1
• dlower- dupper= 2m(xk + 1)+2yk+2b-1 = yk+1 -m(xk + 1)-b
• Rearrange it to have integer calculations:
m=Δy/Δx
Decision parameter: pk= Δx(dlower- dupper)=2Δy.xk - 2Δx. yk + c
The Decision Parameter
Decision parameter: pk= Δx(dlower- dupper)=2Δy.xk - 2Δx. yk + c
• xk+1=xk+1 so
pk+1= pk + 2Δy - 2Δx. (yk+1-yk)
– yk+1-yk is either 0 or 1 depending on the sign of pk
• First parameter p0
– p0=2 Δy - Δx
Bresenham's Line-Drawing Algorithm for I m I < 1
1. Input the two line endpoints and store the left endpoint in (x0 ,y0).
2. Load (x0 ,y0) into the frame buffer; that is, plot the first point.
3. Calculate constants Δx, Δy, 2Δy, and 2Δy - 2Δx, and obtain the starting
value for the decision parameter as
p0=2 Δy - Δx
m 0 horizontal line
m 1 line y x
m vertical line
Example
• Draw the line with endpoints (20,10) and (30, 18).
– Δx=30-20=10, Δy=18-10=8,
– p0 = 2Δy – Δx=16-10=6
– 2Δy=16, and 2Δy - 2Δx=-4
• Plot the initial position at (20,10), then
Example
13
12
11
10
9
8
7
6
4 5 6 7 8 9 10 11
Continue the process...
13
12
11
10
9
8
7
6
4 5 6 7 8 9 10 11
Graph
13
12
11
10
9
8
7
6
4 5 6 7 8 9 10 11
Graph
13
12
11
10
9
8
7
6
4 5 6 7 8 9 10 11
Graph
13
12
11
10
9
8
7
6
4 5 6 7 8 9 10 11
/* Bresenham line-drawing procedure for |m| < 1.0. */
void lineBres (int x0, int y0, int xEnd, int yEnd)
{
int dx = fabs (xEnd - x0), dy = fabs(yEnd - y0);
int x, y, p = 2 * dy - dx;
int twoDy = 2 * dy, twoDyMinusDx = 2 * (dy - dx);
/* Determine which endpoint to use as start position. */
if (x0 > xEnd) {
x = xEnd; y = yEnd; xEnd = x0;
}
else {
x = x0; y = y0;
}
setPixel (x, y);
while (x < xEnd) {
x++;
if (p < 0)
p += twoDy;
else {
y++;
p += twoDyMinusDx;
}
setPixel (x, y);
}
}
Simulating the
Bresenham
algorithm in
drawing 8 radii on a
circle of radius 20
Horizontal, vertical
and 45 radii
handled as special
cases
Line-drawing algorithm should work in every
octant, and special cases
m<-1 m>1
0>m>-1 0<m<1
0<m<1 0>m>-1
m>1 m<-1