Решение на Трета задача от Цветан Коев

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

Към профила на Цветан Коев

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 42 успешни тест(а)
  • 27 неуспешни тест(а)

Код

module Graphics
class Canvas
def initialize (width, height)
@canvas = []
height.times { @canvas << Array.new(width, false)}
end
def width
if @canvas.empty? then 0 else @canvas[0].count end
end
def height
@canvas.count
end
def set_pixel(x, y)
@canvas[y][x] = true
end
def pixel_at?(x, y)
@canvas[y][x]
end
def draw (figure)
figure.draw.each { |pixel| set_pixel(pixel[0], pixel[1]) }
end
def render_as (type)
type.render(self)
end
end
class Renderers
class Ascii
def self.render(canvas)
rendered_canvas = ""
canvas.height.times do |y|
canvas.width.times do |x|
rendered_canvas = rendered_canvas + render_symbol( canvas.pixel_at?(x,y) )
end
if y < canvas.height - 1 then rendered_canvas = rendered_canvas + "\n" end
end
rendered_canvas
end
def self.render_symbol (pixel)
if pixel then '@' else '-' end
end
end
class Html
START_OF_CODE = "<!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\">"
END_OF_CODE = "</div>
</body>
</html>"
def self.render(canvas)
rendered_canvas = ""
canvas.height.times do |y|
canvas.width.times do |x|
rendered_canvas = rendered_canvas + render_symbol(canvas.pixel_at?(x,y))
end
rendered_canvas = rendered_canvas + "<br>" unless y = canvas.height - 1
end
START_OF_CODE + rendered_canvas + END_OF_CODE
end
def self.render_symbol (pixel)
if pixel then "<b></b>" else "<i></i>" end
end
end
end
class Point
attr_reader :x, :y
alias_method :==, :eql?
def eql? (other)
self.class == other.class and @x == other.x and @y == other.y
end
def initialize (x, y)
@x = x
@y = y
end
def draw
[[@x, @y]]
end
end
class Line
attr_reader :from, :to
alias_method :==, :eql?
def eql? (other)
compared_points = points == other.points or points == other.points.reverse
self.class == other.class and compared_points
end
def initialize (first, last)
if first.x < last.x or ( first.x == last.x and first.y < last.y )
then @from = first and @to = last
else @from = last and @to = first
end
end
def draw
pixels_to_draw = []
#Here be Bresenham
pixels_to_draw
end
def points
[@from, @to]
end
end
class Rectangle
attr_reader :left, :right
alias_method :==, :eql?
def eql? (other)
compared_points = points == other.points or points == other.points.reverse
self.class == other.class and compared_points
end
def points
[@left, @right]
end
def initialize(first, last)
if first.x < last.x or ( first.x == last.x and first.y < last.y )
then @left = first and @right = last
else @left = last and @right = first
end
end
def top_left
x = @left.x
if left.y < right.y then y = left.y else y = right.y end
Point.new(x, y)
end
def top_right
x = @right.x
if right.y < left.y then y = right.y else y = left.y end
Point.new(x, y)
end
def bottom_left
x = @left.x
if left.y > right.y then y = left.y else y = right.y end
Point.new(x, y)
end
def bottom_right
x = @right.x
if right.y > left.y then y = right.y else y = left.y end
Point.new(x, y)
end
def draw
pixels_to_draw = []
pixels_to_draw.concat Line.new(top_left, top_right).draw
pixels_to_draw.concat Line.new(top_left, bottom_left).draw
pixels_to_draw.concat Line.new(top_right, bottom_right).draw
pixels_to_draw.concat Line.new(bottom_left, bottom_right).draw
pixels_to_draw
end
end
end

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

........F.FFFFFFFFFFF....FF.....F..F..........FFFFF.......FFFFFF.....

