Professional Documents
Culture Documents
Scanconversion Derivation
Scanconversion Derivation
SCAN CONVERSION
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.
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
s = y- yi
The distance from T to the actual line in the y direction is,
2
t = (yi+1)-y
If s-t < 0
If s-t > 0
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) + 2b - 2yi - 1
Decision variable ,
di= ∆x(s - t)
= ∆x { 2 (∆y/∆x)(xi + 1) + 2b - 2yi – 1}
Similarly, we can write the decision variable di +1 for next step i+1 as,
3
If the chosen pixel is the top pixel T (meaning that di ≥ 0) then yi+1 = yi + 1 and so,
= 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 + 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 :
= ∆x [ 2( mx1 + b - y1 ) + 2m – 1]
= ∆x [ 2*0 + 2m – 1]
=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 11: {
Line 14: }
Line 16: {
Line 20: }
5
Line 21: }
Line 23: }
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
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)
We can also write the decision variable di+1 for the next step i+1 :
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 yi -1
If T is the chosen pixel (meaning that di < 0) then yi+1 = yiand so,
= 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,
Hence, we have,
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 :
= 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 12 :y--;
Line 13 :}
Line 14 :x++;
Line 15 :}
Otherwise, the mid position is outside or on the circle boundary, and we choose pixel S.
= (xi + 1 + 1)2 + (yi+1 -1/2)2 - (xi + 1)2 - (yi -1/2)2 [ since xi+1 = xi + 1 ]
So,
If T is the chosen pixel (meaning that pi < 0) then yi+1 = yi and so,
= 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 + 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,
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 :