2. Midpoint algorithm is an incremental algorithm.
Candidate Pixels
Current Pixel
( xk, yk)
Midpoint
Line
Coordinates of Midpoint = ( xk+1, yk+(1/2) )
( xk+1, yk)
( xk+1, yk+1)
3. Equation of the line:
y-y1/x-x1=y2-y1/x2-x1 ----(1)
Let w = x2 x1, and h = y2 y1
By using equation (1)
y-y1/x-x1=h/w
Then, h (x x1) w (y y1) = 0.
(h, w , x1, y1 are all integers).
In other words, every point (x, y) on the line satisfies the
equation F(x, y) =0, where
F(x, y) = h (x x1) w (y y1).
A(x1,y1)
B(x2,y2)
4. Decision criteria
F(MP) = Fk =F(xk+1, yk+ ½) ------ (Notation)
If Fk < 0 : The midpoint is above the line. So the
next pixel is (xk+1, yk).
If Fk 0 : The midpoint is below or on the line. So
the next pixel is (xk+1, yk+1).
5. Choice of the next pixel
Midpoint Below Line
Next pixel = (xk+1, yk+1)
Fk > 0
yk+1 = yk+1
Midpoint Above Line
Next pixel = (xk+1, yk)
Fk < 0
yk+1 = yk
6. Fk = F(xk+1, yk+ ½)
By using equation,
Fk = h (x x1) w (y y1)
Fk = h (xk+1 x1) w (yk+½ y1)------(1)
But, Fk+1 = ( xk+1+1, yk+1+(1/2) )
=h(xk+1+1-x1)-w( yk+1+1/2-y1)
= h(xk+2-x1)-w( yk+1+1/2-y1)
Now,
Fk+1 -Fk =h w (yk+1 yk)
= Fk + h w (yk+1 yk)
7. So,
Fk< 0 : yk+1 = yk.
Hence, Fk+1 = Fk + h .
Fk 0 : yk+1 = yk+1.
Hence, Fk+1 = Fk + h w.
Now, putting k=0 in equation (1), F0 = h w/2.
8. int h = y2-y1;
int w = x2-x1;
float F=h-w/2;
int x=x1, y=y1;
for (x=x1; x<=x2; x++)
{
setPixel(x, y);
if(F < 0)
F = F+h;
else{
F = F+h-w;
y++;
}
}
9. QUES: Find the intermediate pixels of the point (1,2) and
(6,6) using mid point line drawing algorithm?
ANS: Given,
x1=1,y1=2
x2=6,y2=6
now,
h=y2-y1=6-2=4
w=x2-x1=6-1=5
f=(2*h)-w=(2*4)-5=3
Points are: (1,2); (2,3); (3,4); (4,5); (5,6); (6,6)