You are on page 1of 28

Line Drawing Algorithm

Prepared By: Prof. Bhavini R. Bhatt

Algorithms for Drawing line


Introduction - Pixel addressing - Primitives and attributes Line drawing algorithms - DDA - Bresenham

Pixel addressing in raster graphics and how does computer draw line?
Screen made of pixels High-level language specifies line System must color pixels
Pixel address y y+1 y+2 y+3 x x+1 x+2 x+3 x+4

Theoretical length

Pixel

Actual length

Line drawing algorithms


symbols & descriptions y = 2x + 5 x0 = 100 y0 = 50 d = 100 thickness = 4 images

Line raster representation

DDA ( Digital Differential Algorithm )


m<1

DDA ( Digital Differential Algorithm )


m>1

Line Drawing Algorithms


Line drawn as pixels Graphics system
Projects the endpoints to their pixel locations in the frame buffer (screen coordinates as integers) Finds a path of pixels between the two Loads the color Plots the line on the monitor from frame buffer (video controller) Rounding causes all lines except horizontal or vertical to be displayed as jigsaw appearance (low resolution) Improvement: high resolution, or adjust pixel intensities on the line path.

Line Drawing Algorithms


Line equation
Slope-intercept form y=m.x+ b slope m y-intercept b
Slope y-intercept

b y0 mx0

yend y0 y m xend x0 x

Line Drawing Algorithms

y mx x

y
m

For lines with slope |m|<1, x can be set to a proportional to small deflection value, y can be calculated from the formula For lines with slope |m|<1, y can be set to a proportional to small deflection value, x can be calculated from the formula

Line Drawing Algorithms


DDA (Digital Differential Analyzer)
Scan conversion line algorithm based on calculating either x or y Line sampled at regular intervals of x, then corresponding y is calculated and rounded From left to right
if m 1.0, if m > 1.0,
if m 1.0, if m > 1.0,

y k +1 = y k + m x k +1 = x k 1 + m
1 m

( x = 1) ( y = 1)
( x = -1) ( y = -1)

y k +1 = y k - m x k +1 = x k -

Line Drawing Algorithms


void lineDDA (int x0, int y0, int xEnd, int yEnd) { int dx = xEnd - x0, dy = yEnd - y0, steps, k; float xIncrement, yIncrement, x = x0, y = y0; if (fabs (dx) > fabs (dy)) steps = fabs (dx); else steps = fabs (dy); xIncrement = float (dx) / float (steps); yIncrement = float (dy) / float (steps); setPixel (round (x), round (y)); for (k = 0; k < steps; k++) { x += xIncrement; y += yIncrement; setPixel (round (x), round (y)); } }

DDA Algorithm (continued)

Y_inc

X_inc

but floating point operations and rounding operations are expensive

DDA Algorithm
Start with starting and ending coordinates of the line:
(x0, y0) and (x1, y1)

