Решение на Трета задача от Петър Мазълов

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

Към профила на Петър Мазълов

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 36 успешни тест(а)
  • 33 неуспешни тест(а)

Код

module Graphics
HTML_BEGINNING = '<!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">'
HTML_END = '</div>
</body>
</html>'
class Canvas
attr_accessor :pixels
def initialize(width, height)
@width = width
@height = height
@pixels = fill_pixels(width, height)
end
def fill_pixels(x, y)
result = []
(0..y - 1).each { |i| result.push(Array.new(x, 0))}
result
end
def width
@width
end
def height
@height
end
def set_pixel(x, y)
@pixels[y][x] = 1
end
def pixel_at?(x, y)
@pixels[y][x] == 1
end
def render_as(renderer)
if renderer.to_s.match (/Html$/)
rendered = renderer.new(self)
rendered.to_html
elsif renderer.to_s.match (/Ascii$/)
rendered = renderer.new(self)
rendered.to_ascii
end
end
def draw(figure)
p self.render_as(Renderers::Ascii)
figure.draw_figure(self)
end
end
#private
class Renderers
class Ascii
attr_reader :to_ascii
def initialize(object)
to_ascii = ""
object.pixels.each{ |i| to_ascii << (i.join + "\n")}
to_ascii.gsub! "0", "-"
to_ascii.gsub! "1", "@"
@to_ascii = to_ascii.strip
end
end
class Html
attr_reader :to_html
def initialize(object)
to_html = ""
object.pixels.each{ |i| to_html << (i.join + "<br>")}
to_html.gsub! "0", "<i></i>"
to_html.gsub! "1", "<b></b>"
to_html.chomp!("<br>")
@to_html = HTML_BEGINNING + to_html + HTML_END
end
end
end
class Point
attr_reader :abscissa, :ordinate
def initialize(x, y)
@abscissa = x
@ordinate = y
end
def x
@abscissa
end
def y
@ordinate
end
def == (other)
abscissa == other.abscissa and ordinate == other.ordinate
end
def eql? (other)
self == other
end
def draw_figure(canvas)
canvas.set_pixel(abscissa, ordinate)
end
end
class Rectangle
attr_reader :first_point, :second_point
def initialize(first, second)
@first_point = first
@second_point = second
end
def left
if(first_point.x < second_point.x)
first_point
elsif first_point.y < second_point.y
first_point
else
second_point
end
end
def right
if(first_point.x > second_point.x)
first_point
elsif first_point.y > second_point.y
first_point
else
second_point
end
end
def top_left
Point.new([first_point.x, second_point.x].min, [first_point.y, second_point.y].min)
end
def top_right
Point.new([first_point.x, second_point.x].max, [first_point.y, second_point.y].min)
end
def bottom_left
Point.new([first_point.x, second_point.x].min, [first_point.y, second_point.y].max)
end
def bottom_right
Point.new([first_point.x, second_point.x].max, [first_point.y, second_point.y].max)
end
def ==(other)
self.first_point == other.first_point and self.second_point == other.second_point
end
def eql?(other)
self == other
end
end
end

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

........"---------------\n---------------\n---------------\n---------------\n---------------\n---------------\n---------------\n---------------\n---------------\n---------------\n---------------\n---------------\n---------------\n---------------\n---------------"
F"------------------------------\n------------------------------\n------------------------------\n------------------------------\n------------------------------\n------------------------------\n------------------------------\n------------------------------\n------------------------------\n------------------------------\n------------------------------\n------------------------------\n------------------------------\n------------------------------\n------------------------------\n------------------------------\n------------------------------\n------------------------------\n------------------------------\n------------------------------"
.FFFFFFF"----------\n----------\n----------\n----------\n----------"
F"----------\n----------\n----------\n----------\n----------"
F"----------\n----------\n----------"
F"---\n---\n---"
F..............F.FFFFFFFFFFFFFFF....F..FF.FF.....