Failures:

  1) Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "@@@@@@@@@@@@@@@\n@-------------@\n@-@@@@@@@@@@@-@\n@-@---------@-@\n@-@------@@-@-@\n@-@---@@@---@-@\n@-@-@@------@-@\n@-@---------@-@\n@-@-@@@@----@-@\n@-@-@-------@-@\n@-@---------@-@\n@-@---------@-@\n@-@@@@@@@@@@@-@\n@-------------@\n@@@@@@@@@@@@@@@"
            got: "---------------\n---------------\n---------------\n---------------\n---------------\n---------------\n---------------\n---------------\n---------------\n----@----------\n---------------\n---------------\n---------------\n---------------\n---------------"
       
       (compared using ==)
       
       Diff:
       @@ -1,16 +1,16 @@
       -@@@@@@@@@@@@@@@
       -@-------------@
       -@-@@@@@@@@@@@-@
       -@-@---------@-@
       -@-@------@@-@-@
       -@-@---@@@---@-@
       -@-@-@@------@-@
       -@-@---------@-@
       -@-@-@@@@----@-@
       -@-@-@-------@-@
       -@-@---------@-@
       -@-@---------@-@
       -@-@@@@@@@@@@@-@
       -@-------------@
       -@@@@@@@@@@@@@@@
       +---------------
       +---------------
       +---------------
       +---------------
       +---------------
       +---------------
       +---------------
       +---------------
       +---------------
       +----@----------
       +---------------
       +---------------
       +---------------
       +---------------
       +---------------
     # /tmp/d20131223-4637-10w3ujj/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-10w3ujj/spec.rb:211: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-10w3ujj/solution.rb:17:in `set_pixel'
     # /tmp/d20131223-4637-10w3ujj/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: ascii.should eq rendering(expected)
       
       expected: "--------\n---@@@@-\n--------"
            got: "--------\n--------\n--------"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
        --------
       ----@@@@-
       +--------
        --------
     # /tmp/d20131223-4637-10w3ujj/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-10w3ujj/spec.rb:73: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: ascii.should eq rendering(expected)
       
       expected: "-@------\n-@------\n-@------\n-@------\n-@------\n-@------\n-@------\n--------"
            got: "--------\n--------\n--------\n--------\n--------\n--------\n--------\n--------"
       
       (compared using ==)
       
       Diff:
       @@ -1,9 +1,9 @@
       --@------
       --@------
       --@------
       --@------
       --@------
       --@------
       --@------
       +--------
       +--------
       +--------
       +--------
       +--------
       +--------
       +--------
        --------
     # /tmp/d20131223-4637-10w3ujj/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-10w3ujj/spec.rb:84: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: ascii.should eq rendering(expected)
       
       expected: "----------\n-@@-------\n---@@@@---\n-------@@-\n----------"
            got: "----------\n----------\n----------\n----------\n----------"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,6 @@
        ----------
       --@@-------
       ----@@@@---
       --------@@-
       +----------
       +----------
       +----------
        ----------
     # /tmp/d20131223-4637-10w3ujj/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-10w3ujj/spec.rb:100: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: ascii.should eq rendering(expected)
       
       expected: "----------\n-@--------\n-@--------\n--@-------\n--@-------\n--@-------\n--@-------\n---@------\n---@------\n----------"
            got: "----------\n----------\n----------\n----------\n----------\n----------\n----------\n----------\n----------\n----------"
       
       (compared using ==)
       
       Diff:
       @@ -1,11 +1,11 @@
        ----------
       --@--------
       --@--------
       ---@-------
       ---@-------
       ---@-------
       ---@-------
       ----@------
       ----@------
       +----------
       +----------
       +----------
       +----------
       +----------
       +----------
       +----------
       +----------
        ----------
     # /tmp/d20131223-4637-10w3ujj/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-10w3ujj/spec.rb:113: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: ascii.should eq rendering(expected)
       
       expected: "-@--------\n-@@-------\n-@-@@@@---\n-@-----@@-\n----------"
            got: "----------\n----------\n----------\n----------\n----------"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,6 @@
       --@--------
       --@@-------
       --@-@@@@---
       --@-----@@-
       +----------
       +----------
       +----------
       +----------
        ----------
     # /tmp/d20131223-4637-10w3ujj/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-10w3ujj/spec.rb:132: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: ascii.should eq rendering(expected)
       
       expected: "---\n-@-\n---"
            got: "---\n---\n---"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
        ---
       --@-
       +---
        ---
     # /tmp/d20131223-4637-10w3ujj/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-10w3ujj/spec.rb:145: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: ascii.should eq rendering(expected)
       
       expected: "----------\n-@@@@@@@@-\n-@------@-\n-@@@@@@@@-\n----------"
            got: "----------\n----------\n----------\n----------\n----------"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,6 @@
        ----------
       --@@@@@@@@-
       --@------@-
       --@@@@@@@@-
       +----------
       +----------
       +----------
        ----------
     # /tmp/d20131223-4637-10w3ujj/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-10w3ujj/spec.rb:158: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: ascii.should eq rendering(expected)
       
       expected: "----------\n-@@@@@@@@-\n-@------@-\n-@@@@@@@@-\n----------"
            got: "----------\n----------\n----------\n----------\n----------"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,6 @@
        ----------
       --@@@@@@@@-
       --@------@-
       --@@@@@@@@-
       +----------
       +----------
       +----------
        ----------
     # /tmp/d20131223-4637-10w3ujj/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-10w3ujj/spec.rb:171: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: ascii.should eq rendering(expected)
       
       expected: "----------\n-@@@@@@@@-\n----------"
            got: "----------\n----------\n----------"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
        ----------
       --@@@@@@@@-
       +----------
        ----------
     # /tmp/d20131223-4637-10w3ujj/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-10w3ujj/spec.rb:184: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: ascii.should eq rendering(expected)
       
       expected: "---\n-@-\n---"
            got: "---\n---\n---"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,4 @@
        ---
       --@-
       +---
        ---
     # /tmp/d20131223-4637-10w3ujj/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-10w3ujj/spec.rb:195: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 Renderers Html renders a grid of the size of the canvas
     Failure/Error: lines.size.should eq 3
       
       expected: 3
            got: 1
       
       (compared using ==)
     # /tmp/d20131223-4637-10w3ujj/spec.rb:283: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)>'

  14) Graphics Renderers Html renders simple canvases
     Failure/Error: html_rendering_of(canvas).should eq [
       
       expected: "<i></i><i></i><i></i><i></i><br><i></i><b></b><i></i><i></i><br><i></i><b></b><i></i><i></i>"
            got: "<i></i><i></i><i></i><i></i><i></i><b></b><i></i><i></i><i></i><b></b><i></i><i></i>"
       
       (compared using ==)
     # /tmp/d20131223-4637-10w3ujj/spec.rb:291: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)>'

  15) Graphics shapes Point comparison for equality is true if coordinates are the same
     Failure/Error: (a1 == a2).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-10w3ujj/spec.rb:348: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 Point comparison for equality returns the same hash for the same points
     Failure/Error: a1.hash.should eq a2.hash
       
       expected: -429288098
            got: -447093994
       
       (compared using ==)
     # /tmp/d20131223-4637-10w3ujj/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)>'

  17) Graphics shapes Line comparison for equality is true if line ends are the same
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-10w3ujj/spec.rb:435: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 comparison for equality is true if line ends are the same, even if swapped
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-10w3ujj/spec.rb:442: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)>'

  19) Graphics shapes Line comparison for equality is true if line is vertical and the bottom is given first
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-10w3ujj/spec.rb:449: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 comparison for equality works with eql? as well
     Failure/Error: a.should eql b
       
       expected: #<Graphics::Line:0xb8aa289c @from=#<Graphics::Point:0xb8aa28ec @x=1, @y=1>, @to=#<Graphics::Point:0xb8aa28c4 @x=10, @y=14>>
            got: #<Graphics::Line:0xb8aa293c @from=#<Graphics::Point:0xb8aa298c @x=1, @y=1>, @to=#<Graphics::Point:0xb8aa2964 @x=10, @y=14>>
       
       (compared using eql?)
       
       Diff:
       @@ -1,4 +1,4 @@
       -#<Graphics::Line:0xb8aa289c
       - @from=#<Graphics::Point:0xb8aa28ec @x=1, @y=1>,
       - @to=#<Graphics::Point:0xb8aa28c4 @x=10, @y=14>>
       +#<Graphics::Line:0xb8aa293c
       + @from=#<Graphics::Point:0xb8aa298c @x=1, @y=1>,
       + @to=#<Graphics::Point:0xb8aa2964 @x=10, @y=14>>
     # /tmp/d20131223-4637-10w3ujj/spec.rb:457: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 comparison for equality returns the same hash if the lines are the same
     Failure/Error: a.hash.should eq b.hash
       
       expected: 341852976
            got: 176131874
       
       (compared using ==)
     # /tmp/d20131223-4637-10w3ujj/spec.rb:465: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 Rectangle comparison for equality is true if rectangle points are the same
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-10w3ujj/spec.rb:526: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 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-10w3ujj/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)>'

  24) 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-10w3ujj/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)>'

  25) Graphics shapes Rectangle comparison for equality works with eql? as well
     Failure/Error: a.should eql b
       
       expected: #<Graphics::Rectangle:0xb8a556a0 @left=#<Graphics::Point:0xb8a55704 @x=1, @y=1>, @right=#<Graphics::Point:0xb8a556dc @x=10, @y=14>>
            got: #<Graphics::Rectangle:0xb8a5572c @left=#<Graphics::Point:0xb8a5577c @x=1, @y=1>, @right=#<Graphics::Point:0xb8a55754 @x=10, @y=14>>
       
       (compared using eql?)
       
       Diff:
       @@ -1,4 +1,4 @@
       -#<Graphics::Rectangle:0xb8a556a0
       - @left=#<Graphics::Point:0xb8a55704 @x=1, @y=1>,
       - @right=#<Graphics::Point:0xb8a556dc @x=10, @y=14>>
       +#<Graphics::Rectangle:0xb8a5572c
       + @left=#<Graphics::Point:0xb8a5577c @x=1, @y=1>,
       + @right=#<Graphics::Point:0xb8a55754 @x=10, @y=14>>
     # /tmp/d20131223-4637-10w3ujj/spec.rb:548: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 Rectangle comparison for equality returns the same hash if the rectangles are the same
     Failure/Error: a.hash.should eq b.hash
       
       expected: -723832656
            got: 884343420
       
       (compared using ==)
     # /tmp/d20131223-4637-10w3ujj/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)>'

  27) 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: 418021958
            got: -350117738
       
       (compared using ==)
     # /tmp/d20131223-4637-10w3ujj/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.09352 seconds
