Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Graphics Lecture 03

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

IT-331 ( Computer Graphics

1)

Lecture 3
Line - Drawing Algorithms
Bresenham

Dr. Mohammed El-Said


Assistant Professor, Computer Science Dept.,
Faculty of Computers & Artificial Intelligence,
Helwan University - Cairo, Egypt.
1
Bresenham's line algorithm

• Accurate and efficient


• Uses only incremental integer calculations

The method is described for a line segment with a


positive slope less than one
The method generalizes to line segments of other slopes
by considering the symmetry between the various
octants and quadrants of the xy plane
Bresenham's line algorithm
• Decide what is
the next pixel
13 Specified position
line path
– (11,11) or
12
(11,12)
11

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

xk xk+1 xk+2 xk+3


Bresenham’s Approach

• 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

• pk has the same sign with dlower- dupper since Δx>0.


• c is constant and has the value c= 2Δy + Δx(2b-1)
– c is independent of the pixel positions and is eliminated from
decision parameter pk.

• If dlower< dupper then pk is negative.


– Plot the lower pixel (East)
• Otherwise
– Plot the upper pixel (North East)
Succesive decision parameter
• At step k+1
pk+1= 2Δy.xk+1 - 2Δx. yk+1 + c

• Subtracting two subsequent decision parameters yields:


pk+1-pk= 2Δy.(xk+1-xk) - 2Δx. (yk+1-yk)

• 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

4. 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.


Trivial Situations: Do not need Bresenham

 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

• Line end points: ( x0 , y0 ) (5,8); ( x1 , y1 ) (9,11)


• Deltas:
dx 4; dy 3

initially p (5,8)  2(dy )-(dx)


6  4 2  0
p 2  NE
Graph

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

You might also like