SLIDE 5 Midpoint Line Drawing (cont.) y = x + B dy dx
Implicit form of a line: f(x,y) = ax + by + c = 0 Decision Variable : f = f(M) = f(xp +1,yp +1/2) = a(xp +1) + b(yp +1/2) + c The sign of f defines the move f(x,y) = dy x - dx y + B dx = 0 f(x,y) = 0 f(x,y) > 0 f(x,y) < 0 If was chosen How to update f - the value at M Mi = (x,y) Mi+1 = (x+1,y), thus = ax + by + c, and = a(x+1) + by + c, or = + a. Since a is constant we denote it with 'h, and we have: f += 'h M M M
i
f
1 i
f
1 i
f
i
f
If was chosen How to update f - the value at M Mi = (x,y) Mi+1 = (x+1,y+1), thus = ax + by + c, and = a(x+1) + b(y+1) + c, or = + a + b. Since a and b are constants we denote their sum with 'd, and we have: f += 'd M M M
i
f
1 i
f
1 i
f
i
f
Incremental Algorithm:
Initialization: First point = (x0,y0), first MidPoint = (x0+1,y0+1/2) fstart = f(x0 +1,y0+1/2) = a(x0 +1) + b(y0 +1/2) +c = ax0 + by0 + c + a + b/2 = f(x0,y0) + a + b/2 = a + b/2 dstart =dy - dx/2 Enhancement: To eliminate fractions, define: f(x,y) = 2(ax + by + c) = 0 dstart =2dy - dx
Mid-point Line Algorithm
h = 2dy;
d = 2dy - 2dx;
f = 2dy - dx; set_pixel(x1,y1); x = x1; y = y1; while (x++ < x2){ if (f < 0) f += h; else { f += d; y++; } set_pixel(x,y); }
- The sign of f(x0+1,y0+1/2) indicates
whether to move East or North-East.
- At the beginning d=f(x0+1,y0+1/2)=2dy-dx.
- The increment in d (after this step) is:
– If we moved East: E=2dy – If we moved North-East: 1(=2dy-2dx
– Integer arithmetic (dx and dy are integers). – One addition for each iteration. – No accumulated errors.
Midpoint Line Drawing - Summary