Forays into Anti-Aliased ASCII Art

Time limit1sMemory limit128 MB

Summary
For each line y = mx + b, shade two pixels per integer x by fractional part, round gray levels to tenths, and print a 20x20 character plot.
Level

Medium4 of 10

Topics
Implementation, Simulation, Math
Solved
No attempts yet

Problem

Aliasing is the name for the artifacts that appear when a digital device samples an analog source at a finite resolution. It is a common problem in computer graphics, where straight lines and smooth curves look jagged once they are drawn as discrete pixels. For instance, a naive attempt to plot the line

y=mx+by = mx + b

might produce something like this:

        *** 
      **
   ***
***

which looks more like a staircase than a smooth line.

Aliasing (also called jaggies) can be reduced by anti-aliasing, where pixels are drawn in different shades of gray so that, seen from a distance, the image looks smoother.

The scheme used here shades two pixels in each column. For every integer xx, take y=mx+by = mx + b and split it into a whole part ywy_w (the largest integer that is at most yy) and a fractional part yfy_f, so that

yw+yf=y,0≤yf<1.y_w + y_f = y, \qquad 0 \le y_f < 1.

For example, if y=23.56y = 23.56 then yw=23y_w = 23 and yf=0.56y_f = 0.56; if y=−1.3y = -1.3 then yw=−2y_w = -2 and yf=0.7y_f = 0.7.

Let the gray level of a pixel be a value from 0.00.0 to 1.01.0, where 0.00.0 is pure white and 1.01.0 is pure black. In column xx:

  • If yf=0y_f = 0, shade pixel (x,yw)(x, y_w) at gray level 1.01.0.
  • If yf≠0y_f \ne 0, shade pixel (x,yw)(x, y_w) at gray level 1−yf1 - y_f and pixel (x,yw+1)(x, y_w + 1) at gray level yfy_f.

Write a program that draws anti-aliased lines with this scheme.

Input

The input contains several test cases. Each case is a single line with two numbers, mm and bb, the slope and the intercept of the line y=mx+by = mx + b. Both are given as floating-point numbers with at most two digits after the decimal point. mm lies between 0.000.00 and 0.500.50 inclusive, and bb lies between −20.00-20.00 and 20.0020.00 inclusive.

A line whose mm and bb are both zero marks the end of the input and is not plotted.

Output

For each input line, draw a separate plot: a 20×2020 \times 20 block of characters covering the region 0≤x<200 \le x < 20, 0≤y<200 \le y < 20 of the Cartesian plane. Any position the line does not touch is left blank (a space).

For each shaded pixel, take its gray level, round it to the nearest tenth (breaking ties by rounding up), and choose the matching character from this table:

Rounded gray level0.00.10.20.30.40.50.60.70.80.91.0
Character.:-=+tw*#%@

The characters are period, colon, hyphen, equals, plus, lowercase t, lowercase w, asterisk, hash, percent, and at. Both yfy_f and the gray level must be computed exactly.

Print the plot from the top of the region downward: the first row is y=19y = 19 and the last row is y=0y = 0. Print each of the 20 rows as its 20 characters, immediately preceded and followed by a vertical bar (|). After each plot, print one line of 22 underscores (_).

Examples1

  1. Example 1

    Input
    0.5 0
    0.2 10.0
    0 0
    
    Expected output
    |                    |
    |                    |
    |                    |
    |                    |
    |                    |
    |                    |
    |                    |
    |                    |
    |                    |
    |                   t|
    |                 t@t|
    |               t@t  |
    |             t@t    |
    |           t@t      |
    |         t@t        |
    |       t@t          |
    |     t@t            |
    |   t@t              |
    | t@t                |
    |@t                  |
    ______________________
    |                    |
    |                    |
    |                    |
    |                    |
    |                    |
    |                -+w#|
    |           -+w#@#w+-|
    |      -+w#@#w+-     |
    | -+w#@#w+-          |
    |@#w+-               |
    |                    |
    |                    |
    |                    |
    |                    |
    |                    |
    |                    |
    |                    |
    |                    |
    |                    |
    |                    |
    ______________________