You are on page 1of 19

UNIVERSITY INSTITUTE OF COMPUTING

Bachelor of Computer Science


Computer Graphics
20CAT- 312

DISCOVER . LEARN . EMPOWER


1
Syllabus
Unit-I
Introduction: Computer Graphics, Computer Graphics Applications.
Video Display Devices: Refresh Cathode Ray Tube, Raster Scan displays, Random Scan Displays,
Architecture of Raster and Random Scan Monitors, Color CRT-monitors, Color Generating
Techniques (Shadow Mask, Beam Penetration), Direct View Storage Tube, Flat Panel Display,
Graphics monitors and Workstations.
Two dimensional Graphics Primitives: Points and Lines, Point plotting Techniques: Coordinate
system, Increment method, Line drawing algorithm: DDA, Bresenham’s line drawing,
Bresenham‘s circle drawing algorithm: Using polar coordinates, Midpoint circle drawing
algorithms Filled area algorithm: Scan line, Polygon filling algorithms, Boundary filled
algorithms.
2
Bresenham’s Line Drawing Algorithm

Uses only incremental integer calculations


Which pixel to draw ?
Fig. 3.17 Section of a display screen where
a straight line segment is to be plotted,
– (11,11) or (11,12) ? starting from the pixel at column 10 on
scan Line 11

– (51,50) or (51,49) ?

– Answered by Bresenham
Fig. 3.18 Section of a display screen where
a negative slope line segment is to be
plotted, starting from the pixel at column
50 on scan line 50.

3
Bresenham’s Line Drawing Algorithm
continued..
For |m|<1
– Start from left end point (x0,y0) step to each
successive column (x samples) and plot the pixel
whose scan line y value is closest to the line path.
– After (xk,yk) the choice could be (xk+1,yk) or
(xk+1,yk+1)

4
Bresenham’s Line Drawing Algorithm
contd.
y  m(x k 1)  b

d1  y  y k
 m(x k 1)  b  yk

d 2  ( yk 1)  y
 yk 1 m(x k 1)  b
Difference between separations
Fig. 3.19 Distances between pixel
positions and the line y coordinate at
d1  d 2  2m(x k 1)  2 yk  2b 1 sampling position xk+ I. [1]

5
Bresenham’s Line Drawing Algorithm
contd.
y  mx  c
y k  m( x k 1)  c Equation 1
d1  y  y k
 m( x k 1)  c  y k Equation  2
d 2  ( y k 1)  y k
d 2  ( y k 1)  [ m( x k 1)  c ]
d1  d 2  [ m( x k 1)  c  y k ]  [( y k 1)  [ m( x k 1)  c ]]
d1  d 2  m( x k 1)  c  y k  ( y k 1)  [ m( x k 1)  c ]
d1  d 2  m( x k 1)  c  y k  y k  1  m( x k 1)  c
d1  d 2  2m( x k 1)  2 y k  2c 1 Equation  3

6
Bresenham’s Line Drawing Algorithm
contd.
y
now multiply both sides by x and put the value of m  in equation  3
x
x( d1  d 2 )  x[ 2m( x k  1)  2 y k  2c  1]
y
x( d1  d 2 )  x[ 2 ( x  1)  2 y  2c  1]
x k k
y
x( d1  d 2 )  x 2 ( x k  1)  2xy k  x( 2c  1)
x
y
Pk  x( d1  d 2 )  x . 2 ( x k  1)  2x . y k  x( 2c  1)
x
Pk  2y . x k  2y  2x . y k  x( 2c  1)
Pk  2y . x  2x . y  2y  2cx  x
k k
Pk  2y . x k  2x . y k  C where C  2y  2cx  x

7
Bresenham’s Line Drawing
Algorithm contd.
Pk  2y . x k  2x . y k  C           Equation  4
Pk  1  2y . x k  1  2x . y k  1  C       Equation  5
Pk  1  Pk  [2y . x k  1  2x . y k  1  C ]  [2y . x k  2x . y k  C ]

Pk  1  Pk  2y . x k  1  2x . y k  1  C  2y . x k  2x . y k  C

Pk  1  Pk  2y . ( x k  1  x k )  2x . ( y k  1  y k )      Equation  6

8
Bresenham’s Line Drawing
Algorithm contd.
But x  x  1 , Now put this value in equation  6 we have
k 1 k
Pk  1  Pk  2y . ( x  1  x )  2x . ( y y )
k k k 1 k
Pk  1  Pk  2y  2x . ( y y )
k 1 k
Pk  1  Pk  2y  2x . ( y k  1  y k )           Equation  7

Where the term y  y is either 0 or 1


