Вы находитесь на странице: 1из 18

Line Drawing Algorithms by Virendra Singh Kushwah

Assistant Professor Department of Computer Science

4/28/2012

Designed By Virendra Singh Kushwah

Points and Lines


Point: A geometric element that has position but no extension; "a point is defined by its coordinates". A point is represented by position on plane.

4/28/2012

Designed By Virendra Singh Kushwah

Intermediate Positions between Two Endpoints

4/28/2012

Designed By Virendra Singh Kushwah

Line drawing algorithm


A line drawing algorithm is a graphical algorithm for approximating a line segment on discrete graphical media. There are two types of line drawing algorithms:
DDA (Digital Differential Analyzer) algorithm Bresenham line algorithm

4/28/2012

Designed By Virendra Singh Kushwah

How does computer draw line?


Screen made of pixels System must color pixels

4/28/2012

Designed By Virendra Singh Kushwah

Line Concept
Line Equation The Cartesian slop-intercept equation for a straight line is y=mx+b with m->slope b->y intercept -----(1)

The 2 end points of a line segment are specified at a position(x1,y1)

4/28/2012

Designed By Virendra Singh Kushwah

Determine the values for the slope m and y intercept b with the following calculation.
here, slope m: m = ( y2 - y1) / ( x2 - x1 ) m= Dy / Dx y intercept b b=y1-mx1 ----------(2) (3)

Algorithms for displaying straight line based on this equation y interval Dy from the equation m= Dy / Dx Dy= m. Dx ------ ( 4 )

Similarly x interval Dx from the equation m = Dy / Dx Dx = Dy /m ------- ( 5 )

4/28/2012

Designed By Virendra Singh Kushwah

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

4/28/2012

Designed By Virendra Singh Kushwah

DDA Pseudo-code
LineDDA(int x0, int y0,int xl, int yl) { int dx,dy,steps,k; float xinc,yinc,x,y; dx = x1-x0; dy= y1-y0; if (abs(dx) > abs(dy) steps = abs(dx); else steps = abs(dy); xinc= dx/steps; yinc= dy/steps; x = x0; y = y0; DrawPixel(round(x),round(y)); for (k = 1; k <= steps; k++) { x = x + xinc; y = y + yinx; DrawPixel(round(x),round(y)); } 4/28/2012 }

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

Designed By Virendra Singh Kushwah

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 time step? What are the pixels colored, according to the DDA algorithm?
4/28/2012

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 x 2 3 4 5 6 7 8 9 10 11 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
10

Designed By Virendra Singh Kushwah 10 12

DDA Algorithm (continued)

Y_inc

X_inc

but floating point operations and rounding operations are expensive

4/28/2012

Designed By Virendra Singh Kushwah

11

Advantage: faster method for calculating pixel position then the equation of a pixel position. Y=mx+b Disadvantage: The accumulation of round of error is successive addition of the floating point increments is used to find the pixel position but it take lot of time to compute the pixel position.
4/28/2012 Designed By Virendra Singh Kushwah 12

Bresenhams Algorithm
Uses only integer calculations Uses distance between ideal y-coordinate and the upper and lower pixel

dupper dlower

4/28/2012

Designed By Virendra Singh Kushwah

13

4/28/2012

Designed By Virendra Singh Kushwah

14

4/28/2012

Designed By Virendra Singh Kushwah

15

Bresenhams Algorithm
1. 2. 3. 4. 5. Input the two line endpoints and store left endpoint as (x0,y0) Pre-calculate the values dx, dy, 2dy and 2dy - 2dx Color pixel (x0,y0) Let p0 = 2dy dx At each xk along the line, starting with k=0: If pk<0, then the next point to plot is (xk + 1,yk), and pk+1 = pk + 2dy

Otherwise, the next point to plot is (xk + 1, yk + 1), and pk+1 = pk + 2dy 2dx
6. Repeat Step-4 dx times

4/28/2012

Designed By Virendra Singh Kushwah

16

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 time step? 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 4 5 6 7 p 0 -10 0 -10 0 -10 0 -10 P(x) 2 3 4 5 6 7 8 9 2dy = 10 2dy 2dx = -10 P(y) 3 4 4 5 5 6 6 7

8
9
4/28/2012

0
-10 0

10
11 12

7
8 8
17

Designed By Virendra Singh Kushwah

10

Thanking You
4/28/2012 Designed By Virendra Singh Kushwah 18

Вам также может понравиться