You are on page 1of 2

Bresenhams line algorithm

To draw a line with coordinates (0,0) and (5,3) ?

x,y
0,0

m= y / x = 3-0 / 5-0 = 3/5 = 0.6 = m-1 = 0.6-1 = -0.4<0

1,0 1,1 2,1 3,1 3,2 4,2

= -0.4+ 0.6 = 0.2 0 = 0.2 1 = -0.8< 0 = -0.8 + 0.6 = -0.2< 0 = -0.2 +0.6 = 0.40 = 0.4 1 = -0.6<0 = -0.6 + 0.6 = 0

Consider a line with initial points (x1,y1) and terminal points (x2,y2) in device space. If x = x2 x1 and y = y2 y1, we define the driving axis to be x axis if | x | | y | and y axis if | y | | x |. Driving axis is used as the axis of control for the algorithm. With in the main loop of the algorithm the coordinates corresponding to the driving axis is incremented by 1 unit. The coordinates corresponding to other axis is only incremented as needed. Consider an example in which we have to draw a line from (0 , 0) to (5 , 3). Bresenhams algorithm begins with the point (0 , 0) since x is the driving axis. It then increments x coordinate by 1 rather than keeping the track of y coordinates, the algorithm keeps an error bounded at each stage, which represents negative of the distance from points where line exists the pixel to the top edge of the pixel. This value is initially set to m 1, and is incremented by each time the x coordinate is incremented by 1. If > 0, the line has moved up by 1 pixel and we must increment the y coordinate and re adjust the error to represent the distance from the top of the new pixel which is done by subtracting 1 from . The above illustration shows the complete operation of the algoriyhm.

Bresenhams Circle Algorithm


Bresenhams circle (Xc , Yc , R) Description: Xc and Yc denote x and y coordinates of center of circle and R is the radius. 1) Set X = 0 and Y = R

2) Set D = 3 2R 3) Repeat while (X < Y) 4) Call draw circle (Xc, Yc, X, Y) 5) Set X = X+1 6) If (D < 0) Then 7) D = D+ 4X + 6 8) Else 9) Set Y = Y 1 10) D = D + 4 (X - Y) + 10 [End of if] 11) Call Draw circle (Xc, Yc, X, Y) [End of While] 12) Exit

You might also like