More Low-level Optimizations Note that y is now an integer. Can we represent the fraction as an integer? After we draw the first pixel (which happens outside our main loop) the correct fraction is: ` fraction = 1/2 + dy/dx` If we scale the fraction by 2*dx the following expression results: ` scaledFraction = dx + 2*dy` and the incremental update becomes: ` scaledFraction += 2*dy // 2*dx*(dy/dx)` and our test must be modified to reflect the new scaling ` if (scaledFraction >= 2*dx) { ... }` Lecture 5 Slide 14 6.837 Fall '98

Note that the y variable is now an integer. Next we discuss how to retain the fraction as an integer. After we draw the first pixel (which happens outside our main loop) the correct fraction value is:

fraction = 1/2 + dy / dx

If we scale the fraction by 2*dx the following expression results:

scaledFraction = dx + 2*dy,
and the incremental update becomes:

scaledFraction += 2*dy,
and our test must be modified to reflect the new scaling

if (scaledFraction >= 2*dx) { ... }.

This test can be made a test against a value of zero if the inital value of scaledFraction has 2*dx subtracted from it. Giving outside the loop:

OffsetScaledFraction = dx + 2*dy - 2*dx = 2*dy - dx,

and the inner loop becomes

OffsetScaledFraction += 2*dy
if (OffsetScaledFraction >= 0) { y = y +1; fraction -= 2*dx; }

The net result is that we might as well double the values of dy and dx (this can be accomplished with either an add or a shift).