Решение на Трета задача от Николай Каращранов

Обратно към всички решения

Към профила на Николай Каращранов

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 20 успешни тест(а)
  • 49 неуспешни тест(а)

Код

module Graphics
class Renderers
class Ascii
end
module Html
end
end
class Canvas
def initialize(width, height)
@width = width
@height = height
@matrix = Array.new(height) { Array.new(width, 0) }
end
def width()
@width
end
def height()
@height
end
def set_pixel(x,y)
@matrix[y][x] = 1
end
def pixel_at?(x,y)
@matrix[y][x] == 0
end
def draw(figure)
figure.draw(@matrix)
end
def render_matrix(a, b, c)
@matrix.each_with_index.map do |row, i|
row = row.join.gsub("0", b).gsub("1", a)
row = row + c if i + 1 < height
row
end
end
def render_as_ascii()
render_matrix('@', '-', "\n")
end
def render_as_html()
'<!DOCTYPE html> <html> <head> <title>Rendered Canvas</title>
<style type="text/css"> .canvas { font-size: 1px; line-height: 1px;} .canvas *
{ display: inline-block; width: 10px; height: 10px; border-radius: 5px; } .canvas i
{ background-color: #eee;} .canvas b { background-color: #333; } </style> </head> <body>
<div class="canvas">' + render_matrix('<b></b>', '<i></i>', '<br>').join +
'</div> </body> </html>'
end
def render_as(renderer)
if renderer == Graphics::Renderers::Ascii
render_as_ascii()
elsif renderer == Graphics::Renderers::Html
render_as_html
end
end
end
class Point
def initialize(abscissa, ordinate)
@x = abscissa
@y = ordinate
end
def x()
@x
end
def y()
@y
end
def draw(matrix)
matrix[@y][@x] = 1
end
def ==(point)
@x == point.x && @y == point.y
end
alias :eql? :==
def hash()
[@x, @y]
end
end
class Line
def initialize(point_1, point_2)
@point_1 = point1
@point_2 = point2
end
def from()
@p1 if @p1.x < @p2.x
@p2 if @p1.x > @p2.x
@p1 if @p1.x == @p2.x && @p1.y < @p2.y
@p2 if @p1.x == @p2.x && @p1.y > @p2.y
@p1 if @p1 == @p2
end
def to()
@p1 if @p1.x > @p2.x
@p2 if @p1.x < @p2.x
@p1 if @p1.x == @p2.x && @p1.y > @p2.y
@p2 if @p1.x == @p2.x && @p1.y < @p2.y
@p1 if @p1 == @p2
end
def ==(line)
from == line.from && to == line.to
end
alias :eql? :==
def hash()
[from.x, from.y, to.x, to.y]
end
end
end

Лог от изпълнението

.....F.FFFFFFFFFFFFFFFFF..............FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

Failures:

  1) Graphics Canvas allows checking if a pixel at a given x and y is set
     Failure/Error: canvas.pixel_at?(0, 0).should be_false
       expected: false value
            got: true
     # /tmp/d20131223-4637-ujaurm/spec.rb:29:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Graphics Canvas allows setting a pixel at a given x and y
     Failure/Error: canvas.pixel_at?(3, 5).should be_false
       expected: false value
            got: true
     # /tmp/d20131223-4637-ujaurm/spec.rb:38:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:205:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Graphics Canvas drawing of shapes and rasterization of points works for a single one
     Failure/Error: canvas.pixel_at?(2, 4).should be_false
       expected: false value
            got: true
     # /tmp/d20131223-4637-ujaurm/spec.rb:46:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
     Failure/Error: canvas.set_pixel 4, 4
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     # /tmp/d20131223-4637-ujaurm/solution.rb:21:in `set_pixel'
     # /tmp/d20131223-4637-ujaurm/spec.rb:57:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) Graphics Canvas drawing of shapes and rasterization of lines works with simple horizontal lines
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb906f6a8>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:71:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) Graphics Canvas drawing of shapes and rasterization of lines works with vertical lines
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb906df10>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:82:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb906cd40>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:98:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  9) Graphics Canvas drawing of shapes and rasterization of lines works with lines with a significant slope, with swapped ends
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb90576e8>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:111:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  10) Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb9055be0>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:129:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  11) Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb9054da8>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:143:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  12) Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:156:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  13) Graphics Canvas drawing of shapes and rasterization of rectangles works with rects defined with their bottom left and top right points
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:169:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  14) Graphics Canvas drawing of shapes and rasterization of rectangles works with rects with a zero height as a line
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:182:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  15) Graphics Canvas drawing of shapes and rasterization of rectangles works with rects with a zero width and height as a single point
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:193:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  16) Graphics Renderers Ascii renders a grid of the size of the canvas
     Failure/Error: lines = canvas.render_as(ascii).split("\n")
     NoMethodError:
       undefined method `split' for ["----\n", "----\n", "----"]:Array
     # /tmp/d20131223-4637-ujaurm/spec.rb:238:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  17) Graphics Renderers Ascii renders blank canvases
     Failure/Error: canvas.render_as(ascii).should eq rendering('
       
       expected: "----\n----\n----"
            got: ["----\n", "----\n", "----"]
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
       -----
       -----
       +----\n
       +----\n
        ----
     # /tmp/d20131223-4637-ujaurm/spec.rb:245:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  18) Graphics Renderers Ascii renders simple canvases
     Failure/Error: canvas.render_as(ascii).should eq rendering('
       
       expected: "@---\n----\n---@"
            got: ["@---\n", "----\n", "---@"]
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
       -@---
       -----
       +@---\n
       +----\n
        ---@
     # /tmp/d20131223-4637-ujaurm/spec.rb:256:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  19) Graphics shapes Line initialization works with two points
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb8fe1f88>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:379:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  20) Graphics shapes Line initialization returns its from and to points via getters
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb8fe10b0>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:371:in `block (4 levels) in <top (required)>'
     # /tmp/d20131223-4637-ujaurm/spec.rb:383:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  21) Graphics shapes Line initialization does not allow setting its from and to
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb8fe0250>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:371:in `block (4 levels) in <top (required)>'
     # /tmp/d20131223-4637-ujaurm/spec.rb:390:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  22) Graphics shapes Line initialization with swapped points puts the leftmost point in the from field
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb8fda558>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:398:in `block (6 levels) in <top (required)>'
     # /tmp/d20131223-4637-ujaurm/spec.rb:402:in `block (6 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  23) Graphics shapes Line initialization with swapped points puts the rightmost point in the to field
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb8fd93b0>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:398:in `block (6 levels) in <top (required)>'
     # /tmp/d20131223-4637-ujaurm/spec.rb:407:in `block (6 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  24) Graphics shapes Line initialization with swapped points puts the top point of vertical lines in the from field
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb8fd8244>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:399:in `block (6 levels) in <top (required)>'
     # /tmp/d20131223-4637-ujaurm/spec.rb:412:in `block (6 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  25) Graphics shapes Line initialization with swapped points puts the bottom point of vertical lines in the to field
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb8fd3514>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:399:in `block (6 levels) in <top (required)>'
     # /tmp/d20131223-4637-ujaurm/spec.rb:417:in `block (6 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  26) Graphics shapes Line comparison for equality is false if any of the points differ
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb8fd1ef8>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:425:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  27) Graphics shapes Line comparison for equality is true if line ends are the same
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb8fd2510>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:432:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  28) Graphics shapes Line comparison for equality is true if line ends are the same, even if swapped
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb8fca324>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:439:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  29) Graphics shapes Line comparison for equality is true if line is vertical and the bottom is given first
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb8fc93c0>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:446:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  30) Graphics shapes Line comparison for equality works with eql? as well
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb8fc8358>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:453:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  31) Graphics shapes Line comparison for equality returns the same hash if the lines are the same
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb8fc3498>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:462:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  32) Graphics shapes Line comparison for equality returns a different hash if the lines differ
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       undefined local variable or method `point1' for #<Graphics::Line:0xb8fc2458>
     # /tmp/d20131223-4637-ujaurm/solution.rb:79:in `initialize'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `new'
     # /tmp/d20131223-4637-ujaurm/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-ujaurm/spec.rb:469:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  33) Graphics shapes Rectangle initialization requires arguments
     Failure/Error: expect { Graphics::Rectangle.new }.to raise_error(ArgumentError)
       expected ArgumentError, got #<NameError: uninitialized constant Graphics::Rectangle> with backtrace:
         # /tmp/d20131223-4637-ujaurm/spec.rb:480:in `block (6 levels) in <top (required)>'
         # /tmp/d20131223-4637-ujaurm/spec.rb:480:in `block (5 levels) in <top (required)>'
         # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
         # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'
     # /tmp/d20131223-4637-ujaurm/spec.rb:480:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  34) Graphics shapes Rectangle initialization can be created from two points
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:484:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  35) Graphics shapes Rectangle initialization allows accessing its left and right points via getters
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:488:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  36) Graphics shapes Rectangle initialization does not allow setting its x and y
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:497:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  37) Graphics shapes Rectangle initialization puts the leftmost point in its left field
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:505:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  38) Graphics shapes Rectangle comparison for equality is false if any of the points differ
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:516:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  39) Graphics shapes Rectangle comparison for equality is true if rectangle points are the same
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:523:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  40) Graphics shapes Rectangle comparison for equality is true if rectangle points are the same, even if swapped
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:530:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  41) Graphics shapes Rectangle comparison for equality is true for rectangles defined with different diagonal corners
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:537:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  42) Graphics shapes Rectangle comparison for equality works with eql? as well
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:544:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  43) Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:553:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  44) Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:560:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  45) Graphics shapes Rectangle comparison for equality returns a different hash if the rectangles differ
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:567:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  46) Graphics shapes Rectangle corners top left
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:576:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  47) Graphics shapes Rectangle corners top right
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:582:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  48) Graphics shapes Rectangle corners bottom right
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:588:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  49) Graphics shapes Rectangle corners bottom left
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       uninitialized constant Graphics::Rectangle
     # /tmp/d20131223-4637-ujaurm/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-ujaurm/spec.rb:594:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.08221 seconds
