Решение на Трета задача от Виктор Кетипов

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

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

Резултати

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

Код

module Graphics
class Canvas
def initialize(height, width)
@height = height
@width = width
@data = Array.new(@height) { Array.new(@width) }
end
def width()
@width
end
def height()
@height
end
def set_pixel(x, y)
@data[y][x] = true
end
def pixel_at?(x, y)
data[y][x]
end
#this is going to get ugly
def bresenham_steep_correction(x, y, a, b)
steep = (b-y).abs > (a-x).abs
if steep then
x, y = y, x
a, b = b, a
end
return x, y, a, b, steep
end
def bresenham_delta_error(x, y, a, b)
if x > a then
x, a = a, x
y, b = b, y
end
if y < b then step_y = 1 else step_y = -1 end
return x, y, a, b,(a - x).abs, (b - y).abs, (a - x).abs.fdiv(2), y, step_y
end
def bresenham_render(x, a, y, steep, error, delta_x, delta_y, step_y)
(x..a).each do |x|
if steep then set_pixel(y,x) else set_pixel(x,y) end
error -= delta_y
if error < 0 then
y += step_y
error += delta_x
end
end
end
def bresenham(x, y, a, b)
x, y, a, b, steep = bresenham_steep_correction(x, y, a, b)
x, y, a, b, delta_x, delta_y, error, y_1, step_y = bresenham_delta_error(x, y, a, b)
bresenham_render(x, a, y_1, steep, error, delta_x, delta_y, step_y)
end
def draw_rectangle(r)
(r.top_left.x..r.top_right.x).each {|x| set_pixel(x, r.top_left.y)}
(r.bottom_left.x()..r.bottom_right.x()).each {|x| set_pixel(x, r.bottom_right.y())}
(r.top_left.y() + 1..r.bottom_left.y() - 1).each {|y| set_pixel(r.top_left.x(), y)}
(r.top_right.y() + 1..r.bottom_right.y() - 1).each {|y| set_pixel(r.top_right.x(), y)}
end
def draw(fig)
fig.is_a?(Line) ? bresenham(fig.from.x, fig.from.y, fig.to.x, fig.to.y) : nil
fig.is_a?(Rectangle) ? draw_rectangle(fig) : nil
fig.is_a?(Point) ? set_pixel(fig.x, fig.y) : nil
end
def render_as(renderer_class)
# allocation ... not cool
renderer = renderer_class.new
renderer.render(@data)
end
end
module Renderers
class Ascii
def render_element(element)
if element
"@"
else
"-"
end
end
def render_line_end(column, last_column)
if column.equal?(last_column) then
""
else
"\n"
end
end
def render(data)
image = String.new()
data.each do |column|
column.each { |element| image += render_element(element) }
image += render_line_end(column, data.last())
end
image
end
end
class Html
def render_header()
return "<!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
def render_footer()
return "
</div>
</body>
</html>"
end
def render_element(element)
if element then
"<b></b>"
else
"<i></i>"
end
end
def render_line_end(column, last_column)
if column.equal?(last_column) then
""
else
"<br>\n\t\t"
end
end
def render_grid(data)
grid = ""
data.each do |column|
column.each { |element| grid += render_element(element)}
grid += render_line_end(column, data.last())
end
grid
end
def render(data)
image = render_header()
image += render_grid(data)
image += render_footer()
image
end
end
end
class Point
def initialize(x, y)
@x = x
@y = y
end
def x()
@x
end
def y()
@y
end
def ==(point)
point.x() == self.x() and point.y() == self.y()
end
def eql?(point)
point.x() == self.x() and point.y() == self.y()
end
end
class Line
def initialize(from, to)
@from = from
@to = to
end
def from()
@from
end
def to()
@to
end
def ==(point)
point.x() == self.x() and point.y() == self.y()
end
def eql?(point)
point.x() == self.x() and point.y() == self.y()
end
end
class Rectangle
def initialize(left, right)
@left = left
@right = right
end
def left()
@left
end
def right()
@right
end
def top_left()
min_x = [@left.x(), @right.x()].min
min_y = [@left.y(), @right.y()].min
Point.new min_x, min_y
end
def top_right()
max_x = [@left.x(), @right.x()].max
min_y = [@left.y(), @right.y()].min
Point.new max_x, min_y
end
def bottom_left()
min_x = [@left.x(), @right.x()].min
max_y = [@left.y(), @right.y()].max
Point.new min_x, max_y
end
def bottom_right()
max_x = [@left.x(), @right.x()].max
max_y = [@left.y(), @right.y()].max
Point.new max_x, max_y
end
def ==(r)
(left == r.left and right == r.right) or (left == r.right and right == r.left)
end
def eql?(r)
(left == r.left and right == r.right) or (left == r.right and right == r.left)
end
end
end

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

