Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

CHAPTER 3

SCAN CONVERSION

BRESENHAM’S LINE ALGORITHM

We assume that we want to scan-convert the line shown in the following fig, where 0<m<1.

We start with pixel P1(x1,y1) . Once a pixel is chosen at any step, the next pixel is either the one
to its right or the one to its right
and up due to the limit on m. The
line is best approximated by those
pixels that fall the least distance
from its true path between two
considered points.

The coordinates of the last chosen


pixel upon entering step i are (xi,yi).
Our task is to choose the next one
between the bottom pixel S and top
pixel T.

If S is chosen, we have

xi+1 = xi+1

and yi+1 = yi

If T is chosen, we have

xi+1 = xi+1

and yi+1 = yi +1

The actual y coordinates of the line atx =xi+1is,

y = m xi+1+b = m(xi+1)+b ………………………..(i)

The distance from S to the actual line in the y direction is,

s = y- yi
The distance from T to the actual line in the y direction is,
2

t = (yi+1)-y

Considering the difference between two distance values :s-t,

If s-t < 0

s < t and the closest pixel is S.

If s-t > 0

s > t and the closest pixel is T.

We also choose T when

s-t = 0

The difference is –

s -t = (y-yi)-[(yi+1)-y]

= y-yi-yi-1+y

= 2y-2yi-1

= 2(y-yi)-1

= 2 [ { m(xi + 1) + b } -yi) ] - 1 ……………. [from (i)]

=2 m(xi + 1) + 2b - 2yi - 1

= 2 (∆y/∆x)(xi + 1) + 2b - 2yi – 1 [ Since, m = ∆y/∆x ]

Decision variable ,

di= ∆x(s - t)

= ∆x { 2 (∆y/∆x)(xi + 1) + 2b - 2yi – 1}

= 2∆y(xi + 1) + ∆x (2b - 2yi – 1)

= 2∆yxi - 2∆xyi + 2∆y + 2b∆x - ∆x

di= 2∆yxi - 2∆xyi + c …………………..(ii)

Similarly, we can write the decision variable di +1 for next step i+1 as,
3

di+1 = 2∆yxi+1 - 2∆xyi+1 + c …………….(iii)

[ (iii) – (ii) ] =>

di+1 - di = 2∆y( xi+1- xi)- 2∆x( yi+1 – yi)

= 2∆y( xi + 1- xi)- 2∆x( yi+1 – yi) [since xi+1 = xi + 1 ]

= 2∆y- 2∆x( yi+1 – yi)

So, di+1 = di + 2∆y- 2∆x( yi+1 – yi)

If the chosen pixel is the top pixel T (meaning that di ≥ 0) then yi+1 = yi + 1 and so,

di+1= di + 2∆y- 2∆x( yi + 1 – yi)

= di + 2∆y - 2∆x

= di + 2(∆y - ∆x)

On the other hand, if the chosen pixel is the bottom pixel S (meaning that di < 0) then yi+1 =
yiand so,

di+1= di + 2∆y- 2∆x( yi – yi )

= di + 2∆y

= di + 2(∆y - ∆x)

Hence, we have

di + 2(∆y - ∆x) d i≥ 0
di+1 =
di + 2∆y d i< 0

Finally, we calculate d1 ,the base case value for this recursive formula, from the original
definition of the decision variable di :

di= ∆x [ 2m (x1 + 1) + 2b - 2y1 – 1]

= ∆x [ 2( mx1 + b - y1 ) + 2m – 1]

= ∆x [ 2*0 + 2m – 1]

= 2(∆y/ ∆x)* ∆x- ∆x


4

=2∆y- ∆x

In summary, Bresenham’s algorithm for scan-converting a line from p1(x1,y1) to p2(x2,y2) with x1<
x2 and 0 < m <1 can be stated as follows :

 Void Bresenham( )

 {

 Line 1: dx=x2-x1;

 Line 2: dy=y2-y1;

 Line 3: dT=2*(dy-dx);

 Line 4: dS=2*dy;

 Line 5: d=(2*dy)-dx;

 Line 6: putpixel(x1,y1);

 Line 7: while(x1<x2)

 Line 8: {

 Line 9: x1++;

 Line 10: if(d<0)

 Line 11: {

 Line 12: d=d+dS;

 Line 13: putpixel(x1,y1);

 Line 14: }

 Line 15: else

 Line 16: {

 Line 17: y1++;

 Line 18: d=d+dT;

 Line 19: putpixel(x1,y1);

 Line 20: }
5

 Line 21: }

 Line 22: putpixel(x2,y2);

 Line 23: }