Failures:

  1) Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
     Failure/Error: canvas.draw make_rectangle(make_point(2, 2), make_point(12, 12))
     NoMethodError:
       undefined method `draw_figure' for #<Graphics::Rectangle:0xb8bc62f0>
     # /tmp/d20131223-4637-170wdgj/solution.rb:75:in `draw'
     # /tmp/d20131223-4637-170wdgj/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)>'

  2) 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-170wdgj/solution.rb:57:in `set_pixel'
     # /tmp/d20131223-4637-170wdgj/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)>'

  3) Graphics Canvas drawing of shapes and rasterization of lines works with simple horizontal lines
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/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)>'

  4) Graphics Canvas drawing of shapes and rasterization of lines works with vertical lines
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/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)>'

  5) Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/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)>'

  6) 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:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/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)>'

  7) Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/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)>'

  8) Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/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)>'

  9) Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
     Failure/Error: canvas.draw make_rectangle(make_point(1, 1), make_point(8, 3))
     NoMethodError:
       undefined method `draw_figure' for #<Graphics::Rectangle:0xb8b6c0ac>
     # /tmp/d20131223-4637-170wdgj/solution.rb:75:in `draw'
     # /tmp/d20131223-4637-170wdgj/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)>'

  10) Graphics Canvas drawing of shapes and rasterization of rectangles works with rects defined with their bottom left and top right points
     Failure/Error: canvas.draw make_rectangle(make_point(1, 3), make_point(8, 1))
     NoMethodError:
       undefined method `draw_figure' for #<Graphics::Rectangle:0xb8b673f4>
     # /tmp/d20131223-4637-170wdgj/solution.rb:75:in `draw'
     # /tmp/d20131223-4637-170wdgj/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)>'

  11) Graphics Canvas drawing of shapes and rasterization of rectangles works with rects with a zero height as a line
     Failure/Error: canvas.draw make_rectangle(make_point(1, 1), make_point(8, 1))
     NoMethodError:
       undefined method `draw_figure' for #<Graphics::Rectangle:0xb8b65734>
     # /tmp/d20131223-4637-170wdgj/solution.rb:75:in `draw'
     # /tmp/d20131223-4637-170wdgj/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)>'

  12) Graphics Canvas drawing of shapes and rasterization of rectangles works with rects with a zero width and height as a single point
     Failure/Error: canvas.draw make_rectangle(make_point(1, 1), make_point(1, 1))
     NoMethodError:
       undefined method `draw_figure' for #<Graphics::Rectangle:0xb8b64208>
     # /tmp/d20131223-4637-170wdgj/solution.rb:75:in `draw'
     # /tmp/d20131223-4637-170wdgj/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)>'

  13) Graphics shapes Point comparison for equality returns the same hash for the same points
     Failure/Error: a1.hash.should eq a2.hash
       
       expected: -314704088
            got: 956739236
       
       (compared using ==)
     # /tmp/d20131223-4637-170wdgj/spec.rb:361: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 shapes Line initialization requires arguments
     Failure/Error: expect { Graphics::Line.new }.to raise_error(ArgumentError)
       expected ArgumentError, got #<NameError: uninitialized constant Graphics::Line> with backtrace:
         # /tmp/d20131223-4637-170wdgj/spec.rb:375:in `block (6 levels) in <top (required)>'
         # /tmp/d20131223-4637-170wdgj/spec.rb:375: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-170wdgj/spec.rb:375: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 shapes Line initialization works with two points
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/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)>'

  16) Graphics shapes Line initialization returns its from and to points via getters
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/spec.rb:371:in `block (4 levels) in <top (required)>'
     # /tmp/d20131223-4637-170wdgj/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)>'

  17) Graphics shapes Line initialization does not allow setting its from and to
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/spec.rb:371:in `block (4 levels) in <top (required)>'
     # /tmp/d20131223-4637-170wdgj/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)>'

  18) Graphics shapes Line initialization with swapped points puts the leftmost point in the from field
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/spec.rb:398:in `block (6 levels) in <top (required)>'
     # /tmp/d20131223-4637-170wdgj/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)>'

  19) Graphics shapes Line initialization with swapped points puts the rightmost point in the to field
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/spec.rb:398:in `block (6 levels) in <top (required)>'
     # /tmp/d20131223-4637-170wdgj/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)>'

  20) 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:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/spec.rb:399:in `block (6 levels) in <top (required)>'
     # /tmp/d20131223-4637-170wdgj/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)>'

  21) 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:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/spec.rb:399:in `block (6 levels) in <top (required)>'
     # /tmp/d20131223-4637-170wdgj/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)>'

  22) Graphics shapes Line comparison for equality is false if any of the points differ
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/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)>'

  23) Graphics shapes Line comparison for equality is true if line ends are the same
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/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)>'

  24) Graphics shapes Line comparison for equality is true if line ends are the same, even if swapped
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/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)>'

  25) 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:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/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)>'

  26) Graphics shapes Line comparison for equality works with eql? as well
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/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)>'

  27) Graphics shapes Line comparison for equality returns the same hash if the lines are the same
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/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)>'

  28) Graphics shapes Line comparison for equality returns a different hash if the lines differ
     Failure/Error: Graphics::Line.new(*args)
     NameError:
       uninitialized constant Graphics::Line
     # /tmp/d20131223-4637-170wdgj/spec.rb:611:in `make_line'
     # /tmp/d20131223-4637-170wdgj/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)>'

  29) Graphics shapes Rectangle initialization puts the leftmost point in its left field
     Failure/Error: rect.left.x.should eq 4
       
       expected: 4
            got: 7
       
       (compared using ==)
     # /tmp/d20131223-4637-170wdgj/spec.rb:507: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 Rectangle comparison for equality is true if rectangle points are the same, even if swapped
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-170wdgj/spec.rb:533: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 Rectangle comparison for equality is true for rectangles defined with different diagonal corners
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-170wdgj/spec.rb:540: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 Rectangle comparison for equality returns the same hash if the rectangles are the same
     Failure/Error: a.hash.should eq b.hash
       
       expected: 995757310
            got: -246337052
       
       (compared using ==)
     # /tmp/d20131223-4637-170wdgj/spec.rb:556: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 comparison for equality returns the same hash for rectangles defined with different diagonal corners
     Failure/Error: a.hash.should eq b.hash
       
       expected: 379029884
            got: 235119954
       
       (compared using ==)
     # /tmp/d20131223-4637-170wdgj/spec.rb:563: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.0858 seconds
