Решение на Трета задача от Лилия Любенова

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

Към профила на Лилия Любенова

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 60 успешни тест(а)
  • 9 неуспешни тест(а)

Код

module Graphics
class Canvas
def initialize(width, height)
@width = width
@height = height
@canvas = "-" * width * height
end
def width
@width
end
def height
@height
end
def canvas
@canvas
end
def set_pixel(x,y)
@canvas[y * width + x] = '@'
end
def pixel_at?(x,y)
@canvas[y * width + x] == '@' ? true : false
end
def draw(figure)
figure.rasterize.each {|point| set_pixel point.x, point.y}
end
def render_as(renderer)
renderer.render self
end
end
module Renderers
module Ascii
def self.render(canvas)
canvas.canvas.chars.each_slice(canvas.width).map(&:join).join "\n"
end
end
module Html
def self.render(canvas)
html = "<!DOCTYPE html>
<html>
<head>
<title>Rendered Canvas</title>
<style type=\"text/css\">
.canvas {
font-size: 1px;
line-height: 1px;
}
.canvas * {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 5px;
}
.canvas i {
background-color: #eee;
}
.canvas b {
background-color: #333;
}
</style>
</head>
<body>
<div class=\"canvas\">"
rendered = canvas.canvas.chars.each_slice(canvas.width).map(&:join).join "<br>"
rendered.gsub!(/@/, "<b></b>").gsub!(/-/, "<i></i>")
html << rendered
html << "</div>
</body>
</html>"
end
end
end
class Point
def initialize(x,y)
@x = x
@y = y
end
def x
@x
end
def y
@y
end
def ==(other)
self.x == other.x and self.y == other.y
end
def eql?(other)
self == other
end
def hash
self.x.hash ^ self.y.hash
end
def rasterize
pixels = [self]
end
end
class Line
def initialize(from,to)
@from = from
@to = to
end
def from
if @from.x == @to.x
@from.y <= @to.y ? @from : @to
else
@from.x < @to.x ? @from : @to
end
end
def to
if @from.x == @to.x
@from.y >= @to.y ? @from : @to
else
@from.x > @to.x ? @from : @to
end
end
def ==(other)
self.from == other.from and self.to == other.to
end
def != (other)
not self == other
end
def eql?(other)
self == other
end
def hash
self.from.hash ^ self.to.hash
end
def rasterize
pixels = []
steep = (to.y - from.y).abs > (to.x - from.x).abs
if steep
bresenham(from.y, from.x, to.y, to.x ,steep, pixels)
else
bresenham(from.x, from.y, to.x ,to.y, steep, pixels)
end
pixels
end
private
def bresenham(from_x, from_y, to_x, to_y, steep, pixels)
dx, dy = to_x - from_x, (to_y - from_y).abs
error, step = dx / 2, to_y <=> from_y
from_x.upto to_x do |x|
steep ? (pixels.push Point.new from_y, x) : (pixels.push Point.new x, from_y)
error -= dy
from_y += step and error += dx if error <= 0
end
end
end
class Rectangle
def initialize(start_point, end_point)
@start = start_point
@end = end_point
end
def left
Line.new(@start,@end).from
end
def right
Line.new(@start,@end).to
end
def top_left
y_coordinate = [left.y, right.y].min
Point.new(left.x, y_coordinate)
end
def top_right
y_coordinate = [left.y, right.y].min
Point.new(right.x, y_coordinate)
end
def bottom_left
y_coordinate = [left.y, right.y].max
Point.new(left.x, y_coordinate)
end
def bottom_right
y_coordinate = [left.y, right.y].max
Point.new(right.x, y_coordinate)
end
def ==(other)
self.top_left == other.top_left and self.bottom_right == other.bottom_right
end
def eql?(other)
self == other
end
def hash
self.start.hash ^ self.end.hash
end
def rasterize
pixels = []
pixels << Line.new(top_left,top_right).rasterize
pixels << Line.new(top_left,bottom_left).rasterize
pixels << Line.new(top_right,bottom_right).rasterize
pixels << Line.new(bottom_left,bottom_right).rasterize
pixels.flatten
end
end
end

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

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

