21
Drawing lines

Drawing lines

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Drawing lines

Line algorithm 1: DDASimple, but uses floating point values

Xdifference = (Xend-Xstart)Ydifference = (Yend-Ystart)

y = Ystartdelta_Y = Ydifference / Xdifference

loop x Xstart to Xend putpixel(x, y) y = y + delta_yend_of_loop x

Line algorithm 2Keep the fraction part of y in a separate variable, Yerror

Start one half up.

Xdifference = (Xend-Xstart)Ydifference = (Yend-Ystart)

y = Ystartdelta_Y = Ydifference / XdifferenceYerror = 0.5loop 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 mathMultiply Yerror with 2*Xdifference

Xdifference = (Xend-Xstart)Ydifference = (Yend-Ystart)

y = YstartYerror = (0.5*2)*Xdifferenceloop x Xstart to Xend putpixel(x, y) Yerror = Yerror + 2*Ydifference if Yerror >= 2*Xdifference then y = y + 1 Yerror = Yerror - 2*Xdifference end_ifend_of_loop x

Line algorithm 4Subtract 2*Xdifference from Yerror

Xdifference = (Xend-Xstart)Ydifference = (Yend-Ystart)

y = YstartYerror = -Xdifferenceloop x Xstart to Xend putpixel(x, y) Yerror = Yerror + 2*Ydifference if Yerror >= 0 then y = y + 1 Yerror = Yerror - 2*Xdifference end_ifend_of_loop x

Line algorithm 5, BresenhamBrushing up things a little

Xdifference = (Xend-Xstart)Ydifference = (Yend-Ystart)

Yerror = 2*Ydifference - XdifferenceStepEast = 2*YdifferenceStepNorthEast = 2*Ydifference - 2*Xdifference

plot_pixel(x, y)loop x Xstart to Xend if Yerror <= 0 then Yerror = Yerror + StepEast else y = y + 1 Yerror = Yerror + StepNorthEast end_if putpixel(x, y)end_of_loop x

Using symmetry

(x,y)

(y,x)(-y,x)

(-x,y)

(-x,-y)

(-y,-x) (y,-x)

(x,-y)

Filling polygonsDraw 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.

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.)

Sliver

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 caseof a figure limited by two arbitrarysloped (not horizontal) lines andtwo horizontal lines.

Note that the horizontal edgesmay have length zero.

Linear interpolation

p1=(X1,Y1)

p2=(X2,Y2)

p() = (1- )p1 + p2

p1=(X1,Y1)

y(x) = kx + mk = dy/dx= (Y2- Y1) / (X2- X1)

m = Y1

Flat top and bottom

(X1L,Y1)

(X2L,Y2) (X2R,Y2)

(X1R,Y1)

height: H=Y2-Y1

slope left/right: SL=(X2L-X1L)/HSR=(X2R-X1R)/H

Scan converting polygons

(X1L,Y1)

xL=X1LxR=X1R

y=ceil(Y1)ySkip=y-Y1

xL+=ySkip*SLxR+=ySkip*SR ySkip

Scan converting polygons

(X1L,Y1)

while y floor(Y2) for x from ceil(xL)

to floor(xR) putpixel(x,y);

x+=1 end for y+=1 xL+=SL*1 xR+=SR*1end while

hline(xL,xR,y)

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 2D, we draw them in 3D (or rather 5D) where the colour value is the z-dimension.

Bi-linear interpolationLinear 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=(C2L-C1L)/HCSR=(C2R-C1R)/H

xL=X1LxR=X1RcL=C1LcR=C1R

y=ceil(Y1)ySkip=y-Y1

xL+=ySkip*SLxR+=ySkip*SRcL+=ySkip*CSLcR+=ySkip*CSR

ySkip

(X1L,Y1,C1L)

We must interpolate horizontally also by adding dc/dxwhile y floor(Y2) dcdx=(cR-cL)/(xR-xL) xSkip=ceil(xL)-xL c=cL+xSkip*dcdx

for x from ceil(xL) to floor(xR)

putpixel(x,y,c);x+=1c+=dcdx

end for

y+=1 xL+=SL*1 xR+=SR*1 cL+=CSL*1 cR+=CSR*1end while

xSkip

hline(xL,xR,cL,cR,y)