...F.F.F.FFF.F.F.FFF.FFF.FF........F.....FFFFFFFFFF.....F...F.FF.....

Failures:

  1) Graphics Canvas exposes its width and height via getters
     Failure/Error: canvas.width.should eq 5
       
       expected: 5
            got: 10
       
       (compared using ==)
     # /tmp/d20131223-4637-pru0nm/spec.rb:18: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 checking if a pixel at a given x and y is set
     Failure/Error: canvas.pixel_at?(0, 0).should be_false
     NameError:
       undefined local variable or method `data' for #<Graphics::Canvas:0xb8ff7f68>
     # /tmp/d20131223-4637-pru0nm/solution.rb:18:in `pixel_at?'
     # /tmp/d20131223-4637-pru0nm/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)>'

  3) Graphics Canvas allows setting a pixel at a given x and y
     Failure/Error: canvas.pixel_at?(3, 5).should be_false
     NameError:
       undefined local variable or method `data' for #<Graphics::Canvas:0xb8ff5934>
     # /tmp/d20131223-4637-pru0nm/solution.rb:18:in `pixel_at?'
     # /tmp/d20131223-4637-pru0nm/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)>'

  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
     NameError:
       undefined local variable or method `data' for #<Graphics::Canvas:0xb8fa482c>
     # /tmp/d20131223-4637-pru0nm/solution.rb:18:in `pixel_at?'
     # /tmp/d20131223-4637-pru0nm/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-pru0nm/solution.rb:15:in `set_pixel'
     # /tmp/d20131223-4637-pru0nm/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: ascii.should eq rendering(expected)
       
       expected: "--------\n---@@@@-\n--------"
            got: "---\n---@@@@\n---\n---\n---\n---\n---\n---"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,9 @@
       ---------
       ----@@@@-
       ---------
       +---
       +---@@@@
       +---
       +---
       +---
       +---
       +---
       +---
     # /tmp/d20131223-4637-pru0nm/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-pru0nm/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)>'

  7) 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-----\n-----\n-----\n-----\n-----\n-----"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,11 @@
       -----------
       --@@-------
       ----@@@@---
       --------@@-
       -----------
       +-----
       +-@@--
       +---@@@@
       +-------@@
       +-----
       +-----
       +-----
       +-----
       +-----
       +-----
     # /tmp/d20131223-4637-pru0nm/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-pru0nm/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)>'

  8) 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-----\n-----\n-----\n-----\n-----\n-----"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,11 @@
       --@--------
       --@@-------
       --@-@@@@---
       --@-----@@-
       -----------
       +-@---
       +-@@--
       +-@-@@@@
       +-@-----@@
       +-----
       +-----
       +-----
       +-----
       +-----
       +-----
     # /tmp/d20131223-4637-pru0nm/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-pru0nm/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)>'

  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-----\n-----\n-----\n-----\n-----\n-----"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,11 @@
       -----------
       --@@@@@@@@-
       --@------@-
       --@@@@@@@@-
       -----------
       +-----
       +-@@@@@@@@
       +-@------@
       +-@@@@@@@@
       +-----
       +-----
       +-----
       +-----
       +-----
       +-----
     # /tmp/d20131223-4637-pru0nm/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-pru0nm/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-----\n-----\n-----\n-----\n-----\n-----"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,11 @@
       -----------
       --@@@@@@@@-
       --@------@-
       --@@@@@@@@-
       -----------
       +-----
       +-@@@@@@@@
       +-@------@
       +-@@@@@@@@
       +-----
       +-----
       +-----
       +-----
       +-----
       +-----
     # /tmp/d20131223-4637-pru0nm/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-pru0nm/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---\n---\n---\n---\n---\n---\n---\n---"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,11 @@
       -----------
       --@@@@@@@@-
       -----------
       +---
       +-@@@@@@@@
       +---
       +---
       +---
       +---
       +---
       +---
       +---
       +---
     # /tmp/d20131223-4637-pru0nm/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-pru0nm/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 Renderers Ascii renders a grid of the size of the canvas
     Failure/Error: lines.size.should eq 3
       
       expected: 3
            got: 4
       
       (compared using ==)
     # /tmp/d20131223-4637-pru0nm/spec.rb:240: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)>'

  13) Graphics Renderers Ascii renders blank canvases
     Failure/Error: canvas.render_as(ascii).should eq rendering('
       
       expected: "----\n----\n----"
            got: "---\n---\n---\n---"
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,5 @@
       -----
       -----
       -----
       +---
       +---
       +---
       +---
     # /tmp/d20131223-4637-pru0nm/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)>'

  14) Graphics Renderers Ascii renders simple canvases
     Failure/Error: canvas.render_as(ascii).should eq rendering('
       
       expected: "@---\n----\n---@"
            got: "@--\n---\n---@\n---"
       
       (compared using ==)
       
       Diff:
       
       @@ -1,4 +1,5 @@
       -@---
       -----
       +@--
       +---
        ---@
       +---
     # /tmp/d20131223-4637-pru0nm/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)>'

  15) Graphics Renderers Html renders a grid of the size of the canvas
     Failure/Error: lines.size.should eq 3
       
       expected: 3
            got: 4
       
       (compared using ==)
     # /tmp/d20131223-4637-pru0nm/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)>'

  16) 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><br><i></i><b></b><i></i><br><i></i><b></b><i></i><br><i></i><i></i><i></i>"
       
       (compared using ==)
     # /tmp/d20131223-4637-pru0nm/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)>'

  17) Graphics shapes Point comparison for equality returns the same hash for the same points
     Failure/Error: a1.hash.should eq a2.hash
       
       expected: -505126247
            got: -290537659
       
       (compared using ==)
     # /tmp/d20131223-4637-pru0nm/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)>'

  18) Graphics shapes Line initialization with swapped points puts the leftmost point in the from field
     Failure/Error: inverted_line.from.x.should eq 1
       
       expected: 1
            got: 25
       
       (compared using ==)
     # /tmp/d20131223-4637-pru0nm/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: inverted_line.to.x.should eq 25
       
       expected: 25
            got: 1
       
       (compared using ==)
     # /tmp/d20131223-4637-pru0nm/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: vertical_line.from.y.should eq 1
       
       expected: 1
            got: 8
       
       (compared using ==)
     # /tmp/d20131223-4637-pru0nm/spec.rb:413: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: vertical_line.to.y.should eq 8
       
       expected: 8
            got: 1
       
       (compared using ==)
     # /tmp/d20131223-4637-pru0nm/spec.rb:418: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: (a == b).should be_false
     NoMethodError:
       undefined method `x' for #<Graphics::Line:0xb8ed0db0>
     # /tmp/d20131223-4637-pru0nm/solution.rb:193:in `=='
     # /tmp/d20131223-4637-pru0nm/spec.rb:428: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: (a == b).should be_true
     NoMethodError:
       undefined method `x' for #<Graphics::Line:0xb8ecbf18>
     # /tmp/d20131223-4637-pru0nm/solution.rb:193:in `=='
     # /tmp/d20131223-4637-pru0nm/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)>'

  24) Graphics shapes Line comparison for equality is true if line ends are the same, even if swapped
     Failure/Error: (a == b).should be_true
     NoMethodError:
       undefined method `x' for #<Graphics::Line:0xb8ecab18>
     # /tmp/d20131223-4637-pru0nm/solution.rb:193:in `=='
     # /tmp/d20131223-4637-pru0nm/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)>'

  25) 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
     NoMethodError:
       undefined method `x' for #<Graphics::Line:0xb8ec97b8>
     # /tmp/d20131223-4637-pru0nm/solution.rb:193:in `=='
     # /tmp/d20131223-4637-pru0nm/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)>'

  26) Graphics shapes Line comparison for equality works with eql? as well
     Failure/Error: a.should eql b
     NoMethodError:
       undefined method `x' for #<Graphics::Line:0xb8ec8980>
     # /tmp/d20131223-4637-pru0nm/solution.rb:196:in `eql?'
     # /tmp/d20131223-4637-pru0nm/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)>'

  27) 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: 1029068213
            got: -682904849
       
       (compared using ==)
     # /tmp/d20131223-4637-pru0nm/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)>'

  28) 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-pru0nm/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)>'

  29) 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-pru0nm/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)>'

  30) 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: 246946503
            got: -930315493
       
       (compared using ==)
     # /tmp/d20131223-4637-pru0nm/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)>'

  31) 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: 836088831
            got: -119542043
       
       (compared using ==)
     # /tmp/d20131223-4637-pru0nm/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.09537 seconds
