15
Scan Conversion Algorithms www.smartphonetech.org

Scan Conversion Algorithms

Embed Size (px)

Citation preview

Page 1: Scan Conversion Algorithms

Scan Conversion Algorithms

www.smartphonetech.org

Page 2: Scan Conversion Algorithms

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

Page 3: Scan Conversion Algorithms

DDA Algorithm

Consider the slope-intercept equation of a line

y = m . x + b where m slope & b y-intercept Given two end-points (x1,y1) & (x2,y2)

m = (y2-y1) / (x2-x1)

b = y1 – m . x1

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

yk+1 = yk + m

xk+1 = xk + 1/m

If m > 1, we should calculate x otherwise we should calculate y

www.smartphonetech.org

Page 4: Scan Conversion Algorithms

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

x1, y1

x2, y2

x2, y2

x1, y1

dy

dx

1

2

www.smartphonetech.org

Page 5: Scan Conversion Algorithms

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

Page 6: Scan Conversion Algorithms

The value of F(M) can be calculated in an incremental way as follows: Consider a decision variable

d = F(xp+1, yp + ½)

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 dold = d

Case E is chosen: (current point is xp + 1, yp)

dnew = F(xp + 2, yp + ½)

= a . xp +2a + b . yp + b/2 +c

(∆d)E = dnew – dold = a = dy

Case NE is chosen: (current point is xp + 1, yp + 1)

dnew = F(xp + 2, yp + 3/2)

= a . xp +2a + b . yp + 3b/2 +c

(∆d)NE = dnew – dold = a + b = dy – dx

Bresenham’s Line Algorithm (cont.)

xp+1,ypE

NE

M

xp,yp

xp+1,yp+1

xp+1,yp+1/2

www.smartphonetech.org

Page 7: Scan Conversion Algorithms

The initial decision value of the decision variable is calculated as

dstart = F(x0+1, y0+1/2)

= a . x0 + a + b . y0 + 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

dstart = 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

Page 8: Scan Conversion Algorithms

Bresenham’s Line Algorithm (cont.)

What’s about lines belong to other octants?

1

23

4

5

6 7

8

Octant Change

1 None

2 Swap x & y

3 Plot from P1 to P0; Swap x & y; Use y = y - 1

4 Plot from P1 to P0; Use y = y - 1

5 Plot from P1 to P0

6 Plot from P1 to P0; Swap x & y

7 Swap x & y; Use y = y - 1

8 Use y = y - 1

www.smartphonetech.org

Page 9: Scan Conversion Algorithms

Due to symmetry in circle, it needs to plot only octant 2

Others can be plotted though mirror effect. Thus

setCirclePixel(xc, yc, x, y){

setPixel(xc + x, yc + y); 2

setPixel(xc - x, yc + y); 3

setPixel(xc - y, yc + x); 4

setPixel(xc - y, yc - x); 5

setPixel(xc - x, yc - y); 6

setPixel(xc + x, yc - y); 7

setPixel(xc + y, yc - x); 8

setPixel(xc + y, yc + x); 1

} Now for octant 2 choice is between E and SE and the decision function is

F(x, y) = x2 +y2 –R2 = 0;

Midpoint Circle Algorithm

1

23

4

5

6 7

8

E

SEM

www.smartphonetech.org

Page 10: Scan Conversion Algorithms

The decision variable now becomes d = F(xp + 1, yp – ½)

(∆d)E = F(xp + 2, yp – ½) - F(xp + 1, yp – ½)

= 2 xp + 3

(∆d)SE = F(xp + 2, yp – 3/2) - F(xp + 1, yp – ½)

= 2 xp - 2yp + 5

dstart = F(x0 + 1, y0 – ½) = F(1, R – ½)

= 5/4 – R

To get rid of the fraction, leth = d – ¼ => hstart = 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

SEM

www.smartphonetech.org

Page 11: Scan Conversion Algorithms

The algorithm

x = 0;

y = R;

h = 1 – R;

setCirclePixel(xc, yc, 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(xc, yc, x, y);

}

Midpoint Circle Algorithm (cont.)

www.smartphonetech.org

Page 12: Scan Conversion Algorithms

Midpoint Ellipse Algorithm

a

b

-b

-a

Slope = -1

R2

R1

E

SEM

S SE

M

setEllipsePixel(xc, yc, x, y){ setPixel(xc + x, yc + y); 1 setPixel(xc - x, yc + y); 2 setPixel(xc - x, yc - y); 3 setPixel(xc +x, yc - y); 4}

www.smartphonetech.org

Page 13: Scan Conversion Algorithms

Let us consider the following ellipse equation

F(x, y) = b2x2 + a2y2 –a2b2 = 0

Slope of the equation at a point (x, y) is dy/dx = - b2x/a2y

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 b2x < a2y and for R2 b2x >= a2y

Analysis for R1: The decision variable becomes

d = F(xp + 1, yp – ½)

(∆d)E = F(xp + 2, yp – ½) - F(xp + 1, yp – ½) = b2(2xp + 3)

(∆d)SE1 = F(xp + 2, yp – 3/2) - F(xp + 1, yp – ½) = b2(2xp + 3) -2a2(yp - 1)

dstart = F(1, b – ½ ) = b2 + a2(1/4 – b)

Analysis for R2: The decision variable becomes

d = F(xp + ½ , yp – 1)

(∆d)SE2 = F(xp + 3/2, yp – 2) - F(xp + ½, yp – 1) = 2b2(xp + 1) – a2(2yp - 3)

(∆d)S = F(xp + ½ , yp – 2) - F(xp + ½, yp – 1) = - a2(2yp - 3)

Midpoint Ellipse Algorithm (cont.)

a

b

-b

-a

E

SEM

S SE

M

Page 14: Scan Conversion Algorithms

The Algorithm:sa = sqr(a); sb = sqr(b);

d = b2 + a2(1/4 – b); setEllipsePixel(xc, yc, 0, b);

while(b2.(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(xc, yc, 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(xc, yc, x, y);}

www.smartphonetech.org

Page 15: Scan Conversion Algorithms

Thank You

www.smartphonetech.org