69 examples, 33 failures

Failed examples:

rspec /tmp/d20131223-4637-170wdgj/spec.rb:203 # Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
rspec /tmp/d20131223-4637-170wdgj/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-170wdgj/spec.rb:69 # Graphics Canvas drawing of shapes and rasterization of lines works with simple horizontal lines
rspec /tmp/d20131223-4637-170wdgj/spec.rb:80 # Graphics Canvas drawing of shapes and rasterization of lines works with vertical lines
rspec /tmp/d20131223-4637-170wdgj/spec.rb:96 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
rspec /tmp/d20131223-4637-170wdgj/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-170wdgj/spec.rb:127 # Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
rspec /tmp/d20131223-4637-170wdgj/spec.rb:141 # Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
rspec /tmp/d20131223-4637-170wdgj/spec.rb:154 # Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
rspec /tmp/d20131223-4637-170wdgj/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-170wdgj/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-170wdgj/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-170wdgj/spec.rb:360 # Graphics shapes Point comparison for equality returns the same hash for the same points
rspec /tmp/d20131223-4637-170wdgj/spec.rb:374 # Graphics shapes Line initialization requires arguments
rspec /tmp/d20131223-4637-170wdgj/spec.rb:378 # Graphics shapes Line initialization works with two points
rspec /tmp/d20131223-4637-170wdgj/spec.rb:382 # Graphics shapes Line initialization returns its from and to points via getters
rspec /tmp/d20131223-4637-170wdgj/spec.rb:389 # Graphics shapes Line initialization does not allow setting its from and to
rspec /tmp/d20131223-4637-170wdgj/spec.rb:401 # Graphics shapes Line initialization with swapped points puts the leftmost point in the from field
rspec /tmp/d20131223-4637-170wdgj/spec.rb:406 # Graphics shapes Line initialization with swapped points puts the rightmost point in the to field
rspec /tmp/d20131223-4637-170wdgj/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-170wdgj/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-170wdgj/spec.rb:424 # Graphics shapes Line comparison for equality is false if any of the points differ
rspec /tmp/d20131223-4637-170wdgj/spec.rb:431 # Graphics shapes Line comparison for equality is true if line ends are the same
rspec /tmp/d20131223-4637-170wdgj/spec.rb:438 # Graphics shapes Line comparison for equality is true if line ends are the same, even if swapped
rspec /tmp/d20131223-4637-170wdgj/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-170wdgj/spec.rb:452 # Graphics shapes Line comparison for equality works with eql? as well
rspec /tmp/d20131223-4637-170wdgj/spec.rb:461 # Graphics shapes Line comparison for equality returns the same hash if the lines are the same
rspec /tmp/d20131223-4637-170wdgj/spec.rb:468 # Graphics shapes Line comparison for equality returns a different hash if the lines differ
rspec /tmp/d20131223-4637-170wdgj/spec.rb:504 # Graphics shapes Rectangle initialization puts the leftmost point in its left field
rspec /tmp/d20131223-4637-170wdgj/spec.rb:529 # Graphics shapes Rectangle comparison for equality is true if rectangle points are the same, even if swapped
rspec /tmp/d20131223-4637-170wdgj/spec.rb:536 # Graphics shapes Rectangle comparison for equality is true for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-170wdgj/spec.rb:552 # Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
rspec /tmp/d20131223-4637-170wdgj/spec.rb:559 # Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners

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