69 examples, 31 failures

Failed examples:

rspec /tmp/d20131223-4637-pru0nm/spec.rb:16 # Graphics Canvas exposes its width and height via getters
rspec /tmp/d20131223-4637-pru0nm/spec.rb:28 # Graphics Canvas allows checking if a pixel at a given x and y is set
rspec /tmp/d20131223-4637-pru0nm/spec.rb:37 # Graphics Canvas allows setting a pixel at a given x and y
rspec /tmp/d20131223-4637-pru0nm/spec.rb:45 # Graphics Canvas drawing of shapes and rasterization of points works for a single one
rspec /tmp/d20131223-4637-pru0nm/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-pru0nm/spec.rb:69 # Graphics Canvas drawing of shapes and rasterization of lines works with simple horizontal lines
rspec /tmp/d20131223-4637-pru0nm/spec.rb:96 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
rspec /tmp/d20131223-4637-pru0nm/spec.rb:127 # Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
rspec /tmp/d20131223-4637-pru0nm/spec.rb:154 # Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
rspec /tmp/d20131223-4637-pru0nm/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-pru0nm/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-pru0nm/spec.rb:237 # Graphics Renderers Ascii renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-pru0nm/spec.rb:244 # Graphics Renderers Ascii renders blank canvases
rspec /tmp/d20131223-4637-pru0nm/spec.rb:252 # Graphics Renderers Ascii renders simple canvases
rspec /tmp/d20131223-4637-pru0nm/spec.rb:280 # Graphics Renderers Html renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-pru0nm/spec.rb:287 # Graphics Renderers Html renders simple canvases
rspec /tmp/d20131223-4637-pru0nm/spec.rb:360 # Graphics shapes Point comparison for equality returns the same hash for the same points
rspec /tmp/d20131223-4637-pru0nm/spec.rb:401 # Graphics shapes Line initialization with swapped points puts the leftmost point in the from field
rspec /tmp/d20131223-4637-pru0nm/spec.rb:406 # Graphics shapes Line initialization with swapped points puts the rightmost point in the to field
rspec /tmp/d20131223-4637-pru0nm/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-pru0nm/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-pru0nm/spec.rb:424 # Graphics shapes Line comparison for equality is false if any of the points differ
rspec /tmp/d20131223-4637-pru0nm/spec.rb:431 # Graphics shapes Line comparison for equality is true if line ends are the same
rspec /tmp/d20131223-4637-pru0nm/spec.rb:438 # Graphics shapes Line comparison for equality is true if line ends are the same, even if swapped
rspec /tmp/d20131223-4637-pru0nm/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-pru0nm/spec.rb:452 # Graphics shapes Line comparison for equality works with eql? as well
rspec /tmp/d20131223-4637-pru0nm/spec.rb:461 # Graphics shapes Line comparison for equality returns the same hash if the lines are the same
rspec /tmp/d20131223-4637-pru0nm/spec.rb:504 # Graphics shapes Rectangle initialization puts the leftmost point in its left field
rspec /tmp/d20131223-4637-pru0nm/spec.rb:536 # Graphics shapes Rectangle comparison for equality is true for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-pru0nm/spec.rb:552 # Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
rspec /tmp/d20131223-4637-pru0nm/spec.rb:559 # Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners

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

