Решение на Трета задача от Георги Урумов

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

Към профила на Георги Урумов

Резултати

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

Код

module Graphics
class Point
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
def ==(point)
eql?(point)
end
def eql?(point)
@x == point.x and @y == point.y
end
end
class Line
attr_reader :from, :to
def initialize(from, to)
if from == to then @from = @to = to
elsif from.x == to.x
@from = from.y < to.y ? from : to
@to = from.y > to.y ? from : to
else
@from = from.x < to.x ? from : to
@to = from.x > to.x ? from : to
end
end
def ==(line)
eql?(line)
end
def eql?(line)
@from == line.from and @to == line.to
end
end
class Rectangle
attr_reader :left, :right
def initialize(left, right)
if left == right then @left = @right = right
elsif left.x == right.x
@left = left.y < right.y ? left : right
@right = left.y > right.y ? left : right
else
@left = left.x < right.x ? left : right
@right = left.x > right.x ? left : right
end
end
def top_left
@left
end
def top_right
Point.new(right.x, left.y)
end
def bottom_left
Point.new(left.x, right.y)
end
def bottom_right
@right
end
def ==(rectangle)
eql?(rectangle)
end
def eql?(rectangle)
@left == rectangle.left and @right == rectangle.right
end
end
class Canvas
attr_reader :width, :height
def initialize(width, height)
@width = width
@height = height
@canvas = Array.new(@width) {Array.new(@height,false)}
end
def set_pixel(x, y)
@canvas[x][y] = true
end
def pixel_at?(x, y)
@canvas[x][y]
end
def draw(figure)
if figure.is_a?(Point)
set_pixel(figure.x, figure.y)
elsif figure.is_a?(Line)
draw_line(figure)
else
draw_rectangle(figure)
end
end
def draw_line(line)
if line.from.x == line.to.x or line.from.y == line.to.y
(line.from.x..line.to.x).each { |i| set_pixel(i, line.from.y)}
(line.from.y..line.to.y).each { |k| set_pixel(line.from.x, k)}
else
#bresenham(line)
end
end
def draw_rectangle(rectangle)
draw_line(Line.new(rectangle.top_left, rectangle.top_right))
draw_line(Line.new(rectangle.top_left, rectangle.bottom_left))
draw_line(Line.new(rectangle.top_right, rectangle.bottom_right))
draw_line(Line.new(rectangle.bottom_left, rectangle.bottom_right))
end
def render_as(renderer)
result = renderer.new(self)
result.draw
end
end
module Renderers
class Ascii
def initialize(canvas)
@canvas = canvas
end
def draw
(0...@canvas.width).each do |x|
(0...@canvas.height).each { |y| render(y, x)}
puts "" unless x == @canvas.width
end
end
def render(x, y)
pixel = @canvas.pixel_at?(x,y) ? '@' : '-'
print pixel
end
end
class Html
def header
print '<!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 footer
print '</div></body></html>'
end
def initialize(canvas)
@canvas = canvas
end
def draw
header
(0...@canvas.width).each do |x|
(0...@canvas.height).each { |y| render(y, x)}
print "<br>" unless x == @canvas.width
end
footer
end
def render(x, y)
pixel = @canvas.pixel_at?(x,y) ? '<b></b>' : '<i></i>'
print pixel
end
end
end
end

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