BRESENHAM’S CIRCLE ALGORITHM

Scan-converting a circle using Bresenham’s


algorithm works as follows :

From fig, it can be noticed that, if points are


generated from 900 to 450 , each new point
closest to the true circle can be found by
taking either of two actions :

1. Move in the x direction one unit


or

2. Move in the x direction one unit and move in the negative y direction one unit.

Assume that (xi,yi) are the coordinates of the last scan-converted pixel upon entering step i .
Our task is to choose the next one between the bottom pixel S and top pixel T.

Let, the distance from origin to pixel T squared minus the distance to the true circle squared =
D(T)

Then, let the distance from origin to pixel S squared minus the distance to the true circle
squared = D(S)

As the coordinates of

T = (xi + 1,yi) and

S = (xi + 1,yi - 1), the following expressions can be developed :

D(T) = (xi + 1)2 + yi2 – r2

D(S) = (xi + 1)2 + (yi -1)2 – r2


6

Since D(T) will always be positive and D(S) will always be negative, a decision variable di may be
defined as follows :

di = D(T) + D(S)

=(xi + 1)2 + yi2 – r2 + (xi + 1)2 + (yi -1)2 – r2

=2(xi + 1)2 + yi2 +(yi -1)2 –2 r2

So, di =2(xi + 1)2 + yi2 +(yi -1)2 –2 r2…………………………………………..(i)

When di < 0 , we have |D(T)| < |D(S)| and pixel T is chosen.

When di ≥ 0 , we have |D(T)| ≥ |D(S)| and pixel S is chosen.

We can also write the decision variable di+1 for the next step i+1 :

di+1 = 2(xi+1 + 1)2 + yi+12 +(yi+1 -1)2 –2 r2………………………….(ii)

Hence, [ (ii) – (i) ] =>

di+1 - di= 2(xi+1 + 1)2 + yi+12 +(yi+1 -1)2 –2 r2 - 2(xi + 1)2 - yi2-(yi -1)2+2 r2

= 2(xi + 1 + 1)2 + yi+12 +(yi+1 -1)2 - 2(xi + 1)2 - yi2- (yi -1)2

[ since xi+1 = xi + 1 ]

= 2[ (xi + 1)2 + 2(xi + 1) + 1 ] + yi+12 + yi+12 -2yi+1 +1-2(xi + 1)2 - yi2 - yi 2 +

2 yi -1

= 2(xi + 1)2 + 4(xi + 1) + 2 +2 yi+12 -2yi+1-2(xi + 1)2 - yi2 - yi 2 + 2 yi

= 4xi + 4 + 2 +2 yi+12 -2yi+1 - 2yi 2 + 2 yi

= 4xi + 6 +2( yi+12- yi 2 ) – 2(yi+1 - yi)

So, di+1 = di + 4xi +2( yi+12- yi 2 ) – 2(yi+1 - yi)+ 6

If T is the chosen pixel (meaning that di < 0) then yi+1 = yiand so,

di+1 = di+ 4xi +2( yi2- yi 2 )– 2(yi-yi)+ 6

= di+ 4xi + 6
7

On the other hand, if the chosen pixel is the bottom pixel S (meaning that di ≥ 0) then yi+1 = yi-
1and so,

di+1 = di+ 4xi +2[( yi-1)2- yi 2 )– 2[(yi- 1-yi)]+ 6

