400 likes | 791 Views
Mid Point Line Drawing Algorithm. Assist. Prof. Dr. Ahmet Sayar Computer Engineering Department Computer Graphics Course Kocaeli University Fall 2013. How to Improve It?. Question: if we draw the current pixel, where is the next pixel? Next column, E or NE direction.
E N D
Mid Point Line Drawing Algorithm Assist. Prof. Dr. Ahmet Sayar Computer Engineering Department Computer Graphics Course Kocaeli University Fall 2013
How to Improve It? Question: if we draw the current pixel, where is the next pixel? Next column, E or NE direction
Midpoint Algorithm • Given a point just drawn, determine whether we move E or NE on next step • Is the line above or below ? Below: move E Above: move NE
Two Issues • How to evaluate if the midpoint is above or below the line? • How to incrementally evaluate this?
Midpoint Algorithm – Implicit Forms • How do we tell if a point is above or below the line?
Midpoint Algorithm – Implicit Forms • How do we tell if a point is above or below the line?
Midpoint Algorithm – Implicit Forms • How do we tell if a point is above or below the line? Properties f(x,y)=0 (x,y) on the line f(x,y)<0 (x,y) below the line f(x,y)>0 (x,y) above the line
Two Issues • How to evaluate if the midpoint is above or below the line? • How to incrementally evaluate this? What about starting value? What is the middle point? (xL+1,yL+1/2)
Midpoint Algorithm What about starting value? (xL+1,yL+1/2) is on the line!
Midpoint Algorithm What about starting value? Multiplying coefficients by 2 doesn’t change sign of f
Midpoint Algorithm • Need value of to determine E or NE • Build incremental algorithm • Assume we have value of • Find value of if E chosen
Midpoint Algorithm Need value of to determine E or NE Build incremental algorithm Assume we have value of Find value of if E chosen Find value of if NE chosen 14/110
Midpoint Algorithm If E was chosen, find
Midpoint Algorithm If NE was chosen, find
Midpoint Algorithm – Summary x=xLy=yL d=xH - xL c=yL - yH sum=2c+d //initial value draw(x,y) while ( x < xH) // iterate until reaching the end point if ( sum < 0 ) // below the line and choose NE sum+= 2d y++ x++ sum += 2c draw(x,y)
Midpoint Algorithm – Summary x=xLy=yL d=xH - xL c=yL - yH sum=2c+d //initial value draw(x,y) while ( x < xH) // iterate until reaching the end point if ( sum < 0 ) // below the line and choose NE sum+= 2d y++ x++ sum += 2c draw(x,y) - Update the current pixel - Update the function value of midpoint 18/110
Midpoint Algorithm – Summary x=xLy=yL d=xH - xL c=yL - yH sum=2c+d //initial value draw(x,y) while ( x < xH) // iterate until reaching the end point if ( sum < 0 ) // below the line and choose NE sum+= 2d y++ x++ sum += 2c draw(x,y) - Draw the pixel based on the function value of midpoint 19/110
Midpoint Algorithm – Example x=xLy=yL d=xH - xL c=yL - yH sum=2c+d draw(x,y) while ( x < xH) if ( sum < 0 ) sum+= 2d y++ x++ sum += 2c draw(x,y) d =6 c = -2
Midpoint Algorithm – Example x=xLy=yL d=xH - xL c=yL - yH sum=2c+d draw(x,y) while ( x < xH) if ( sum < 0 ) sum+= 2d y++ x++ sum += 2c draw(x,y) d =6 c = -2
Midpoint Algorithm – Example x=xLy=yL d=xH - xL c=yL - yH sum=2c+d draw(x,y) while ( x < xH) if ( sum < 0 ) sum+= 2d y++ x++ sum += 2c draw(x,y) d =6 c = -2
Midpoint Algorithm – Example x=xLy=yL d=xH - xL c=yL - yH sum=2c+d draw(x,y) while ( x < xH) if ( sum < 0 ) sum+= 2d y++ x++ sum += 2c draw(x,y) d =6 c = -2
Midpoint Algorithm – Example x=xLy=yL d=xH - xL c=yL - yH sum=2c+d draw(x,y) while ( x < xH) if ( sum < 0 ) sum+= 2d y++ x++ sum += 2c draw(x,y) d =6 c = -2
Midpoint Algorithm – Example x=xLy=yL d=xH - xL c=yL - yH sum=2c+d draw(x,y) while ( x < xH) if ( sum < 0 ) sum+= 2d y++ x++ sum += 2c draw(x,y) d =6 c = -2
Midpoint Algorithm – Example x=xLy=yL d=xH - xL c=yL - yH sum=2c+d draw(x,y) while ( x < xH) if ( sum < 0 ) sum+= 2d y++ x++ sum += 2c draw(x,y) d =6 c = -2
Midpoint Algorithm – Example x=xLy=yL d=xH - xL c=yL - yH sum=2c+d draw(x,y) while ( x < xH) if ( sum < 0 ) sum+= 2d y++ x++ sum += 2c draw(x,y) d =6 c = -2
Midpoint Algorithm • Only integer operations • Exactly the same as the more commonly found Bresenham’s line drawing algorithm • Extends to other types of shapes (circles)
Midpoint Algorithm – Example x=xmin y=ymin d=xmax- xmin c=ymin- ymax sum=2c+d draw(x,y) while ( x < xmax) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)
Midpoint Algorithm – Example x=xmin y=ymin d=xmax- xmin c=ymin- ymax sum=2c+d draw(x,y) while ( x < xmax) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)