Explain Two Speed Hacks
Most everything in our Draw( ) method is straightforward, with two exceptions.
int xflag = 0;
for (x = xMin; x <= xMax; x++) {
if ((e0|e1|e2) >= 0) {
r.pixel[y+x] = color;
xflag++;
} else if (xflag != 0) break;
e0 += A0;
e1 += A1;
e2 += A2;
}
All three edges are tested with a single comparison
by oring together the three edges and checking if the
result is positive. If any one of the three is negative
then its sign-bit will be set to a 1, and the result of
the or will be negative.
Since triangles are convex, we can only be inside
for a single interval on any given scanline. The
xflag variable is used to keep track of when we
exit the triangle's interior. If ever we find ourselves
outside of the triangle having already set some pixels
on the span then we can skip over the remainder of the
scanline.
|