k 1 k
if y y  1 then y  y is equal to y  1  y  1 and
k 1 k k 1 k k k
if y  y then y  y is equal to y  y  0
k 1 k k 1 k k k
9
Bresenham’s Line Drawing Algorithm
contd.
Pk  1  Pk  2y  2x . ( y  y )           Equation  7
k 1 k
Where the term y  y is either 0 or 1
k 1 k
if y  y  1 then y  y is equal to y  1  y  1 and
k 1 k k 1 k k k
Pk  1  Pk  2y  2x .1
Pk  1  Pk  2y  2x
if y  y then y  y is equal to y  y  0
k 1 k k 1 k k k
Pk  1  Pk  2y
10
Bresenham’s Line Drawing Algorithm
contd.
Pk  2y . x k  2x . y k  2y  2cx  x      equation  8
As you know the slope int ercept equation is y  m.x  c
If the cordinate of starting po int is ( x0 , y0 )
Therefore , y0  m . x0  c
y
c  y0  m . x0 where m 
x
Now put the value of m in equation  8
y
Pk  2y . x k  2x . y k  2y  2x[ y0  . x0 ]  x
x
y
Pk  2y . x0  2x . y0  2y  2x . y0  2x . x0  x
x
P0  2y . x0  2x . y0  2y  2x. y0  2y . x0  x
P0  2y  x
11
Defining decision parameter for Bresenham’s
Line
Constant=2Δy + Δx(2b-1) Which is
independent of pixel position

pk  x(d1  d 2 ) [1]
 2y.xk  2x.yk  c
Sign of pk is same as that of d1-d2 for Δx>0 (left to right sampling)
pk 1  2y.xk 1  2x.yk 1  c c eliminated here

pk 1  pk
 2y(x k 1  xk )  2x( yk 1  because xk+1 = xk + 1
yk )
p  p
 2y  2x( yk 1  yk+1-yk = 0 if pk < 0 & yk+1-yk = 1 if pk ≥ 0
For Recursive calculation,
k 1 k
yk ) initially

p0  2y  x Substitute b = y0 – m.x0 and m = Δy/Δx


in [1]
12
Algorithm Steps if (|m|<1)
1. Input the two line endpoints and store the left endpoint in (x0,y0)
2. Plot first point (x0,y0)
3. Calculate constants Δx, Δy, 2Δy and 2 Δy- 2Δx, and obtain 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 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 times

13
Example For Bresenham’s Line
Drawing
Endpoints (20,10) and (30,18)
Slope m = 0.8 Δx = 10, Δy = 8
P0 = 2Δy - Δx = 6
2Δy = 16, 2Δy-2Δx = -4
Plot (x0,y0) = (20,10)

Fig. 3.20 A plot of the pixels generated along this line path [1]

14
Another Example For Bresenham’s
Line Drawing
Digitize a line with endpoints
19
(15,18) and (10,15) using BLA.
Table 3.8 Pixel calculation of line with 18
endpoints (15, 18) and (10, 15)

K Pk Xk+1 Yk+1 17

0 1 11 16
16

1 -3 12 16
15
2 3 13 17

3 -1 14 17 10 11 12 13 14 15

Fig. 3.21 Pixel display of line with


4 5 15 18
endpoints (15, 18) and (10, 15)

15
Another example For Bresenham’s
Line Drawing
Digitize a line with endpoints
19
(15,15) and (10,18) using
BLA.
Table 3.9 - Pixel calculation of line with 18
endpoints (15, 15) and (10, 18)

K Pk Xk+1 Yk+1 17

0 1 11 17 16
1 -3 12 17
15
2 3 13 16
3 -1 14 16 10 11 12 13 14 15 16

4 5 15 15 Fig. 3.22 Pixel display of line with


endpoints (15, 15) and (10, 18) 16
Algorithm Steps when (|m|>1)
1. Input the two line endpoints and store the left endpoint in (x0,y0)
2. Plot first point (x0,y0)
3. Calculate constants Δx, Δy, 2Δx and 2 Δx- 2Δy, and obtain p0 = 2Δx – Δy
4. At each xk along the line, starting at k=0, perform the following test: If
pk<0, the next point plot is (xk, yk+1) and
Pk+1 = pk + 2Δx
Otherwise, the next point to plot is (xk + 1, yk+1) and Pk+1
= pk + 2Δx - 2Δy
5. Repeat step 4 Δx times

17
References

1. Computer Graphics, 2nd Ed., Hearn & Baker –PHI, New Delhi.

2. Graphics Programming with C By Yashwant Kanetkar, BPB

Publications, New Delhi.

3. Computer Graphics, Schaum’s Outline Series, MGH Publications.

18
THANK YOU

19

You might also like