Forays into Anti-Aliased ASCII Art
Time limit1sMemory limit128 MB
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
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 , take and split it into a whole part (the largest integer that is at most ) and a fractional part , so that
For example, if then and ; if then and .
Let the gray level of a pixel be a value from to , where is pure white and is pure black. In column :
- If , shade pixel at gray level .
- If , shade pixel at gray level and pixel at gray level .
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, and , the slope and the intercept of the line . Both are given as floating-point numbers with at most two digits after the decimal point. lies between and inclusive, and lies between and inclusive.
A line whose and are both zero marks the end of the input and is not plotted.
Output
For each input line, draw a separate plot: a block of characters covering the region , 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:
The characters are period, colon, hyphen, equals, plus, lowercase t, lowercase w, asterisk, hash, percent, and at. Both and the gray level must be computed exactly.
Print the plot from the top of the region downward: the first row is and the last row is . 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 (_).