69 examples, 27 failures

Failed examples:

rspec /tmp/d20131223-4637-10w3ujj/spec.rb:203 # Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:69 # Graphics Canvas drawing of shapes and rasterization of lines works with simple horizontal lines
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:80 # Graphics Canvas drawing of shapes and rasterization of lines works with vertical lines
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:96 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
rspec /tmp/d20131223-4637-10w3ujj/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-10w3ujj/spec.rb:127 # Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:141 # Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:154 # Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
rspec /tmp/d20131223-4637-10w3ujj/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-10w3ujj/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-10w3ujj/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-10w3ujj/spec.rb:280 # Graphics Renderers Html renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:287 # Graphics Renderers Html renders simple canvases
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:347 # Graphics shapes Point comparison for equality is true if coordinates are the same
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:360 # Graphics shapes Point comparison for equality returns the same hash for the same points
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:431 # Graphics shapes Line comparison for equality is true if line ends are the same
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:438 # Graphics shapes Line comparison for equality is true if line ends are the same, even if swapped
rspec /tmp/d20131223-4637-10w3ujj/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-10w3ujj/spec.rb:452 # Graphics shapes Line comparison for equality works with eql? as well
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:461 # Graphics shapes Line comparison for equality returns the same hash if the lines are the same
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:522 # Graphics shapes Rectangle comparison for equality is true if rectangle points are the same
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:529 # Graphics shapes Rectangle comparison for equality is true if rectangle points are the same, even if swapped
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:536 # Graphics shapes Rectangle comparison for equality is true for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:543 # Graphics shapes Rectangle comparison for equality works with eql? as well
rspec /tmp/d20131223-4637-10w3ujj/spec.rb:552 # Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
rspec /tmp/d20131223-4637-10w3ujj/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:25 (преди почти 11 години)

