Drawing lines Line algorithm 1 DDA Simple but
Drawing lines
Line algorithm 1: DDA Simple, but uses floating point values Xdifference = (Xend-Xstart) Ydifference = (Yend-Ystart) y = Ystart delta_Y = Ydifference / Xdifference loop x Xstart to Xend putpixel(x, y) y = y + delta_y end_of_loop x
Line algorithm 2 Keep the fraction part of y in a separate variable, Yerror Start one half up. Xdifference = (Xend-Xstart) Ydifference = (Yend-Ystart) y = Ystart delta_Y = Ydifference / Xdifference Yerror = 0. 5 loop x Xstart to Xend putpixel(x, y) Yerror = Yerror + delta_y if Yerror >= 1 then y=y+1 Yerror = Yerror - 1 end_if end_of_loop x
Line algorithm 3, integer math Multiply Yerror with 2*Xdifference = (Xend-Xstart) Ydifference = (Yend-Ystart) y = Ystart Yerror = (0. 5*2)*Xdifference loop x Xstart to Xend putpixel(x, y) Yerror = Yerror + 2*Ydifference if Yerror >= 2*Xdifference then y=y+1 Yerror = Yerror - 2*Xdifference end_if end_of_loop x
Line algorithm 4 Subtract 2*Xdifference from Yerror Xdifference = (Xend-Xstart) Ydifference = (Yend-Ystart) y = Ystart Yerror = -Xdifference loop x Xstart to Xend putpixel(x, y) Yerror = Yerror + 2*Ydifference if Yerror >= 0 then y=y+1 Yerror = Yerror - 2*Xdifference end_if end_of_loop x
Line algorithm 5, Bresenham Brushing up things a little Xdifference = (Xend-Xstart) Ydifference = (Yend-Ystart) Yerror = 2*Ydifference - Xdifference Step. East = 2*Ydifference Step. North. East = 2*Ydifference - 2*Xdifference plot_pixel(x, y) loop x Xstart to Xend if Yerror <= 0 then Yerror = Yerror + Step. East else y=y+1 Yerror = Yerror + Step. North. East end_if putpixel(x, y) end_of_loop x
Using symmetry (-y, x) (-x, y) (-x, -y) (-y, -x) (y, -x)
Filling polygons
Filling polygons Draw one scan-line at a time = scan conversion
Scan converting convex polygons Sort the edges, so we know which ones are left / right. Calculate left and right edge for each scanline (y-value) and fill between the left and the right edge values.
Sliver Be careful to only fill pixels inside the polygon, we do not want double edges. Standard is to include the low valued edges (left edge and first scan line) in the polygon. (Does not solve all problems. )
How to calculate the edge values? Bresenham? Requires integer endpoints => polygon jumps a pixel at a time when moving. (Solve using fixed point mathematics. ) Here we will use the simpler DDA algorithm instead.
A ”pixel-perfect” scan conversion algorithm Start by splitting your polygon with horizontal lines at each intermediate vertex and calculate the positions of any new vertices. Each part is now a special case of a figure limited by two arbitrary sloped (not horizontal) lines and two horizontal lines. Note that the horizontal edges may have length zero.
Linear interpolation p(a) = (1 - a)p 1 + a p 2=(X 2, Y 2) p 1=(X 1, Y 1) y(x) = kx + m k = dy/dx= (Y 2 - Y 1) / (X 2 - X 1) m = Y 1
Flat top and bottom (X 2 L, Y 2) (X 2 R, Y 2) height: H=Y 2 -Y 1 slope left/right: SL=(X 2 L-X 1 L)/H SR=(X 2 R-X 1 R)/H (X 1 L, Y 1) (X 1 R, Y 1)
Scan converting polygons x. L=X 1 L x. R=X 1 R y=ceil(Y 1) y. Skip=y-Y 1 x. L+=y. Skip*SL x. R+=y. Skip*SR (X 1 L, Y 1) y. Skip
Scan converting polygons hline(x. L, x. R, y) while y floor(Y 2) for x from ceil(x. L) to floor(x. R) putpixel(x, y); x+=1 end for y+=1 x. L+=SL*1 x. R+=SR*1 (X 1 L, Y 1) end while
Interpolated scan conversion When I interpolate colour, I like to think of the colour value as just another dimension. So instead of drawing polygons in 2 D, we draw them in 3 D (or rather 5 D) where the colour value is the z-dimension.
Bi-linear interpolation Linear interpolation in two directions. First we interpolate in the y-direction, then we interpolate in the x-direction. Unique solution for flat objects (triangles are always flat).
Interpolate the new variable over the polygon in just the same way as you did before using the colour slope: dc/dy. CSL=(C 2 L-C 1 L)/H CSR=(C 2 R-C 1 R)/H x. L=X 1 L x. R=X 1 R c. L=C 1 L c. R=C 1 R y=ceil(Y 1) y. Skip=y-Y 1 x. L+=y. Skip*SL x. R+=y. Skip*SR c. L+=y. Skip*CSL c. R+=y. Skip*CSR y. Skip (X 1 L, Y 1, C 1 L)
We must interpolate horizontally also by adding dc/dx while y floor(Y 2) dcdx=(c. R-c. L)/(x. R-x. L) x. Skip=ceil(x. L)-x. L c=c. L+x. Skip*dcdx for x from ceil(x. L) to floor(x. R) putpixel(x, y, c); x+=1 c+=dcdx end for y+=1 x. L+=SL*1 x. R+=SR*1 c. L+=CSL*1 c. R+=CSR*1 end while x. Skip hline(x. L, x. R, c. L, c. R, y)
- Slides: 21