CS 248: Midterm Solutions

Question 5

Grader: Matt

Points were given for both correctness and efficiency. Correctness criteria included:

Efficiency was basically based on whether you came up with a reasonable algorithm based on finite differences. Writing the implicit function properly gave partial credit: 0 = y^2 - 2^x + 1, and computing f(x+1, y) - f(x, y) = -2^x and f(x, y+1) - f(x, y) = 2y + 1 also gave partial credit.

Based on finite differences, an efficient rasterizer can be written like this:

void rasterize(int n)
{
  int x = 0;
  int y = 0;
  int e = 0;

  for (int i = 0; i < n; ++i) {
    point(x, y);
    e += 2 * y + 1;
    ++y;
    if (e > 0) {
      e -= 1 << x;
      ++x;
    }
  }
}
A common error was to switch the places of x and y; always taking a step in x; this is incorrect, since it causes large gaps in the curve as x gets large.

The following table shows the first few values of the curve as well as the points that the rasterizer should draw and the value of e. Note that for the first few pixels, the points plotted are actually above the curve. However, since the instructions said to ignore half-pixel offsets, the above rasterizer is a good enough solution. Some people wrote rasterizers that had a special case for x < 4; this answer was fine, too, though not necessary for full credit.

xy(float)ye
0000
1110
221.732051
332.645752
443.872981
555.56776-6
667.93725-27
677.93725-14
7811.2694-63
7911.2694-46
71011.2694-27
71111.2694-6

The above rasterizer can be improved further by incrementally computing the adjustments to e, however the above version was sufficient for full credit.

void rasterize(int n)
{
  int x = 0;
  int y = 0;
  int e = 0;
  int dedy = 1;
  int dedx = -1;

  for (int i = 0; i < n; ++i) {
    point(x, y);
    e += dedy;
    dedy += 2;
    ++y;
    if (e > 0) {
      e += dedx;
      dedx *= 2;
      ++x;
    }
  }
}
A few people came up with more creative solutions; as long as they were both correct and efficient, credit was given.