= di+ 4xi +2[( yi2- 2yi + 1- yi 2 )+2+ 6

= di+ 4xi -4yi + 2 + 2+ 6

= di+ 4(xi -yi) + 10

Hence, we have,

di+ 4(xi -yi) + 10 d i≥ 0


di+1 =
di+ 4xi + 6 d i< 0

Finally, we set (0,r) to be the starting pixel coordinates and compute the base case value d 1 for
this recursive formula from the original definition of di :

d1=2(0 + 1)2 + r2 +(r-1)2 –2r2

= 2 + r2 + r2-2r +1 -2r2

= 3 – 2r

We can now summarize the algorithm for generating all the pixel coordinates in the 90 0to
450octant that are needed when scan-converting a circle of radius r.

 Line 1 : x = 0;

 Line 2 : y = r;

 Line 3 : d = 3 – 2r;

 Line 4 :while ( x ≤ y)

 Line 5 :{
8

 Line 6 :setPixel(x,y);

 Line 7 :if(d<0)

 Line 8 :d=d+4x+6;

 Line 9 :else

 Line 10 :{

 Line 11 :d=d+4(x – y)+10;

 Line 12 :y--;

 Line 13 :}

 Line 14 :x++;

 Line 15 :}

MIDPOINT CIRCLE ALGORITHM

Scan-converting a circle using Midpoint Circle algorithm works as follows :

Assume that (xi,yi) are the coordinates of the last


scan-converted pixel upon entering step i . Our task
is to choose the next one between the bottom pixel S
and top pixel T.

Our decision parameter is the circle function


evaluated at the midpoint between these two pixels.
Considering the coordinates of the point halfway
between pixel T and pixel S (xi +1, yi-1/2) known as
midpoint, we find our decision parameter :

pi = fcircle (xi +1, yi-1/2) = (xi +1)2 + (yi -1/2)2 – r2……………..(i)


9

If pi< 0 , this midpoint is inside the circle and we choose pixel T.

Otherwise, the mid position is outside or on the circle boundary, and we choose pixel S.

Similarly, the decision parameter for next step i+1 is

Pi+1 = fcircle(xi+1+1, yi+1-1/2)

= [xi+1+1]2 + (yi+1 -1/2)2 –r2………………………………..(ii)

Hence, [ (ii) – (i) ] =>

pi+1 - pi = (xi+1 + 1)2 + (yi+1 -1/2)2 – r2 - (xi + 1)2 - (yi -1/2)2 + r2

= (xi + 1 + 1)2 + (yi+1 -1/2)2 - (xi + 1)2 - (yi -1/2)2 [ since xi+1 = xi + 1 ]

= (xi + 1)2 + 2(xi + 1) + 1 + yi+12- yi+1+1/4 - (xi + 1)2 - yi 2 + yi -1/4

= 2(xi + 1) + 1 + yi+12- yi+1- yi 2 + yi

= 2(xi + 1) + 1 + (yi+12- yi 2 )– ( yi+1- yi )

So,

pi+1 = pi+ 2(xi + 1) + 1 + (yi+12- yi 2 )– ( yi+1- yi )

If T is the chosen pixel (meaning that pi < 0) then yi+1 = yi and so,

pi+1 = pi+ 2(xi + 1) + 1 + (yi2- yi 2 )– ( yi- yi )

= pi+ 2(xi + 1) + 1

= pi+ 2xi + 3

On the other hand, if the chosen pixel is the bottom pixel S (meaning that p i ≥ 0) then yi+1 = yi -1
and so,

pi+1 = pi + 2(xi + 1) + 1 + [(yi-1)2- yi 2 )]– (yi-1- yi )

= pi + 2(xi + 1) + 2+( yi 2 - 2 yi +1 - yi 2)

= pi + 2(xi + 1) + 2 - 2 yi +1

= pi + 2(xi + 1) +1 - 2 yi + 2
10

= pi + 2xi + 2 +1 – 2 yi + 2

= pi + 2(xi – yi)+ 5

Hence, we have,

pi+ 2xi + 3 p i< 0


pi+1 =
pi+ 2(xi – yi)+ 5 p i≥ 0

Finally, we compute the initial value for the decision parameter using the original definition of
piand (0,r) :

Pi = (0 + 1) 2 + ( r - ½ ) 2 - r2

= 1+ r2 - r + ¼ - r2

= 5/4 - r

This is not really integer computation. However, when r is an integer, we can simply set,

P1 = 1- r.

We can now summarize the algorithm for generating all the pixel coordinates in the 90 0 to 450
octant :

 Line 1 : Int x=0,y=r,p=1-r;


 Line 2 : While(x<=y)
 Line 3 : {
 Line 4 : setPixel(x,y);
 Line 5 : If(p<0)
 Line 6 : p=p+2x+3;
 Line 7 : Else
 Line 8 : {
 Line 9 : P=p+2(x-y)+5;
 Line 10 : y--;
 Line 11 : }
 Line 12 : x++;
 Line 1 3: }
11

You might also like