69 examples, 49 failures

Failed examples:

rspec /tmp/d20131223-4637-ujaurm/spec.rb:28 # Graphics Canvas allows checking if a pixel at a given x and y is set
rspec /tmp/d20131223-4637-ujaurm/spec.rb:37 # Graphics Canvas allows setting a pixel at a given x and y
rspec /tmp/d20131223-4637-ujaurm/spec.rb:203 # Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
rspec /tmp/d20131223-4637-ujaurm/spec.rb:45 # Graphics Canvas drawing of shapes and rasterization of points works for a single one
rspec /tmp/d20131223-4637-ujaurm/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-ujaurm/spec.rb:69 # Graphics Canvas drawing of shapes and rasterization of lines works with simple horizontal lines
rspec /tmp/d20131223-4637-ujaurm/spec.rb:80 # Graphics Canvas drawing of shapes and rasterization of lines works with vertical lines
rspec /tmp/d20131223-4637-ujaurm/spec.rb:96 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
rspec /tmp/d20131223-4637-ujaurm/spec.rb:109 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a significant slope, with swapped ends
rspec /tmp/d20131223-4637-ujaurm/spec.rb:127 # Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
rspec /tmp/d20131223-4637-ujaurm/spec.rb:141 # Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
rspec /tmp/d20131223-4637-ujaurm/spec.rb:154 # Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
rspec /tmp/d20131223-4637-ujaurm/spec.rb:167 # Graphics Canvas drawing of shapes and rasterization of rectangles works with rects defined with their bottom left and top right points
rspec /tmp/d20131223-4637-ujaurm/spec.rb:180 # Graphics Canvas drawing of shapes and rasterization of rectangles works with rects with a zero height as a line
rspec /tmp/d20131223-4637-ujaurm/spec.rb:191 # Graphics Canvas drawing of shapes and rasterization of rectangles works with rects with a zero width and height as a single point
rspec /tmp/d20131223-4637-ujaurm/spec.rb:237 # Graphics Renderers Ascii renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-ujaurm/spec.rb:244 # Graphics Renderers Ascii renders blank canvases
rspec /tmp/d20131223-4637-ujaurm/spec.rb:252 # Graphics Renderers Ascii renders simple canvases
rspec /tmp/d20131223-4637-ujaurm/spec.rb:378 # Graphics shapes Line initialization works with two points
rspec /tmp/d20131223-4637-ujaurm/spec.rb:382 # Graphics shapes Line initialization returns its from and to points via getters
rspec /tmp/d20131223-4637-ujaurm/spec.rb:389 # Graphics shapes Line initialization does not allow setting its from and to
rspec /tmp/d20131223-4637-ujaurm/spec.rb:401 # Graphics shapes Line initialization with swapped points puts the leftmost point in the from field
rspec /tmp/d20131223-4637-ujaurm/spec.rb:406 # Graphics shapes Line initialization with swapped points puts the rightmost point in the to field
rspec /tmp/d20131223-4637-ujaurm/spec.rb:411 # Graphics shapes Line initialization with swapped points puts the top point of vertical lines in the from field
rspec /tmp/d20131223-4637-ujaurm/spec.rb:416 # Graphics shapes Line initialization with swapped points puts the bottom point of vertical lines in the to field
rspec /tmp/d20131223-4637-ujaurm/spec.rb:424 # Graphics shapes Line comparison for equality is false if any of the points differ
rspec /tmp/d20131223-4637-ujaurm/spec.rb:431 # Graphics shapes Line comparison for equality is true if line ends are the same
rspec /tmp/d20131223-4637-ujaurm/spec.rb:438 # Graphics shapes Line comparison for equality is true if line ends are the same, even if swapped
rspec /tmp/d20131223-4637-ujaurm/spec.rb:445 # Graphics shapes Line comparison for equality is true if line is vertical and the bottom is given first
rspec /tmp/d20131223-4637-ujaurm/spec.rb:452 # Graphics shapes Line comparison for equality works with eql? as well
rspec /tmp/d20131223-4637-ujaurm/spec.rb:461 # Graphics shapes Line comparison for equality returns the same hash if the lines are the same
rspec /tmp/d20131223-4637-ujaurm/spec.rb:468 # Graphics shapes Line comparison for equality returns a different hash if the lines differ
rspec /tmp/d20131223-4637-ujaurm/spec.rb:479 # Graphics shapes Rectangle initialization requires arguments
rspec /tmp/d20131223-4637-ujaurm/spec.rb:483 # Graphics shapes Rectangle initialization can be created from two points
rspec /tmp/d20131223-4637-ujaurm/spec.rb:487 # Graphics shapes Rectangle initialization allows accessing its left and right points via getters
rspec /tmp/d20131223-4637-ujaurm/spec.rb:496 # Graphics shapes Rectangle initialization does not allow setting its x and y
rspec /tmp/d20131223-4637-ujaurm/spec.rb:504 # Graphics shapes Rectangle initialization puts the leftmost point in its left field
rspec /tmp/d20131223-4637-ujaurm/spec.rb:515 # Graphics shapes Rectangle comparison for equality is false if any of the points differ
rspec /tmp/d20131223-4637-ujaurm/spec.rb:522 # Graphics shapes Rectangle comparison for equality is true if rectangle points are the same
rspec /tmp/d20131223-4637-ujaurm/spec.rb:529 # Graphics shapes Rectangle comparison for equality is true if rectangle points are the same, even if swapped
rspec /tmp/d20131223-4637-ujaurm/spec.rb:536 # Graphics shapes Rectangle comparison for equality is true for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-ujaurm/spec.rb:543 # Graphics shapes Rectangle comparison for equality works with eql? as well
rspec /tmp/d20131223-4637-ujaurm/spec.rb:552 # Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
rspec /tmp/d20131223-4637-ujaurm/spec.rb:559 # Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-ujaurm/spec.rb:566 # Graphics shapes Rectangle comparison for equality returns a different hash if the rectangles differ
rspec /tmp/d20131223-4637-ujaurm/spec.rb:575 # Graphics shapes Rectangle corners top left
rspec /tmp/d20131223-4637-ujaurm/spec.rb:581 # Graphics shapes Rectangle corners top right
rspec /tmp/d20131223-4637-ujaurm/spec.rb:587 # Graphics shapes Rectangle corners bottom right
rspec /tmp/d20131223-4637-ujaurm/spec.rb:593 # Graphics shapes Rectangle corners bottom left

