Console Draw is a simple console version of a drawing program.
One of the popular tasks for tech interview is an implementation of application that draws in console. Here it is :)
-
Install Bundler and dependencies
$ gem install bundler $ bundle install
-
Run specs
$ rspec
-
Run application with
./bin/console_draw
-
Create a canvas
C 10 10
-
Draw a rectangle with
R 2 2 9 9
-
Draw lines with
L 9 9 2 2 L 2 9 9 2
-
Fill areas
B 5 3 i B 5 8 i B 3 5 o B 8 5 o
The result is
------------
| |
| xxxxxxxx |
| xxiiiixx |
| xoxiixox |
| xooxxoox |
| xooxxoox |
| xoxiixox |
| xxiiiixx |
| xxxxxxxx |
| |
------------
-
Clean the canvas with
C
-
Draw a circle with with
CIRCLE 5 5 3
-
Quit the application with
Q
Points are generated by figures' algorithms located in the folder lib/figures
. Every figure must inherits from ConsoleDraw::Figures::Base
and implements common method #calculate_coordinates
that returns an array of coordinates.
Line figure generates points by algorithm of Bresenham for raster image. Input parameters for a line are coordinates of the first and the last points.
Rectangle figure uses Line figures as edges to generate points. Input parameters for a rectangle are coordinates of an upper left and a lower right corners.
Circle figure generates points by algorithm of Bresenham for raster image. Input parameters for a circle are coordinates of the center and radius.
Canvas is a storage of points generated by figures. A figure might be drawn on a canvas object with a method #draw
.
Points map of canvas is rendered to string by class ConsoleDraw::Render::StringRenderer
using "Raster scan" technique.
Instantiates a command executor and sends user's input to the command executor. Command executor has a Context object, that provides an interface to draw on canvas and render canvas as String.
Corner cases are respected - available commands are whitelisted, implemented a confirmation for too big parameters (for example, attempt to create a canvas 200 * 200 requires a confirmation). If user's input is invalid, application should return an error message and fail a command.