Failures:

  1) Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
     Failure/Error: canvas.set_pixel 4, 4
     IndexError:
       index 20 out of string
     # /tmp/d20131223-4637-1pyxoh7/solution.rb:23:in `[]='
     # /tmp/d20131223-4637-1pyxoh7/solution.rb:23:in `set_pixel'
     # /tmp/d20131223-4637-1pyxoh7/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)>'

  2) 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-1pyxoh7/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-1pyxoh7/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)>'

  3) 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:
       @@ -4,7 +4,7 @@
        --@-------
        --@-------
        --@-------
       ---@-------
       +---@------
        ---@------
        ---@------
        ----------
     # /tmp/d20131223-4637-1pyxoh7/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-1pyxoh7/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)>'

  4) 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-1pyxoh7/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-1pyxoh7/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)>'

  5) Graphics Renderers Html returns html
     Failure/Error: rendering = normalize_html canvas.render_as(html)
     NoMethodError:
       undefined method `gsub!' for nil:NilClass
     # /tmp/d20131223-4637-1pyxoh7/solution.rb:75:in `render'
     # /tmp/d20131223-4637-1pyxoh7/solution.rb:35:in `render_as'
     # /tmp/d20131223-4637-1pyxoh7/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)>'

  6) Graphics Renderers Html renders a grid of the size of the canvas
     Failure/Error: rendering = normalize_html canvas.render_as(html)
     NoMethodError:
       undefined method `gsub!' for nil:NilClass
     # /tmp/d20131223-4637-1pyxoh7/solution.rb:75:in `render'
     # /tmp/d20131223-4637-1pyxoh7/solution.rb:35:in `render_as'
     # /tmp/d20131223-4637-1pyxoh7/spec.rb:309:in `html_rendering_of'
     # /tmp/d20131223-4637-1pyxoh7/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)>'

  7) Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
     Failure/Error: a.hash.should eq b.hash
     NoMethodError:
       undefined method `start' for #<Graphics::Rectangle:0xb9cb84dc>
     # /tmp/d20131223-4637-1pyxoh7/solution.rb:220:in `hash'
     # /tmp/d20131223-4637-1pyxoh7/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)>'

  8) 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
     NoMethodError:
       undefined method `start' for #<Graphics::Rectangle:0xb9cb1664>
     # /tmp/d20131223-4637-1pyxoh7/solution.rb:220:in `hash'
     # /tmp/d20131223-4637-1pyxoh7/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)>'

  9) Graphics shapes Rectangle comparison for equality returns a different hash if the rectangles differ
     Failure/Error: a.hash.should_not eq b.hash
     NoMethodError:
       undefined method `start' for #<Graphics::Rectangle:0xb9cb0944>
     # /tmp/d20131223-4637-1pyxoh7/solution.rb:220:in `hash'
     # /tmp/d20131223-4637-1pyxoh7/spec.rb:570: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.08191 seconds
69 examples, 9 failures

Failed examples:

rspec /tmp/d20131223-4637-1pyxoh7/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-1pyxoh7/spec.rb:96 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
rspec /tmp/d20131223-4637-1pyxoh7/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-1pyxoh7/spec.rb:127 # Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
rspec /tmp/d20131223-4637-1pyxoh7/spec.rb:271 # Graphics Renderers Html returns html
rspec /tmp/d20131223-4637-1pyxoh7/spec.rb:280 # Graphics Renderers Html renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-1pyxoh7/spec.rb:552 # Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
rspec /tmp/d20131223-4637-1pyxoh7/spec.rb:559 # Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-1pyxoh7/spec.rb:566 # Graphics shapes Rectangle comparison for equality returns a different hash if the rectangles differ

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

Лилия обнови решението на 22.12.2013 18:56 (преди над 10 години)

