3Line-Scan Conversion Algorithms Line scan conversion: determine the nearest pixel position (integer) along the line between the two endpoints and store the color for each position in the frame buffer.Three common algorithmsDDAMidpointBresenham
6Naive Idea Costly floating point computations !! void NaiveLine(int x0,int y0,int xend,int yend,int color) int x; float y, m, b; m=(yend-y0)/(xend-x0); b = y0 – m*x0; for (x=x0; xxend; x++) drawpixel (x, int(y+0.5), color); y=m*x+b; Costly floating point computations !!Multiplications, additions, roundings
7DDA (Digital Differential Analyzer) ALGORITHM The digital differential analyzer (DDA) samples the line at unit intervals in one coordinate corresponding integer values nearest the line path of the other coordinate.The following is the basic scan-conversion(DDA) algorithm for line drawing (sample at unit x intervals )for x from x0 to xendCompute y=mx+bDraw_fn(x, round(y))How to improve???
8Reuse Previous Value Increment For Start from x=x0 and y=y0, every position (x,y) can be computed by incrementation, x by 1 and y by m.Mxi+mdx+b=mxi+b+ mdx= yi +mdx, untuk oktan 1 ketika garis agak landai lompatan lebih rapat adalah x
9No more multiplications, but still have fp void DDALine(int x0,int y0,int xend,int yend,int color) int x; float dx, dy, y, m; dx = xend-x0, dy=yend-y0; m=dy/dx; y=y0; for (x=x0; xxend; x++) drawpixel (x, int(y+0.5), color); y=y+m; No more multiplications, but still have fpadditions, roundings
12How about the other cases? The above DDALine only suit the condition that xend>x0 and 0 < m < 1 (octant #1)How about the other cases?|m| > 1xend < x0?
13Octant#2: xend>x0 and 1 < m < ∞ #3#1#4m=0#5#8#6#7
14Octant#2 and #3: reverse the role of x as iterator by y and increment x by 1/m Octant#4: reverse the end points octant#8Octant#5: reverse the end points octant#1Octant#6: reverse the end points octant#2Octant#7: reverse the end points octant#3Octant#8: Just same DDA algorithm for octant#1
15Has rounding operations the accumulation of error Major deficiency in the above approach :Uses floatsHas rounding operationsthe accumulation of error
16According the position of M and Q, choose the next point Pt or Pb Midpoint AlgorithmBasic thought( 0 < m < 1)MPTPBP=(x,y)QPi(xi,yi)M(xi+1,yi+0.5)According the position of M and Q, choose the next point Pt or Pb
17Line equation ( (x0,y0) , (xend,yend) ) Dari y=mx+k terus persamaan y-mx-k=f(x,y) di bawah negatif, di atas positifFor any point (x, y):F(x,y) = 0 (x,y) is on the lineF(x,y) > 0 (x,y) is above the lineF(x,y) < 0 (x,y) is beneath the line
18Discriminant Function d The function is made by using the midpoint M:d < 0, M is beneath Q, P2 is next point;d > 0, M is above Q, P1 is the next point;d = 0, P1 or P2 is right, commonly P1P2QP=(x,y)MPi(xi,yi)P1
19Is it (computing the d) costly. No Is it (computing the d) costly? No! We can use the idea in the DDA: use previous d value to compute next one.
20Incrementation thought If d0，then choose the next point: P1 (xp+1, yp), In order to judge the next point successively，calculateincrement of d is aIf d<0，then choose the next point:P2 (xp+1, yp+1)。In order to judge the next point successively ,calculateincrement of d is a+b
21Initial value of dIn each of iterationIf d >= 0Else if d < 0