 # Scan Conversion Algorithms www.smartphonetech.org.

## Presentation on theme: "Scan Conversion Algorithms www.smartphonetech.org."— Presentation transcript:

Scan Conversion Algorithms www.smartphonetech.org

Line Drawing  Description :  Given the specification of a straight line, find the collection of addressable pixels which closely approximate this line.  Goals :  Straight lines should appear straight  Lines should start and end accurately  Lines should be drawn as quickly as possible.  Line drawing algorithms:  DDA (Digital Differential Analyzer) Algorithm  Bresenham’s Line Algorithm www.smartphonetech.org

DDA Algorithm  Consider the slope-intercept equation of a line y = m. x + b where m  slope & b  y-intercept  Given two end-points (x 1,y 1 ) & (x 2,y 2 ) m = (y 2 -y 1 ) / (x 2 -x 1 ) b = y1 – m. x 1  Now, given any x-interval or y-interval we can calculate the corresponding y-interval or x- interval through the slope ∆y = m. ∆x ∆x = ∆y / m  Thus considering an interval of 1 pixel, we have y k+1 = y k + m x k+1 = x k + 1/m  If m > 1, we should calculate x otherwise we should calculate y www.smartphonetech.org

void lineDDA(int x1, int y1, int x2, int y2){ int dx = x2 –x1, dy = y2 –y1, steps, k; float x = x1, y = y1, xIncr, yIncr; if(abs(dx) > abs(dy)) setps = abs(dx); else steps = abs(dy); xIncr = dx / steps; yIncr = dy / steps; setPixel(Round(x), Round(y)); for(k = 0; k<steps; k++){ x += xIncr; y += yIncr; setPixel(Round(x), Round(y)); } } DDA Algorithm (cont.) x 1, y 1 x 2, y 2 x 1, y 1 dy dx 1 2 www.smartphonetech.org

 Also called Midpoint Line Algorithm.  Considering slope and end point ordering, a line can belong to one of the eight octants. Given the current pixel, which one do we chose next : E or NE (for octant 1) is based on the Midpoint. If the line goes above the midpoint, M then NE is chosen otherwise E is chosen as the next pixel.  Let us consider two forms of a Line equation y = (dy / dx). x + B F(x,y) = a. x + b. y + c = 0  Thus we get a = dy, b = - dx & c = B. dx ------- (1)  Again the midpoint M is below the line if F(M) > 0 otherwise M is above the line. Bresenham’s Line Algorithm E NE M 1 23 4 5 6 7 8 www.smartphonetech.org

 The value of F(M) can be calculated in an incremental way as follows:  Consider a decision variable d = F(x p +1, y p + ½)  The decision variable helps to chose E or NE. If d <= 0 chose E otherwise chose NE.  The next decision variable is calculated as follows: Set d old = d Case E is chosen: (current point is x p + 1, y p ) d new = F(x p + 2, y p + ½) = a. x p +2a + b. y p + b/2 +c (∆d) E = d new – d old = a = dy Case NE is chosen: (current point is x p + 1, y p + 1) d new = F(x p + 2, y p + 3/2) = a. x p +2a + b. y p + 3b/2 +c (∆d) NE = d new – d old = a + b = dy – dx Bresenham’s Line Algorithm (cont.) x p +1,y p E NE M x p, y p x p +1,y p +1 x p +1,y p +1/2 www.smartphonetech.org

 The initial decision value of the decision variable is calculated as d start = F(x 0 +1, y 0 +1/2) = a. x 0 + a + b. y 0 + b/2 + c = a + b/2 = dy – dx/2 = ½ (2dy - dx)  To avoid fractional computation we can assume decision variable values for 2.F(x,y). Thus d start = 2dy – dx (∆d) E = 2dy (∆d) NE = 2(dy –dx)  Thus we can plot the successive points as follows while(x < x1){ if(d<=0) /*chose E */ d = d + (∆d) E ; else{ /* chose NE */ d = d + (∆d) NE ; y = y + 1; } x = x + 1; setPixel(x, y); } Bresenham’s Line Algorithm (cont.) www.smartphonetech.org