История (3 версии и 0 коментара)

Николай обнови решението на 22.12.2013 19:28 (преди почти 11 години)

+module Graphics
+ class Renderers
+ class Ascii
+ end
+ module Html
+ end
+ end
+ class Canvas
+ def initialize(width, height)
+ @width = width
+ @height = height
+ @matrix = Array.new(height) { Array.new(width, 0) }
+ end
+ def width()
+ @width
+ end
+ def height()
+ @height
+ end
+ def set_pixel(x,y)
+ @matrix[y][x] = 1
+ end
+ def pixel_at?(x,y)
+ @matrix[y][x] == 0
+ end
+ def draw()
+ @matrix
+ end
+ def render_matrix(a, b, c)
+ @matrix.each_with_index.map do |row, i|
+ row = row.join.gsub("0", b).gsub("1", a)
+ row = row + c if i + 1 < height
+ row
+ end
+ end
+ def render_as_ascii()
+ render_matrix('@', '-', "\n")
+ end
+ def render_as_html()
+ '<!DOCTYPE html> <html> <head> <title>Rendered Canvas</title>
+<style type="text/css"> .canvas { font-size: 1px; line-height: 1px;} .canvas *
+{ display: inline-block; width: 10px; height: 10px; border-radius: 5px; } .canvas i
+{ background-color: #eee;} .canvas b { background-color: #333; } </style> </head> <body>
+<div class="canvas">' + render_matrix('<b></b>', '<i></i>', '<br>').join +
+'</div> </body> </html>'
+ end
+ def render_as(renderer)
+ if renderer == Graphics::Renderers::Ascii
+ render_as_ascii()
+ elsif renderer == Graphics::Renderers::Html
+ render_as_html
+ end
+ end
+ end
+end

Николай обнови решението на 22.12.2013 23:02 (преди почти 11 години)

module Graphics
class Renderers
class Ascii
end
module Html
end
end
class Canvas
def initialize(width, height)
@width = width
@height = height
@matrix = Array.new(height) { Array.new(width, 0) }
end
def width()
@width
end
def height()
@height
end
def set_pixel(x,y)
@matrix[y][x] = 1
end
def pixel_at?(x,y)
@matrix[y][x] == 0
end
- def draw()
- @matrix
+ def draw(figure)
+ figure.draw(@matrix)
end
def render_matrix(a, b, c)
@matrix.each_with_index.map do |row, i|
row = row.join.gsub("0", b).gsub("1", a)
row = row + c if i + 1 < height
row
end
end
def render_as_ascii()
render_matrix('@', '-', "\n")
end
def render_as_html()
'<!DOCTYPE html> <html> <head> <title>Rendered Canvas</title>
<style type="text/css"> .canvas { font-size: 1px; line-height: 1px;} .canvas *
{ display: inline-block; width: 10px; height: 10px; border-radius: 5px; } .canvas i
{ background-color: #eee;} .canvas b { background-color: #333; } </style> </head> <body>
<div class="canvas">' + render_matrix('<b></b>', '<i></i>', '<br>').join +
'</div> </body> </html>'
end
def render_as(renderer)
if renderer == Graphics::Renderers::Ascii
render_as_ascii()
elsif renderer == Graphics::Renderers::Html
render_as_html
end
end
end
+ class Point
+ def initialize(abscissa, ordinate)
+ @x = abscissa
+ @y = ordinate
+ end
+ def x()
+ @x
+ end
+ def y()
+ @y
+ end
+ def draw(matrix)
+ matrix[@y][@x] = 1
+ end
+ def ==(point)
+ @x == point.x && @y == point.y
+ end
+ alias :eql? :==
+ def hash()
+ [@x, @y]
+ end
+ end
+ class Line
+ def initialize(point_1, point_2)
+ @point_1 = point1
+ @point_2 = point2
+ end
+ def from()
+ @p1 if @p1.x < @p2.x
+ @p2 if @p1.x > @p2.x
+ @p1 if @p1.x == @p2.x && @p1.y < @p2.y
+ @p2 if @p1.x == @p2.x && @p1.y > @p2.y
+ @p1 if @p1 == @p2
+ end
+ def to()
+ @p1 if @p1.x > @p2.x
+ @p2 if @p1.x < @p2.x
+ @p1 if @p1.x == @p2.x && @p1.y > @p2.y
+ @p2 if @p1.x == @p2.x && @p1.y < @p2.y
+ @p1 if @p1 == @p2
+ end
+ def ==(line)
+ from == line.from && to == line.to
+ end
+ alias :eql? :==
+ def hash()
+ [from.x, from.y, to.x, to.y]
+ end
+ end
+end
+module Graphics
+ canvas = Canvas.new 30, 30
+ canvas.draw Point.new(4, 16)
+ puts canvas.render_as(Renderers::Ascii)
end

Николай обнови решението на 22.12.2013 23:02 (преди почти 11 години)

module Graphics
class Renderers
class Ascii
end
module Html
end
end
class Canvas
def initialize(width, height)
@width = width
@height = height
@matrix = Array.new(height) { Array.new(width, 0) }
end
def width()
@width
end
def height()
@height
end
def set_pixel(x,y)
@matrix[y][x] = 1
end
def pixel_at?(x,y)
@matrix[y][x] == 0
end
def draw(figure)
figure.draw(@matrix)
end
def render_matrix(a, b, c)
@matrix.each_with_index.map do |row, i|
row = row.join.gsub("0", b).gsub("1", a)
row = row + c if i + 1 < height
row
end
end
def render_as_ascii()
render_matrix('@', '-', "\n")
end
def render_as_html()
'<!DOCTYPE html> <html> <head> <title>Rendered Canvas</title>
<style type="text/css"> .canvas { font-size: 1px; line-height: 1px;} .canvas *
{ display: inline-block; width: 10px; height: 10px; border-radius: 5px; } .canvas i
{ background-color: #eee;} .canvas b { background-color: #333; } </style> </head> <body>
<div class="canvas">' + render_matrix('<b></b>', '<i></i>', '<br>').join +
'</div> </body> </html>'
end
def render_as(renderer)
if renderer == Graphics::Renderers::Ascii
render_as_ascii()
elsif renderer == Graphics::Renderers::Html
render_as_html
end
end
end
class Point
def initialize(abscissa, ordinate)
@x = abscissa
@y = ordinate
end
def x()
@x
end
def y()
@y
end
def draw(matrix)
matrix[@y][@x] = 1
end
def ==(point)
@x == point.x && @y == point.y
end
alias :eql? :==
def hash()
[@x, @y]
end
end
class Line
def initialize(point_1, point_2)
@point_1 = point1
@point_2 = point2
end
def from()
@p1 if @p1.x < @p2.x
@p2 if @p1.x > @p2.x
@p1 if @p1.x == @p2.x && @p1.y < @p2.y
@p2 if @p1.x == @p2.x && @p1.y > @p2.y
@p1 if @p1 == @p2
end
def to()
@p1 if @p1.x > @p2.x
@p2 if @p1.x < @p2.x
@p1 if @p1.x == @p2.x && @p1.y > @p2.y
@p2 if @p1.x == @p2.x && @p1.y < @p2.y
@p1 if @p1 == @p2
end
def ==(line)
from == line.from && to == line.to
end
alias :eql? :==
def hash()
[from.x, from.y, to.x, to.y]
end
end
-end
-module Graphics
- canvas = Canvas.new 30, 30
- canvas.draw Point.new(4, 16)
- puts canvas.render_as(Renderers::Ascii)
end