Виктор обнови решението на 22.12.2013 22:58 (преди почти 11 години)

+module Graphics
+ class Canvas
+ def initialize(height, width)
+ @height = height
+ @width = width
+ @data = Array.new(@height) { Array.new(@width) }
+ end
+ def width()
+ @width
+ end
+ def height()
+ @height
+ end
+ def set_pixel(x, y)
+ @data[y][x] = true
+ end
+ def pixel_at?(x, y)
+ data[y][x]
+ end
+ #this is going to get ugly
+ def bresenham_steep_correction(x, y, a, b)
+ steep = (b-y).abs > (a-x).abs
+ if steep then
+ x, y = y, x
+ a, b = b, a
+ end
+ return x, y, a, b, steep
+ end
+ def bresenham_delta_error(x, y, a, b)
+ if x > a then
+ x, a = a, x
+ y, b = b, y
+ end
+ if y < b then step_y = 1 else step_y = -1 end
+ return x, y, a, b,(a - x).abs, (b - y).abs, (a - x).abs.fdiv(2), y, step_y
+ end
+ def bresenham_render(x, a, y, steep, error, delta_x, delta_y, step_y)
+ (x..a).each do |x|
+ if steep then set_pixel(y,x) else set_pixel(x,y) end
+ error -= delta_y
+ if error < 0 then
+ y += step_y
+ error += delta_x
+ end
+ end
+ end
+ def bresenham(x, y, a, b)
+ x, y, a, b, steep = bresenham_steep_correction(x, y, a, b)
+ x, y, a, b, delta_x, delta_y, error, y_1, step_y = bresenham_delta_error(x, y, a, b)
+ bresenham_render(x, a, y_1, steep, error, delta_x, delta_y, step_y)
+ end
+ def draw_rectangle(r)
+ (r.top_left.x..r.top_right.x).each {|x| set_pixel(x, r.top_left.y)}
+ (r.bottom_left.x()..r.bottom_right.x()).each {|x| set_pixel(x, r.bottom_right.y())}
+ (r.top_left.y() + 1..r.bottom_left.y() - 1).each {|y| set_pixel(r.top_left.x(), y)}
+ (r.top_right.y() + 1..r.bottom_right.y() - 1).each {|y| set_pixel(r.top_right.x(), y)}
+ end
+ def draw(fig)
+ fig.is_a?(Line) ? bresenham(fig.from.x, fig.from.y, fig.to.x, fig.to.y) : nil
+ fig.is_a?(Rectangle) ? draw_rectangle(fig) : nil
+ fig.is_a?(Point) ? set_pixel(fig.x, fig.y) : nil
+ end
+ def render_as(renderer_class)
+ # allocation ... not cool
+ renderer = renderer_class.new
+ renderer.render(@data)
+ end
+ end
+
+ module Renderers
+ class Ascii
+ def render_element(element)
+ if element
+ "@"
+ else
+ "-"
+ end
+ end
+ def render_line_end(column, last_column)
+ if column.equal?(last_column) then
+ ""
+ else
+ "\n"
+ end
+ end
+ def render(data)
+ image = String.new()
+ data.each do |column|
+ column.each { |element| image += render_element(element) }
+ image += render_line_end(column, data.last())
+ end
+ image
+ end
+ end
+
+ class Html
+ def render_header()
+ return "<!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
+ def render_footer()
+ return "
+ </div>
+</body>
+</html>"
+ end
+ def render_element(element)
+ if element then
+ "<b></b>"
+ else
+ "<i></i>"
+ end
+ end
+ def render_line_end(column, last_column)
+ if column.equal?(last_column) then
+ ""
+ else
+ "<br>\n\t\t"
+ end
+ end
+ def render_grid(data)
+ grid = ""
+ data.each do |column|
+ column.each { |element| grid += render_element(element)}
+ grid += render_line_end(column, data.last())
+ end
+ grid
+ end
+ def render(data)
+ image = render_header()
+ image += render_grid(data)
+ image += render_footer()
+ image
+ end
+ end
+ end
+
+ class Point
+ def initialize(x, y)
+ @x = x
+ @y = y
+ end
+ def x()
+ @x
+ end
+ def y()
+ @y
+ end
+ def ==(point)
+ point.x() == self.x() and point.y() == self.y()
+ end
+ def eql?(point)
+ point.x() == self.x() and point.y() == self.y()
+ end
+ end
+
+ class Line
+ def initialize(from, to)
+ @from = from
+ @to = to
+ end
+ def from()
+ @from
+ end
+ def to()
+ @to
+ end
+ def ==(point)
+ point.x() == self.x() and point.y() == self.y()
+ end
+ def eql?(point)
+ point.x() == self.x() and point.y() == self.y()
+ end
+ end
+
+ class Rectangle
+ def initialize(left, right)
+ @left = left
+ @right = right
+ end
+ def left()
+ @left
+ end
+ def right()
+ @right
+ end
+ def top_left()
+ min_x = [@left.x(), @right.x()].min
+ min_y = [@left.y(), @right.y()].min
+ Point.new min_x, min_y
+ end
+ def top_right()
+ max_x = [@left.x(), @right.x()].max
+ min_y = [@left.y(), @right.y()].min
+ Point.new max_x, min_y
+ end
+ def bottom_left()
+ min_x = [@left.x(), @right.x()].min
+ max_y = [@left.y(), @right.y()].max
+ Point.new min_x, max_y
+ end
+ def bottom_right()
+ max_x = [@left.x(), @right.x()].max
+ max_y = [@left.y(), @right.y()].max
+ Point.new max_x, max_y
+ end
+ def ==(r)
+ (left == r.left and right == r.right) or (left == r.right and right == r.left)
+ end
+ def eql?(r)
+ (left == r.left and right == r.right) or (left == r.right and right == r.left)
+ end
+ end
+ canvas = Canvas.new 30, 30
+
+ # Door frame and window
+ canvas.draw Rectangle.new(Point.new(3, 3), Point.new(18, 12))
+ canvas.draw Rectangle.new(Point.new(1, 1), Point.new(20, 28))
+
+ # Door knob
+ canvas.draw Line.new(Point.new(4, 15), Point.new(7, 15))
+ canvas.draw Point.new(4, 16)
+
+ # Big "R"
+ canvas.draw Line.new(Point.new(8, 5), Point.new(8, 10))
+ canvas.draw Line.new(Point.new(9, 5), Point.new(12, 5))
+ canvas.draw Line.new(Point.new(9, 7), Point.new(12, 7))
+ canvas.draw Point.new(13, 6)
+ canvas.draw Line.new(Point.new(12, 8), Point.new(13, 10))
+
+ puts canvas.render_as(Renderers::Ascii)
+end