Петър обнови решението на 22.12.2013 20:17 (преди почти 11 години)

+module Graphics
+
+HTML_BEGINNING = '<!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">'
+
+HTML_END = '</div>
+ </body>
+ </html>'
+
+ class Canvas
+
+ attr_accessor :pixels
+ def initialize(width, height)
+ @width = width
+ @height = height
+ @pixels = fill_pixels(width, height)
+ end
+
+ def fill_pixels(x, y)
+ result = []
+ (0..y - 1).each { |i| result.push(Array.new(x, 0))}
+ result
+ end
+
+ def width
+ @width
+ end
+
+ def height
+ @height
+ end
+
+ def set_pixel(x, y)
+ @pixels[y][x] = 1
+ end
+
+ def pixel_at?(x, y)
+ @pixels[y][x] == 1
+ end
+
+ def render_as(renderer)
+ if renderer.to_s.match (/Html$/)
+ rendered = renderer.new(self)
+ rendered.to_html
+ elsif renderer.to_s.match (/Ascii$/)
+ rendered = renderer.new(self)
+ rendered.to_ascii
+ end
+ end
+ def draw(figure)
+ p self.render_as(Renderers::Ascii)
+ figure.draw_figure(self)
+ end
+ end
+
+ #private
+ class Renderers
+
+ class Ascii
+ attr_reader :to_ascii
+ def initialize(object)
+ to_ascii = ""
+ object.pixels.each{ |i| to_ascii << (i.join + "\n")}
+ to_ascii.gsub! "0", "-"
+ to_ascii.gsub! "1", "@"
+ @to_ascii = to_ascii.strip
+ end
+ end
+
+ class Html
+ attr_reader :to_html
+ def initialize(object)
+ to_html = ""
+ object.pixels.each{ |i| to_html << (i.join + "<br>")}
+ to_html.gsub! "0", "<i></i>"
+ to_html.gsub! "1", "<b></b>"
+ to_html.chomp!("<br>")
+ @to_html = HTML_BEGINNING + to_html + HTML_END
+ end
+ end
+ end
+
+ class Point
+
+ attr_reader :abscissa, :ordinate
+ def initialize(x, y)
+ @abscissa = x
+ @ordinate = y
+ end
+ def x
+ @abscissa
+ end
+ def y
+ @ordinate
+ end
+ def == (other)
+ abscissa == other.abscissa and ordinate == other.ordinate
+ end
+ def eql? (other)
+ self == other
+ end
+ def draw_figure(canvas)
+ canvas.set_pixel(abscissa, ordinate)
+ end
+ end
+
+ class Rectangle
+
+ attr_reader :first_point, :second_point
+ def initialize(first, second)
+ @first_point = first
+ @second_point = second
+ end
+
+ def left
+ if(first_point.x < second_point.x)
+ first_point
+ elsif first_point.y < second_point.y
+ first_point
+ else
+ second_point
+ end
+ end
+
+ def right
+ if(first_point.x > second_point.x)
+ first_point
+ elsif first_point.y > second_point.y
+ first_point
+ else
+ second_point
+ end
+ end
+ def top_left
+ Point.new([first_point.x, second_point.x].min, [first_point.y, second_point.y].min)
+ end
+ def top_right
+ Point.new([first_point.x, second_point.x].max, [first_point.y, second_point.y].min)
+ end
+ def bottom_left
+ Point.new([first_point.x, second_point.x].min, [first_point.y, second_point.y].max)
+ end
+ def bottom_right
+ Point.new([first_point.x, second_point.x].max, [first_point.y, second_point.y].max)
+ end
+ def ==(other)
+ self.first_point == other.first_point and self.second_point == other.second_point
+ end
+ def eql?(other)
+ self == other
+ end
+ end
+
+
+end