Color first pixel (round to nearest integer) Suppose x1-x0 > y1-y0 (gentle slope)
There will be x1-x0 steps (# pixels to be colored)

Set x=x0, y=y0 At each step,


increment x by (x1-x0)/numsteps, and increment y by (y1-y0)/numsteps

For each step, round off x and y to nearest integer, and color pixel

DDA Pseudo-code
// assume that slope is gentle DDA(float x0, float x1, float y0, float y1) { float x, y; float xinc, yinc; int numsteps; numsteps = Round(x1) Round(x0); xinc = (x1 x0) / numsteps; yinc = (y1 y0) / numsteps; x = x0; y = y0; ColorPixel(Round(x),Round(y)); for (int i=0; i<numsteps; i++) { x += xinc; y += yinc; ColorPixel(Round(x),Round(y)); }

Q: For each step, how many floating point operations are there? A: 4 Q: For each step, how many integer operations are there? A: 2

DDA Example
Suppose we want to draw a line starting at pixel (2,3) and ending at pixel (12,8). What are the values of the variables x and y at each timestep? What are the pixels colored, according to the DDA algorithm?
numsteps = 12 2 = 10 xinc = 10/10 = 1.0 yinc = 5/10 = 0.5 t 0 1 2 3 4 5 6 7 8 9 10 x 2 3 4 5 6 7 8 9 10 11 12 y 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8 R(x) 2 3 4 5 6 7 8 9 10 11 12 R(y) 3 4 4 5 5 6 6 7 7 8 8

DDA Algorithm
Advantage
Does not calculate coordinates based on the complete equation (uses offset method)

Disadvantage
Round-off errors are accumulated, thus line diverges more and more from straight line Round-off operations take time

Perform integer arithmetic by storing float as integers in numerator and denominator and performing integer arithmetic.

Bresenhams Line Drawing Algorithm


Efficient line drawing algorithm using only incremental integer calculations Can be adapted to draw circles and other curves Principle
Vertical axes show scan line positions Horizontal axes show show pixel columns At each step, determine the best next pixel based on the sign of an integer parameter whose value is proportional to the difference between the vertical separations of the two pixel positions from the actual line.

Bresenhams Algorithm
Uses only integer calculations Uses distance between ideal y-coordinate and the upper and lower pixel (assuming gentle slope)

dupper dlower

General idea how Bresenham works


first consider the scan-conversion process for lines with positive slope less than 1. Pixel positions along a line path are then determined by sampling at unit x intervals. Starting from the left endpoint (xo, yo) of a given line, we step to each successive column (x position) and plot the pixel whose scanline y value is closest to the line path. Figure 3-7 demonstrates the Kth step in this process. Assuming we have determined that the pixel at (xk, yk) is to be displayed, we next need to decide which pixel to plot in column xk+1, Our choices are the pixels at positions (Xk+l, Yk) and (Xk+l, Yk+l).

Bresenhams Line Drawing Algorithm

Section of the screen grid showing a pixel in column xk on scan line yk that is to be plotted along the path of a line segment with slope O<m<l.

Bresenhams Line Drawing Algorithm

Distances between pixel positions and the line y coordinate at sampling position xk+ I.

Bresenham's line algorithm


y = mx + b

y = m(x+1) + b y

d2
d1

x+1

Bresenham's line algorithm (slope 1)


1. Input the two line endpoints and store the left endpoint in (x0, y0). 2. Set the color for the frame-buffer position (x0, y0) i.e. plot the first point. 3. Calculate the constant x, y, 2y and 2y 2x, and set the starting value for the decision parameter as p0 = 2y x 4. At each xk along the line, from k=0, perform the following test:
if pk<0, next point to plot is (xk + 1, yk) and pk+1 = pk + 2y otherwise, next point to plot is (xk + 1, yk + 1 ) and pk+1 = pk + 2y- 2x Repeat step-4 x times.

5.

Number of Operations in Bresenhams Algorithm


Q: In each step, how many floating point operations are there? A: 0
Q: In each step, how many integer operations are there? A: 3 or 4

Bresenhams Algorithm Example


Suppose we want to draw a line starting at pixel (2,3) and ending at pixel (12,8). What are the values of p0, dx and dy? What are the values of the variable p at each timestep? What are the pixels colored, according to Bresenhams algorithm?
dx = 12 2 = 10 dy = 8 3 = 5 p0 = 2dy dx = 15 t 0 1 2 3 p 0 -10 0 -10 P(x) 2 3 4 5 2dy = 10 2dy 2dx = -10 P(y) 3 4 4 5

4
5 6 7 8 9 10

0
-10 0 -10 0 -10 0

6
7 8 9 10 11 12

5
6 6 7 7 8 8

DDA versus Bresenhams Algorithm


DDA works with floating point arithmetic Rounding to integers necessary
Bresenhams algorithm uses integer arithmetic Constants need to be computed only once Bresenhams algorithm generally faster than DDA

You might also like