+module Graphics
+ class Canvas
+ def initialize (width, height)
+ @canvas = []
+ height.times { @canvas << Array.new(width, false)}
+ end
+
+ def width
+ if @canvas.empty? then 0 else @canvas[0].count end
+ end
+
+ def height
+ @canvas.count
+ end
+
+ def set_pixel(x, y)
+ @canvas[y][x] = true
+ end
+
+ def pixel_at?(x, y)
+ @canvas[y][x]
+ end
+
+ def draw (figure)
+ figure.draw.each { |pixel| set_pixel(pixel[0], pixel[1]) }
+ end
+
+ def render_as (type)
+ type.render(self)
+ end
+ end
+
+ class Renderers
+ class Ascii
+ def self.render(canvas)
+ rendered_canvas = ""
+ canvas.height.times do |y|
+ canvas.width.times do |x|
+ rendered_canvas = rendered_canvas + render_symbol( canvas.pixel_at?(x,y) )
+ end
+ if y < canvas.height - 1 then rendered_canvas = rendered_canvas + "\n" end
+ end
+ rendered_canvas
+ end
+
+ def self.render_symbol (pixel)
+ if pixel then '@' else '-' end
+ end
+ end
+
+ class Html
+ START_OF_CODE = "<!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\">"
+
+
+ END_OF_CODE = "</div>
+ </body>
+ </html>"
+
+
+ def self.render(canvas)
+ rendered_canvas = ""
+ canvas.height.times do |y|
+ canvas.width.times do |x|
+ rendered_canvas = rendered_canvas + render_symbol(canvas.pixel_at?(x,y))
+ end
+ rendered_canvas = rendered_canvas + "<br>" unless y = canvas.height - 1
+ end
+ START_OF_CODE + rendered_canvas + END_OF_CODE
+ end
+
+ def self.render_symbol (pixel)
+ if pixel then "<b></b>" else "<i></i>" end
+ end
+ end
+ end
+
+ class Point
+ attr_reader :x, :y
+ alias_method :==, :eql?
+
+ def eql? (other)
+ self.class == other.class and @x == other.x and @y == other.y
+ end
+
+ def initialize (x, y)
+ @x = x
+ @y = y
+ end
+
+ def draw
+ [[@x, @y]]
+ end
+ end
+
+ class Line
+ attr_reader :from, :to
+ alias_method :==, :eql?
+
+ def eql? (other)
+ compared_points = points == other.points or points == other.points.reverse
+ self.class == other.class and compared_points
+ end
+
+ def initialize (first, last)
+ if first.x < last.x or ( first.x == last.x and first.y < last.y )
+ then @from = first and @to = last
+ else @from = last and @to = first
+ end
+ end
+
+ def draw
+ pixels_to_draw = []
+ #Here be Bresenham
+ pixels_to_draw
+ end
+
+ def points
+ [@from, @to]
+ end
+ end
+
+ class Rectangle
+ attr_reader :left, :right
+ alias_method :==, :eql?
+
+ def eql? (other)
+ compared_points = points == other.points or points == other.points.reverse
+ self.class == other.class and compared_points
+ end
+
+ def points
+ [@left, @right]
+ end
+
+ def initialize(first, last)
+ if first.x < last.x or ( first.x == last.x and first.y < last.y )
+ then @left = first and @right = last
+ else @left = last and @right = first
+ end
+ end
+
+ def top_left
+ x = @left.x
+ if left.y < right.y then y = left.y else y = right.y end
+ Point.new(x, y)
+ end
+
+ def top_right
+ x = @right.x
+ if right.y < left.y then y = right.y else y = left.y end
+ Point.new(x, y)
+ end
+
+ def bottom_left
+ x = @left.x
+ if left.y > right.y then y = left.y else y = right.y end
+ Point.new(x, y)
+ end
+
+ def bottom_right
+ x = @right.x
+ if right.y > left.y then y = right.y else y = left.y end
+ Point.new(x, y)
+ end
+
+
+ def draw
+ pixels_to_draw = []
+ pixels_to_draw.concat Line.new(top_left, top_right).draw
+ pixels_to_draw.concat Line.new(top_left, bottom_left).draw
+ pixels_to_draw.concat Line.new(top_right, bottom_right).draw
+ pixels_to_draw.concat Line.new(bottom_left, bottom_right).draw
+ pixels_to_draw
+ end
+ end
+end