Виктор обнови решението на 22.12.2013 22:59 (преди почти 11 години)

module Graphics
class Canvas
def initialize(height, width)
@height = height
@width = width
@data = Array.new(@height) { Array.new(@width) }
end
def width()
@width
end
def height()
@height
end
def set_pixel(x, y)
@data[y][x] = true
end
def pixel_at?(x, y)
data[y][x]
end
#this is going to get ugly
def bresenham_steep_correction(x, y, a, b)
steep = (b-y).abs > (a-x).abs
if steep then
x, y = y, x
a, b = b, a
end
return x, y, a, b, steep
end
def bresenham_delta_error(x, y, a, b)
if x > a then
x, a = a, x
y, b = b, y
end
if y < b then step_y = 1 else step_y = -1 end
return x, y, a, b,(a - x).abs, (b - y).abs, (a - x).abs.fdiv(2), y, step_y
end
def bresenham_render(x, a, y, steep, error, delta_x, delta_y, step_y)
(x..a).each do |x|
if steep then set_pixel(y,x) else set_pixel(x,y) end
error -= delta_y
if error < 0 then
y += step_y
error += delta_x
end
end
end
def bresenham(x, y, a, b)
x, y, a, b, steep = bresenham_steep_correction(x, y, a, b)
x, y, a, b, delta_x, delta_y, error, y_1, step_y = bresenham_delta_error(x, y, a, b)
bresenham_render(x, a, y_1, steep, error, delta_x, delta_y, step_y)
end
def draw_rectangle(r)
(r.top_left.x..r.top_right.x).each {|x| set_pixel(x, r.top_left.y)}
(r.bottom_left.x()..r.bottom_right.x()).each {|x| set_pixel(x, r.bottom_right.y())}
(r.top_left.y() + 1..r.bottom_left.y() - 1).each {|y| set_pixel(r.top_left.x(), y)}
(r.top_right.y() + 1..r.bottom_right.y() - 1).each {|y| set_pixel(r.top_right.x(), y)}
end
def draw(fig)
fig.is_a?(Line) ? bresenham(fig.from.x, fig.from.y, fig.to.x, fig.to.y) : nil
fig.is_a?(Rectangle) ? draw_rectangle(fig) : nil
fig.is_a?(Point) ? set_pixel(fig.x, fig.y) : nil
end
def render_as(renderer_class)
# allocation ... not cool
renderer = renderer_class.new
renderer.render(@data)
end
end
module Renderers
class Ascii
def render_element(element)
if element
"@"
else
"-"
end
end
def render_line_end(column, last_column)
if column.equal?(last_column) then
""
else
"\n"
end
end
def render(data)
image = String.new()
data.each do |column|
column.each { |element| image += render_element(element) }
image += render_line_end(column, data.last())
end
image
end
end
class Html
def render_header()
return "<!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
def render_footer()
return "
</div>
</body>
</html>"
end
def render_element(element)
if element then
"<b></b>"
else
"<i></i>"
end
end
def render_line_end(column, last_column)
if column.equal?(last_column) then
""
else
"<br>\n\t\t"
end
end
def render_grid(data)
grid = ""
data.each do |column|
column.each { |element| grid += render_element(element)}
grid += render_line_end(column, data.last())
end
grid
end
def render(data)
image = render_header()
image += render_grid(data)
image += render_footer()
image
end
end
end
class Point
def initialize(x, y)
@x = x
@y = y
end
def x()
@x
end
def y()
@y
end
def ==(point)
point.x() == self.x() and point.y() == self.y()
end
def eql?(point)
point.x() == self.x() and point.y() == self.y()
end
end
class Line
def initialize(from, to)
@from = from
@to = to
end
def from()
@from
end
def to()
@to
end
def ==(point)
point.x() == self.x() and point.y() == self.y()
end
def eql?(point)
point.x() == self.x() and point.y() == self.y()
end
end
class Rectangle
def initialize(left, right)
@left = left
@right = right
end
def left()
@left
end
def right()
@right
end
def top_left()
min_x = [@left.x(), @right.x()].min
min_y = [@left.y(), @right.y()].min
Point.new min_x, min_y
end
def top_right()
max_x = [@left.x(), @right.x()].max
min_y = [@left.y(), @right.y()].min
Point.new max_x, min_y
end
def bottom_left()
min_x = [@left.x(), @right.x()].min
max_y = [@left.y(), @right.y()].max
Point.new min_x, max_y
end
def bottom_right()
max_x = [@left.x(), @right.x()].max
max_y = [@left.y(), @right.y()].max
Point.new max_x, max_y
end
def ==(r)
(left == r.left and right == r.right) or (left == r.right and right == r.left)
end
def eql?(r)
(left == r.left and right == r.right) or (left == r.right and right == r.left)
end
end
- canvas = Canvas.new 30, 30
-
- # Door frame and window
- canvas.draw Rectangle.new(Point.new(3, 3), Point.new(18, 12))
- canvas.draw Rectangle.new(Point.new(1, 1), Point.new(20, 28))
-
- # Door knob
- canvas.draw Line.new(Point.new(4, 15), Point.new(7, 15))
- canvas.draw Point.new(4, 16)
-
- # Big "R"
- canvas.draw Line.new(Point.new(8, 5), Point.new(8, 10))
- canvas.draw Line.new(Point.new(9, 5), Point.new(12, 5))
- canvas.draw Line.new(Point.new(9, 7), Point.new(12, 7))
- canvas.draw Point.new(13, 6)
- canvas.draw Line.new(Point.new(12, 8), Point.new(13, 10))
-
- puts canvas.render_as(Renderers::Ascii)
end