Bresenham’s Line Algorithm (cont.)  What’s about lines belong to other octants? 1 23 4 5 6 7 8 OctantChange 1None 2Swap x & y 3Plot from P 1 to P 0 ; Swap x & y; Use y = y - 1 4Plot from P 1 to P 0 ; Use y = y - 1 5Plot from P 1 to P 0 6Plot from P 1 to P 0 ; Swap x & y 7Swap x & y; Use y = y - 1 8Use y = y - 1 www.smartphonetech.org

 Due to symmetry in circle, it needs to plot only octant 2 Others can be plotted though mirror effect. Thus setCirclePixel(x c, y c, x, y){ setPixel(x c + x, y c + y);  2 setPixel(x c - x, y c + y);  3 setPixel(x c - y, y c + x);  4 setPixel(x c - y, y c - x);  5 setPixel(x c - x, y c - y);  6 setPixel(x c + x, y c - y);  7 setPixel(x c + y, y c - x);  8 setPixel(x c + y, y c + x);  1 }  Now for octant 2 choice is between E and SE and the decision function is F(x, y) = x 2 +y 2 –R 2 = 0; Midpoint Circle Algorithm 1 23 4 5 6 7 8 E SE M www.smartphonetech.org

 The decision variable now becomes d = F(x p + 1, y p – ½) (∆d) E = F(x p + 2, y p – ½) - F(x p + 1, y p – ½) = 2 x p + 3 (∆d) SE = F(x p + 2, y p – 3/2) - F(x p + 1, y p – ½) = 2 x p - 2y p + 5 d start = F(x0 + 1, y0 – ½) = F(1, R – ½) = 5/4 – R  To get rid of the fraction, let h = d – ¼ => h start = 1 – R  Since h is initialized and incremented by integers we can use h instead of d for the decision. Midpoint Circle Algorithm (cont.) E SE M www.smartphonetech.org

 The algorithm x = 0; y = R; h = 1 – R; setCirclePixel(x c, y c, x, y); while(y > x){ if(h <= 0) /* chose E */ h = h + 2x + 3; else{ /* chose SE */ h = h + 2(x – y) + 5; y = y – 1; } x = x + 1; setCirclePixel(x c, y c, x, y); } Midpoint Circle Algorithm (cont.) www.smartphonetech.org

Midpoint Ellipse Algorithm a b -b -a Slope = -1 R2 R1 E SE M S M setEllipsePixel(x c, y c, x, y){ setPixel(x c + x, y c + y);  1 setPixel(x c - x, y c + y);  2 setPixel(x c - x, y c - y);  3 setPixel(x c +x, y c - y);  4 } www.smartphonetech.org

 Let us consider the following ellipse equation F(x, y) = b 2 x 2 + a 2 y 2 –a 2 b 2 = 0  Slope of the equation at a point (x, y) is dy/dx = - b 2 x/a 2 y  The magnitude of the slope is 0 in (0, b) and gradually increments and finally becomes infinity at (a, 0)  Thus the condition for R1 is b 2 x = a 2 y  Analysis for R1:  The decision variable becomes d = F(x p + 1, y p – ½) (∆d) E = F(x p + 2, y p – ½) - F(x p + 1, y p – ½) = b 2 (2x p + 3) (∆d) SE1 = F(x p + 2, y p – 3/2) - F(x p + 1, y p – ½) = b 2 (2x p + 3) -2a 2 (y p - 1) d start = F(1, b – ½ ) = b 2 + a 2 (1/4 – b)  Analysis for R2:  The decision variable becomes d = F(x p + ½, y p – 1) (∆d) SE2 = F(x p + 3/2, y p – 2) - F(x p + ½, y p – 1) = 2b 2 (x p + 1) – a 2 (2y p - 3) (∆d) S = F(x p + ½, y p – 2) - F(x p + ½, y p – 1) = - a 2 (2y p - 3) Midpoint Ellipse Algorithm (cont.) a b -b -a E SE M S M www.smartphonetech.org

 The Algorithm: sa = sqr(a); sb = sqr(b); d = b 2 + a 2 (1/4 – b); setEllipsePixel(x c, y c, 0, b); while(b 2.(x+1) < a2.(y – ½ )){ /* For R1 */ if(d<0) /* chose E */ d += sb. ((x << 1)+3); else{ /* chose SE */ d += sb. ((x << 1)+3) – sa. ((y<<1) - 2) ; y--; } x++; setEllipsePixel(x c, y c, x, y); } Midpoint Ellipse Algorithm (cont.) d = sb. sqr(x+1/2) + sa. sqr(y-1) – sa. sb; while(y>0){ /* For R2 */ if(d<0){ /* chose SE */ d += sb. ((x++)+2) – sa. ((y<<1)-3); x++; }else{ d -= sa. ((y<<1)-3); } y--; setEllipsePixel(x c, y c, x, y); } www.smartphonetech.org

Thank You www.smartphonetech.org www.smartphonetech.org