How Does Computer Draw Line?: - Screen Made of Pixels - High-Level Language Specifies Line - System Must Color Pixels
How Does Computer Draw Line?: - Screen Made of Pixels - High-Level Language Specifies Line - System Must Color Pixels
How Does Computer Draw Line?: - Screen Made of Pixels - High-Level Language Specifies Line - System Must Color Pixels
Screen made of pixels High-level language specifies line System must color pixels
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
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
Y_inc
X_inc
Bresenhams Algorithm
Uses only integer calculations Uses distance between ideal y-coordinate and the upper and lower pixel (assuming gentle slope)
dupper dlower
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
Switch Point 0 and Point 1 if necessary If negative slope, reflect If steep slope, flip y and x
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