........@@@@@@@@@@@@@@@
@-------------@
@-@@@@@@@@@@@-@
@-@---------@-@
@-@---------@-@
@-@---------@-@
@-@---------@-@
@-@---------@-@
@-@-@@@@----@-@
@-@-@-------@-@
@-@---------@-@
@-@---------@-@
@-@@@@@@@@@@@-@
@-------------@
@@@@@@@@@@@@@@@
F.F---
---
---
---
---
---
---
---
F-@------
-@------
-@------
-@------
-@------
-@------
-@------
--------
F-----
-----
-----
-----
-----
-----
-----
-----
-----
-----
F----------
----------
----------
----------
----------
----------
----------
----------
----------
----------
F-@---
-@---
-@---
-@---
-----
-----
-----
-----
-----
-----
F---
-@-
---
F-----
-@@@@
-@---
-@@@@
-----
-----
-----
-----
-----
-----
F-----
-@@@@
-@---
-@@@@
-----
-----
-----
-----
-----
-----
F---
-@@
---
---
---
---
---
---
---
---
F---
-@-
---
F---
---
---
---
F---
---
---
---
F@--
---
---
---
F<!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"><i></i><i></i><i></i><br><i></i><i></i><i></i><br><i></i><i></i><i></i><br><i></i><i></i><i></i><br></div></body></html>F<!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"><i></i><i></i><i></i><br><i></i><i></i><i></i><br><i></i><i></i><i></i><br><i></i><i></i><i></i><br></div></body></html>F<!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"><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><br></div></body></html>F<!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"><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><br></div></body></html>F.......F..............F.........F.FF.F.F.

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: 0...15
       
       (compared using ==)
       
       Diff:
       @@ -1,16 +1,2 @@
       -@@@@@@@@@@@@@@@
       -@-------------@
       -@-@@@@@@@@@@@-@
       -@-@---------@-@
       -@-@------@@-@-@
       -@-@---@@@---@-@
       -@-@-@@------@-@
       -@-@---------@-@
       -@-@-@@@@----@-@
       -@-@-@-------@-@
       -@-@---------@-@
       -@-@---------@-@
       -@-@@@@@@@@@@@-@
       -@-------------@
       -@@@@@@@@@@@@@@@
       +0...15
     # /tmp/d20131223-4637-h7paio/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-h7paio/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-h7paio/solution.rb:92:in `set_pixel'
     # /tmp/d20131223-4637-h7paio/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: 0...8
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,2 @@
       ---------
       ----@@@@-
       ---------
       +0...8
     # /tmp/d20131223-4637-h7paio/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-h7paio/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: 0...8
       
       (compared using ==)
       
       Diff:
       @@ -1,9 +1,2 @@
       --@------
       --@------
       --@------
       --@------
       --@------
       --@------
       --@------
       ---------
       +0...8
     # /tmp/d20131223-4637-h7paio/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-h7paio/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: 0...10
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,2 @@
       -----------
       --@@-------
       ----@@@@---
       --------@@-
       -----------
       +0...10
     # /tmp/d20131223-4637-h7paio/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-h7paio/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: 0...10
       
       (compared using ==)
       
       Diff:
       @@ -1,11 +1,2 @@
       -----------
       --@--------
       --@--------
       ---@-------
       ---@-------
       ---@-------
       ---@-------
       ----@------
       ----@------
       -----------
       +0...10
     # /tmp/d20131223-4637-h7paio/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-h7paio/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: 0...10
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,2 @@
       --@--------
       --@@-------
       --@-@@@@---
       --@-----@@-
       -----------
       +0...10
     # /tmp/d20131223-4637-h7paio/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-h7paio/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: 0...3
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,2 @@
       ----
       --@-
       ----
       +0...3
     # /tmp/d20131223-4637-h7paio/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-h7paio/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: 0...10
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,2 @@
       -----------
       --@@@@@@@@-
       --@------@-
       --@@@@@@@@-
       -----------
       +0...10
     # /tmp/d20131223-4637-h7paio/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-h7paio/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: 0...10
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,2 @@
       -----------
       --@@@@@@@@-
       --@------@-
       --@@@@@@@@-
       -----------
       +0...10
     # /tmp/d20131223-4637-h7paio/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-h7paio/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: 0...10
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,2 @@
       -----------
       --@@@@@@@@-
       -----------
       +0...10
     # /tmp/d20131223-4637-h7paio/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-h7paio/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: 0...3
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,2 @@
       ----
       --@-
       ----
       +0...3
     # /tmp/d20131223-4637-h7paio/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-h7paio/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 Ascii renders a grid of the size of the canvas
     Failure/Error: lines = canvas.render_as(ascii).split("\n")
     NoMethodError:
       undefined method `split' for 0...4:Range
     # /tmp/d20131223-4637-h7paio/spec.rb:238:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

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

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

  16) Graphics Renderers Html returns html
     Failure/Error: html.gsub(/\s+/, '').downcase
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20131223-4637-h7paio/spec.rb:314:in `normalize_html'
     # /tmp/d20131223-4637-h7paio/spec.rb:272:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  17) Graphics Renderers Html renders a grid of the size of the canvas
     Failure/Error: html.gsub(/\s+/, '').downcase
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20131223-4637-h7paio/spec.rb:314:in `normalize_html'
     # /tmp/d20131223-4637-h7paio/spec.rb:309:in `html_rendering_of'
     # /tmp/d20131223-4637-h7paio/spec.rb:281:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  18) Graphics Renderers Html renders simple canvases
     Failure/Error: html.gsub(/\s+/, '').downcase
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20131223-4637-h7paio/spec.rb:314:in `normalize_html'
     # /tmp/d20131223-4637-h7paio/spec.rb:309:in `html_rendering_of'
     # /tmp/d20131223-4637-h7paio/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)>'

  19) Graphics Renderers Html returns the same rendering when called twice
     Failure/Error: html.gsub(/\s+/, '').downcase
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20131223-4637-h7paio/spec.rb:314:in `normalize_html'
     # /tmp/d20131223-4637-h7paio/spec.rb:302: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)>'

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

  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: -287661171
            got: 102717841
       
       (compared using ==)
     # /tmp/d20131223-4637-h7paio/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 for rectangles defined with different diagonal corners
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-h7paio/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)>'

  23) 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: 658556941
            got: -575907815
       
       (compared using ==)
     # /tmp/d20131223-4637-h7paio/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)>'

  24) 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: 118729355
            got: -253085989
       
       (compared using ==)
     # /tmp/d20131223-4637-h7paio/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)>'

  25) Graphics shapes Rectangle corners top left
     Failure/Error: rect.top_left.y.should eq 2
       
       expected: 2
            got: 4
       
       (compared using ==)
     # /tmp/d20131223-4637-h7paio/spec.rb:578: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 corners bottom right
     Failure/Error: rect.bottom_right.y.should eq 4
       
       expected: 4
            got: 2
       
       (compared using ==)
     # /tmp/d20131223-4637-h7paio/spec.rb:590: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.09731 seconds
69 examples, 26 failures

Failed examples:

rspec /tmp/d20131223-4637-h7paio/spec.rb:203 # Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
rspec /tmp/d20131223-4637-h7paio/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-h7paio/spec.rb:69 # Graphics Canvas drawing of shapes and rasterization of lines works with simple horizontal lines
rspec /tmp/d20131223-4637-h7paio/spec.rb:80 # Graphics Canvas drawing of shapes and rasterization of lines works with vertical lines
rspec /tmp/d20131223-4637-h7paio/spec.rb:96 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
rspec /tmp/d20131223-4637-h7paio/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-h7paio/spec.rb:127 # Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
rspec /tmp/d20131223-4637-h7paio/spec.rb:141 # Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
rspec /tmp/d20131223-4637-h7paio/spec.rb:154 # Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
rspec /tmp/d20131223-4637-h7paio/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-h7paio/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-h7paio/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-h7paio/spec.rb:237 # Graphics Renderers Ascii renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-h7paio/spec.rb:244 # Graphics Renderers Ascii renders blank canvases
rspec /tmp/d20131223-4637-h7paio/spec.rb:252 # Graphics Renderers Ascii renders simple canvases
rspec /tmp/d20131223-4637-h7paio/spec.rb:271 # Graphics Renderers Html returns html
rspec /tmp/d20131223-4637-h7paio/spec.rb:280 # Graphics Renderers Html renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-h7paio/spec.rb:287 # Graphics Renderers Html renders simple canvases
rspec /tmp/d20131223-4637-h7paio/spec.rb:298 # Graphics Renderers Html returns the same rendering when called twice
rspec /tmp/d20131223-4637-h7paio/spec.rb:360 # Graphics shapes Point comparison for equality returns the same hash for the same points
rspec /tmp/d20131223-4637-h7paio/spec.rb:461 # Graphics shapes Line comparison for equality returns the same hash if the lines are the same
rspec /tmp/d20131223-4637-h7paio/spec.rb:536 # Graphics shapes Rectangle comparison for equality is true for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-h7paio/spec.rb:552 # Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
rspec /tmp/d20131223-4637-h7paio/spec.rb:559 # Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-h7paio/spec.rb:575 # Graphics shapes Rectangle corners top left
rspec /tmp/d20131223-4637-h7paio/spec.rb:587 # Graphics shapes Rectangle corners bottom right

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

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

+module Graphics
+
+ class Point
+ attr_reader :x, :y
+
+ def initialize(x, y)
+ @x = x
+ @y = y
+ end
+
+ def ==(point)
+ eql?(point)
+ end
+
+ def eql?(point)
+ @x == point.x and @y == point.y
+ end
+ end
+
+ class Line
+ attr_reader :from, :to
+
+ def initialize(from, to)
+ if from == to then @from = @to = to
+ elsif from.x == to.x
+ @from = from.y < to.y ? from : to
+ @to = from.y > to.y ? from : to
+ else
+ @from = from.x < to.x ? from : to
+ @to = from.x > to.x ? from : to
+ end
+ end
+
+ def ==(line)
+ eql?(line)
+ end
+
+ def eql?(line)
+ @from == line.from and @to == line.to
+ end
+ end
+
+ class Rectangle
+ attr_reader :left, :right
+
+ def initialize(left, right)
+ if left == right then @left = @right = right
+ elsif left.x == right.x
+ @left = left.y < right.y ? left : right
+ @right = left.y > right.y ? left : right
+ else
+ @left = left.x < right.x ? left : right
+ @right = left.x > right.x ? left : right
+ end
+ end
+
+ def top_left
+ @left
+ end
+
+ def top_right
+ Point.new(right.x, left.y)
+ end
+
+ def bottom_left
+ Point.new(left.x, right.y)
+ end
+
+ def bottom_right
+ @right
+ end
+
+ def ==(rectangle)
+ eql?(rectangle)
+ end
+
+ def eql?(rectangle)
+ @left == rectangle.left and @right == rectangle.right
+ end
+ end
+
+ class Canvas
+ attr_reader :width, :height
+
+ def initialize(width, height)
+ @width = width
+ @height = height
+ @canvas = Array.new(@width) {Array.new(@height,false)}
+ end
+
+ def set_pixel(x, y)
+ @canvas[x][y] = true
+ end
+
+ def pixel_at?(x, y)
+ @canvas[x][y]
+ end
+
+ def draw(figure)
+ if figure.is_a?(Point)
+ set_pixel(figure.x, figure.y)
+ elsif figure.is_a?(Line)
+ draw_line(figure)
+ else
+ draw_rectangle(figure)
+ end
+ end
+
+ def draw_line(line)
+ if line.from.x == line.to.x or line.from.y == line.to.y
+ (line.from.x..line.to.x).each { |i| set_pixel(i, line.from.y)}
+ (line.from.y..line.to.y).each { |k| set_pixel(line.from.x, k)}
+ else
+ #bresenham(line)
+ end
+ end
+
+ def draw_rectangle(rectangle)
+ draw_line(Line.new(rectangle.top_left, rectangle.top_right))
+ draw_line(Line.new(rectangle.top_left, rectangle.bottom_left))
+ draw_line(Line.new(rectangle.top_right, rectangle.bottom_right))
+ draw_line(Line.new(rectangle.bottom_left, rectangle.bottom_right))
+ end
+
+ def render_as(renderer)
+ result = renderer.new(self)
+ result.draw
+ end
+ end
+
+ module Renderers
+ class Ascii
+
+ def initialize(canvas)
+ @canvas = canvas
+ end
+
+ def draw
+ (0...@canvas.width).each do |x|
+ (0...@canvas.height).each { |y| render(y, x)}
+ puts "" unless x == @canvas.width
+ end
+ end
+
+ def render(x, y)
+ pixel = @canvas.pixel_at?(x,y) ? '@' : '-'
+ print pixel
+ end
+ end
+
+ class Html
+
+ @header = <<-EOM
+ <!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">
+ EOM
+
+ @footer = "</div></body></html>"
+
+ def initialize(canvas)
+ @canvas = canvas
+ end
+
+ def draw
+ print @header
+ (0...@canvas.width).each do |x|
+ (0...@canvas.height).each { |y| render(y, x)}
+ print "<br>" unless x == @canvas.width
+ end
+ print @footer
+ end
+
+ def render(x, y)
+ pixel = @canvas.pixel_at?(x,y) ? '<b></b>' : '<i></i>'
+ print pixel
+ end
+ end
+ end
+end

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

module Graphics
class Point
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
def ==(point)
eql?(point)
end
def eql?(point)
@x == point.x and @y == point.y
end
end
class Line
attr_reader :from, :to
def initialize(from, to)
if from == to then @from = @to = to
elsif from.x == to.x
@from = from.y < to.y ? from : to
@to = from.y > to.y ? from : to
else
@from = from.x < to.x ? from : to
@to = from.x > to.x ? from : to
end
end
def ==(line)
eql?(line)
end
def eql?(line)
@from == line.from and @to == line.to
end
end
class Rectangle
attr_reader :left, :right
def initialize(left, right)
if left == right then @left = @right = right
elsif left.x == right.x
@left = left.y < right.y ? left : right
@right = left.y > right.y ? left : right
else
@left = left.x < right.x ? left : right
@right = left.x > right.x ? left : right
end
end
def top_left
@left
end
def top_right
Point.new(right.x, left.y)
end
def bottom_left
Point.new(left.x, right.y)
end
def bottom_right
@right
end
def ==(rectangle)
eql?(rectangle)
end
def eql?(rectangle)
@left == rectangle.left and @right == rectangle.right
end
end
class Canvas
attr_reader :width, :height
def initialize(width, height)
@width = width
@height = height
@canvas = Array.new(@width) {Array.new(@height,false)}
end
def set_pixel(x, y)
@canvas[x][y] = true
end
def pixel_at?(x, y)
@canvas[x][y]
end
def draw(figure)
if figure.is_a?(Point)
set_pixel(figure.x, figure.y)
elsif figure.is_a?(Line)
draw_line(figure)
else
draw_rectangle(figure)
end
end
def draw_line(line)
if line.from.x == line.to.x or line.from.y == line.to.y
(line.from.x..line.to.x).each { |i| set_pixel(i, line.from.y)}
(line.from.y..line.to.y).each { |k| set_pixel(line.from.x, k)}
else
#bresenham(line)
end
end
def draw_rectangle(rectangle)
draw_line(Line.new(rectangle.top_left, rectangle.top_right))
draw_line(Line.new(rectangle.top_left, rectangle.bottom_left))
draw_line(Line.new(rectangle.top_right, rectangle.bottom_right))
draw_line(Line.new(rectangle.bottom_left, rectangle.bottom_right))
end
def render_as(renderer)
result = renderer.new(self)
result.draw
end
end
module Renderers
class Ascii
def initialize(canvas)
@canvas = canvas
end
def draw
(0...@canvas.width).each do |x|
(0...@canvas.height).each { |y| render(y, x)}
puts "" unless x == @canvas.width
end
end
def render(x, y)
pixel = @canvas.pixel_at?(x,y) ? '@' : '-'
print pixel
end
end
class Html
- @header = <<-EOM
- <!DOCTYPE html><html><head><title>Rendered Canvas</title><style type="text/css">
+ def header
+ print '<!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">
- EOM
+ }.canvas b {background-color: #333;}</style></head><body><div class="canvas">'
+ end
- @footer = "</div></body></html>"
+ def footer
+ print '</div></body></html>'
+ end
def initialize(canvas)
@canvas = canvas
end
def draw
- print @header
+ header
(0...@canvas.width).each do |x|
(0...@canvas.height).each { |y| render(y, x)}
print "<br>" unless x == @canvas.width
end
- print @footer
+ footer
end
def render(x, y)
pixel = @canvas.pixel_at?(x,y) ? '<b></b>' : '<i></i>'
print pixel
end
end
end
end