+module Graphics
+
+ class Canvas
+ def initialize(width, height)
+ @width = width
+ @height = height
+ @canvas = "-" * width * height
+ end
+
+ def width
+ @width
+ end
+
+ def height
+ @height
+ end
+
+ def canvas
+ @canvas
+ end
+
+ def set_pixel(x,y)
+ @canvas[y * width + x] = '@'
+ end
+
+ def pixel_at?(x,y)
+ @canvas[y * width + x] == '@' ? true : false
+ end
+
+ def draw(figure)
+ figure.rasterize.each {|point| set_pixel point.x, point.y}
+ end
+
+ def render_as(renderer)
+ renderer.render self
+ end
+ end
+
+ module Renderers
+
+ module Ascii
+ def self.render(canvas)
+ canvas.canvas.chars.each_slice(canvas.width).map(&:join).join "\n"
+ end
+ end
+
+ module Html
+ def self.render(canvas)
+ html = "<!DOCTYPE html>
+ <html>
+ <head>
+ <title>Rendered Canvas</title>
+ <style type=\"text/css\">
+ .canvas {
+ font-size: 1px;
+ line-height: 1px;
+ }
+ .canvas * {
+ display: inline-block;
+ width: 10px;
+ height: 10px;
+ border-radius: 5px;
+ }
+ .canvas i {
+ background-color: #eee;
+ }
+ .canvas b {
+ background-color: #333;
+ }
+ </style>
+ </head>
+ <body>
+ <div class=\"canvas\">"
+ rendered = canvas.canvas.chars.each_slice(canvas.width).map(&:join).join "<br>"
+ rendered.gsub!(/@/, "<b></b>").gsub!(/-/, "<i></i>")
+ html << rendered
+ html << "</div>
+ </body>
+ </html>"
+ end
+ end
+ end
+
+ class Point
+ def initialize(x,y)
+ @x = x
+ @y = y
+ end
+
+ def x
+ @x
+ end
+
+ def y
+ @y
+ end
+
+ def ==(other)
+ self.x == other.x and self.y == other.y
+ end
+
+ def eql?(other)
+ self == other
+ end
+
+ def hash
+ self.x.hash ^ self.y.hash
+ end
+
+ def rasterize
+ pixels = [self]
+ end
+ end
+
+ class Line
+ def initialize(from,to)
+ @from = from
+ @to = to
+ end
+
+ def from
+ if @from.x == @to.x
+ @from.y <= @to.y ? @from : @to
+ else
+ @from.x < @to.x ? @from : @to
+ end
+ end
+
+ def to
+ if @from.x == @to.x
+ @from.y >= @to.y ? @from : @to
+ else
+ @from.x > @to.x ? @from : @to
+ end
+ end
+
+ def ==(other)
+ self.from == other.from and self.to == other.to
+ end
+
+ def != (other)
+ not self == other
+ end
+
+ def eql?(other)
+ self == other
+ end
+
+ def hash
+ self.from.hash ^ self.to.hash
+ end
+
+ def rasterize
+ pixels = []
+ steep = (to.y - from.y).abs > (to.x - from.x).abs
+ if steep
+ bresenham(from.y, from.x, to.y, to.x ,steep, pixels)
+ else
+ bresenham(from.x, from.y, to.x ,to.y, steep, pixels)
+ end
+ pixels
+ end
+
+ private
+ def bresenham(from_x, from_y, to_x, to_y, steep, pixels)
+ dx, dy = to_x - from_x, (to_y - from_y).abs
+ error, step = dx / 2, to_y <=> from_y
+ from_x.upto to_x do |x|
+ steep ? (pixels.push Point.new from_y, x) : (pixels.push Point.new x, from_y)
+ error -= dy
+ from_y += step and error += dx if error <= 0
+ end
+ end
+
+ end
+
+ class Rectangle
+ def initialize(start_point, end_point)
+ @start = start_point
+ @end = end_point
+ end
+
+ def left
+ Line.new(@start,@end).from
+ end
+
+ def right
+ Line.new(@start,@end).to
+ end
+
+ def top_left
+ y_coordinate = [left.y, right.y].min
+ Point.new(left.x, y_coordinate)
+ end
+
+ def top_right
+ y_coordinate = [left.y, right.y].min
+ Point.new(right.x, y_coordinate)
+ end
+
+ def bottom_left
+ y_coordinate = [left.y, right.y].max
+ Point.new(left.x, y_coordinate)
+ end
+
+ def bottom_right
+ y_coordinate = [left.y, right.y].max
+ Point.new(right.x, y_coordinate)
+ end
+
+ def ==(other)
+ self.top_left == other.top_left and self.bottom_right == other.bottom_right
+ end
+
+ def eql?(other)
+ self == other
+ end
+
+ def hash
+ self.start.hash ^ self.end.hash
+ end
+
+ def rasterize
+ pixels = []
+ pixels << Line.new(top_left,top_right).rasterize
+ pixels << Line.new(top_left,bottom_left).rasterize
+ pixels << Line.new(top_right,bottom_right).rasterize
+ pixels << Line.new(bottom_left,bottom_right).rasterize
+ pixels.flatten
+ end
+ end
+end