Skip to content

SamMolokanov/console_draw

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Console Draw

Console Draw is a simple console version of a drawing program.

Notes

One of the popular tasks for tech interview is an implementation of application that draws in console. Here it is :)

Getting Started

  1. Install Bundler and dependencies

     $ gem install bundler
     $ bundle install
    
  2. Run specs

     $ rspec
    

Using

  1. Run application with

     ./bin/console_draw
    
  2. Create a canvas

     C 10 10
    
  3. Draw a rectangle with

     R 2 2 9 9
    
  4. Draw lines with

     L 9 9 2 2
     L 2 9 9 2
    
  5. 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 |
|          |
------------
  1. Clean the canvas with

     C
    
  2. Draw a circle with with

     CIRCLE 5 5 3
    
  3. Quit the application with

     Q
    

Details

Supported figures

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

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

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

Circle figure generates points by algorithm of Bresenham for raster image. Input parameters for a circle are coordinates of the center and radius.

Canvas

Canvas is a storage of points generated by figures. A figure might be drawn on a canvas object with a method #draw.

Renders

Points map of canvas is rendered to string by class ConsoleDraw::Render::StringRenderer using "Raster scan" technique.

CLI

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.

About

An example of application that draws in console

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages