Line Drawing Algorithms: by Virendra Singh Kushwah
Line Drawing Algorithms: by Virendra Singh Kushwah
4/28/2012
4/28/2012
4/28/2012
4/28/2012
4/28/2012
Line Concept
Line Equation The Cartesian slop-intercept equation for a straight line is y=mx+b with m->slope b->y intercept -----(1)
4/28/2012
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 )
4/28/2012
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)
For each step, round off x and y to nearest integer, and color pixel
4/28/2012
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
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
Y_inc
X_inc
4/28/2012
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
13
4/28/2012
14
4/28/2012
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
16
8
9
4/28/2012
0
-10 0
10
11 12
7
8 8
17
10
Thanking You
4/28/2012 Designed By Virendra Singh Kushwah 18