Решение на Трета задача от Десислав Илиев

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

Към профила на Десислав Илиев

Резултати

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

Код

require 'matrix'
class Matrix
def []=(i, j, x)
@rows[i][j] = x
end
end
class Array
def each_recur(&block)
each do |elem|
if elem.is_a? Array
elem.each_recur &block
else
block.call elem
end
end
end
end
module Graphics
class Canvas
def initialize(rows, columns = rows)
@matrix = Matrix.build(rows, columns) { false }
@width = columns
@height = rows
end
def width
@width
end
def height
@height
end
def pixel_at?(x, y)
@matrix[y,x]
end
def render_as(type)
type.render(@matrix)
end
def draw(figure)
if figure.class.to_s == "Graphics::Point"
set_pixel(figure.x, figure.y)
elsif figure.class.to_s == "Graphics::Line"
draw_line figure.from, figure.to
elsif figure.class.to_s == "Graphics::Rectangle"
draw_rectangle figure
end
end
def draw_line(start_point, end_point)
x, y, z, t = start_point.x, start_point.y, end_point.x, end_point.y
steep = (t - y).abs > (z - x).abs
if steep
x, y = y, x
z, t = t, z
end
if x > z then deltas(z, t, x, y, steep) else deltas(x, y, z, t,steep) end
end
def deltas(x, y, z, t, steep)
delta_x = z - x
delta_y = (t - y).abs
error = delta_x / 2
y_step = y < t ? 1 : -1
increment_y = y
action(x, y, z, t, delta_x, delta_y, error, increment_y, y_step, steep)
end
def action(x, y, z, t, delta_x, delta_y, error, increment_y, y_step, steep)
x.upto(z) do |x|
steep ? set_pixel(increment_y, x) : set_pixel(x, increment_y)
error -= delta_y
if error <= 0
increment_y += y_step
error += delta_x
end
end
end
def draw_rectangle(rectangle)
draw_line rectangle.top_left, rectangle.bottom_left
draw_line rectangle.top_left, rectangle.top_right
draw_line rectangle.top_right, rectangle.bottom_right
draw_line rectangle.bottom_left, rectangle.bottom_right
end
private
def set_pixel(x, y)
@matrix[y,x] = true
end
end
class Point
def initialize(x, y)
@x = x
@y = y
end
attr_reader :x
attr_reader :y
def ==(point)
if @x == point.x and @y == point.y then true else false end
end
def eql?(point)
if @x == point.x and @y == point.y then true else false end
end
def hash
[@x, @y].hash
end
end
class Line
def initialize(from, to)
@from = from
@to = to
if from.x > to.x or from.y > to.y then @from, @to = @to, @from end
end
attr_reader :from
attr_reader :to
def ==(line)
if @from == line.from and @to == line.to then true else false end
end
def eql?(line)
if @from == line.from and @to == line.to then true else false end
end
def hash
[@from, @to].hash
end
end
class Rectangle
def initialize(left, right)
@left = left
@right = right
if left.y > right.y
@left, @right = @right, @left
elsif left.x > right.x and left.y == right.y
@left, @right = @right, @left
end
end
attr_reader :left
attr_reader :right
def top_left
if @left.x <= right.x
@left
else
Point.new right.x, left.y
end
end
def bottom_left
if @left.x >= right.x
@left
else
Point.new right.x, left.y
end
end
def top_right
if @left.x >= right.x
@right
else
Point.new left.x, right.y
end
end
def bottom_right
if @left.x <= right.x
@right
else
Point.new left.x, right.y
end
end
def ==(rectangle)
if self.top_left==rectangle.top_left and self.bottom_right==rectangle.bottom_right
true
else
false
end
end
def eql?(rectangle)
if self.top_left==rectangle.top_left and self.bottom_right==rectangle.bottom_right
true
else
false
end
end
def hash
[self.top_left,self.top_right,self.bottom_left,self.bottom_right].hash
end
end
module Renderers
class Html
def Html.render(matrix)
new=matrix.collect{|item|if item==true then item="<b></b>"else item="<i></i>"end}
"<!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\">
#{new.to_a.map! {|row| row.join('')}.join("<br>\n")}
</div>
</body>
</html>"
end
end
class Ascii
def Ascii.render(matrix)
parsed_matrix=matrix.collect{|item|if item==true then item="@" else item="-" end}
parsed_matrix.to_a.map {|row| row.join('')}
end
end
end
end

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

...F..FFF.FFFFFFFFFFFFFF.FFF...........F................F.........F.F

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-12hfd38/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 responds to calls to set_pixel with two arguments
     Failure/Error: canvas.set_pixel 1, 2
     NoMethodError:
       private method `set_pixel' called for #<Graphics::Canvas:0xba2275bc>
     # /tmp/d20131223-4637-12hfd38/spec.rb:34: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.set_pixel(3, 5)
     NoMethodError:
       private method `set_pixel' called for #<Graphics::Canvas:0xba225fc8>
     # /tmp/d20131223-4637-12hfd38/spec.rb:39: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 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: ["@@@@@@@@@@@@@@@", "@-------------@", "@-@@@@@@@@@@@-@", "@-@---------@-@", "@-@------@@-@-@", "@-@---@@@---@-@", "@-@-@@------@-@", "@-@---------@-@", "@-@-@@@@----@-@", "@-@-@-------@-@", "@-@---------@-@", "@-@---------@-@", "@-@@@@@@@@@@@-@", "@-------------@", "@@@@@@@@@@@@@@@"]
       
       (compared using ==)
       
       Diff:
     # /tmp/d20131223-4637-12hfd38/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-12hfd38/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)>'

  5) Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
     Failure/Error: canvas.set_pixel 0, 0
     NoMethodError:
       private method `set_pixel' called for #<Graphics::Canvas:0xba214214>
     # /tmp/d20131223-4637-12hfd38/spec.rb:53: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: ["---", "---@@@@", "---", "---", "---", "---", "---", "---"]
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,9 @@
       ---------
       ----@@@@-
       ---------
       +---
       +---@@@@
       +---
       +---
       +---
       +---
       +---
       +---
     # /tmp/d20131223-4637-12hfd38/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-12hfd38/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 vertical lines
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "-@------\n-@------\n-@------\n-@------\n-@------\n-@------\n-@------\n--------"
            got: ["-@------", "-@------", "-@------", "-@------", "-@------", "-@------", "-@------", "--------"]
       
       (compared using ==)
       
       Diff:
     # /tmp/d20131223-4637-12hfd38/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-12hfd38/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)>'

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

  9) 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: ["----------", "-@--------", "-@--------", "--@-------", "--@-------", "--@-------", "---@------", "---@------", "---@------", "----------"]
       
       (compared using ==)
       
       Diff:
       @@ -4,7 +4,7 @@
        --@-------
        --@-------
        --@-------
       ---@-------
       +---@------
        ---@------
        ---@------
        ----------
     # /tmp/d20131223-4637-12hfd38/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-12hfd38/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)>'

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

  11) 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: ["---", "-@-", "---"]
       
       (compared using ==)
       
       Diff:
     # /tmp/d20131223-4637-12hfd38/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-12hfd38/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)>'

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

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

  14) 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: ["---", "-@@@@@@@@", "---", "---", "---", "---", "---", "---", "---", "---"]
       
       (compared using ==)
       
       Diff:
       @@ -1,4 +1,11 @@
       -----------
       --@@@@@@@@-
       -----------
       +---
       +-@@@@@@@@
       +---
       +---
       +---
       +---
       +---
       +---
       +---
       +---
     # /tmp/d20131223-4637-12hfd38/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-12hfd38/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)>'

  15) 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: ["---", "-@-", "---"]
       
       (compared using ==)
       
       Diff:
     # /tmp/d20131223-4637-12hfd38/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-12hfd38/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)>'

  16) 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 ["---", "---", "---", "---"]:Array
     # /tmp/d20131223-4637-12hfd38/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)>'

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

  18) Graphics Renderers Ascii renders simple canvases
     Failure/Error: canvas.set_pixel 0, 0
     NoMethodError:
       private method `set_pixel' called for #<Graphics::Canvas:0xba187e54>
     # /tmp/d20131223-4637-12hfd38/spec.rb:253: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 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-12hfd38/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)>'

  20) Graphics Renderers Html renders simple canvases
     Failure/Error: canvas.set_pixel 1, 1
     NoMethodError:
       private method `set_pixel' called for #<Graphics::Canvas:0xba0f804c>
     # /tmp/d20131223-4637-12hfd38/spec.rb:288: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)>'

  21) Graphics Renderers Html returns the same rendering when called twice
     Failure/Error: canvas.set_pixel 1, 1
     NoMethodError:
       private method `set_pixel' called for #<Graphics::Canvas:0xba0eb158>
     # /tmp/d20131223-4637-12hfd38/spec.rb:299: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)>'

  22) Graphics shapes Line initialization returns its from and to points via getters
     Failure/Error: line.from.x.should eq 1
       
       expected: 1
            got: 25
       
       (compared using ==)
     # /tmp/d20131223-4637-12hfd38/spec.rb:383:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  23) 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-12hfd38/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)>'

  24) Graphics shapes Rectangle corners top right
     Failure/Error: rect.top_right.x.should eq 5
       
       expected: 5
            got: 1
       
       (compared using ==)
     # /tmp/d20131223-4637-12hfd38/spec.rb:583: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 bottom left
     Failure/Error: rect.bottom_left.x.should eq 1
       
       expected: 1
            got: 5
       
       (compared using ==)
     # /tmp/d20131223-4637-12hfd38/spec.rb:595: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.0963 seconds
69 examples, 25 failures

Failed examples:

rspec /tmp/d20131223-4637-12hfd38/spec.rb:16 # Graphics Canvas exposes its width and height via getters
rspec /tmp/d20131223-4637-12hfd38/spec.rb:33 # Graphics Canvas responds to calls to set_pixel with two arguments
rspec /tmp/d20131223-4637-12hfd38/spec.rb:37 # Graphics Canvas allows setting a pixel at a given x and y
rspec /tmp/d20131223-4637-12hfd38/spec.rb:203 # Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
rspec /tmp/d20131223-4637-12hfd38/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-12hfd38/spec.rb:69 # Graphics Canvas drawing of shapes and rasterization of lines works with simple horizontal lines
rspec /tmp/d20131223-4637-12hfd38/spec.rb:80 # Graphics Canvas drawing of shapes and rasterization of lines works with vertical lines
rspec /tmp/d20131223-4637-12hfd38/spec.rb:96 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
rspec /tmp/d20131223-4637-12hfd38/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-12hfd38/spec.rb:127 # Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
rspec /tmp/d20131223-4637-12hfd38/spec.rb:141 # Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
rspec /tmp/d20131223-4637-12hfd38/spec.rb:154 # Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
rspec /tmp/d20131223-4637-12hfd38/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-12hfd38/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-12hfd38/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-12hfd38/spec.rb:237 # Graphics Renderers Ascii renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-12hfd38/spec.rb:244 # Graphics Renderers Ascii renders blank canvases
rspec /tmp/d20131223-4637-12hfd38/spec.rb:252 # Graphics Renderers Ascii renders simple canvases
rspec /tmp/d20131223-4637-12hfd38/spec.rb:280 # Graphics Renderers Html renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-12hfd38/spec.rb:287 # Graphics Renderers Html renders simple canvases
rspec /tmp/d20131223-4637-12hfd38/spec.rb:298 # Graphics Renderers Html returns the same rendering when called twice
rspec /tmp/d20131223-4637-12hfd38/spec.rb:382 # Graphics shapes Line initialization returns its from and to points via getters
rspec /tmp/d20131223-4637-12hfd38/spec.rb:504 # Graphics shapes Rectangle initialization puts the leftmost point in its left field
rspec /tmp/d20131223-4637-12hfd38/spec.rb:581 # Graphics shapes Rectangle corners top right
rspec /tmp/d20131223-4637-12hfd38/spec.rb:593 # Graphics shapes Rectangle corners bottom left

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

Десислав обнови решението на 22.12.2013 22:51 (преди почти 11 години)

+require 'matrix'
+
+class Matrix
+ def []=(i, j, x)
+ @rows[i][j] = x
+ end
+end
+
+class Array
+ def each_recur(&block)
+ each do |elem|
+ if elem.is_a? Array
+ elem.each_recur &block
+ else
+ block.call elem
+ end
+ end
+ end
+end
+
+module Graphics
+ class Canvas
+ def initialize(rows, columns = rows)
+ @matrix = Matrix.build(rows, columns) { false }
+ @width = columns
+ @height = rows
+ end
+ def width
+ @width
+ end
+ def height
+ @height
+ end
+ def pixel_at?(x, y)
+ @matrix[y,x]
+ end
+ def render_as(type)
+ type.render(@matrix)
+ end
+ def draw(figure)
+ if figure.class.to_s == "Graphics::Point"
+ set_pixel(figure.x, figure.y)
+ elsif figure.class.to_s == "Graphics::Line"
+ draw_line figure.from, figure.to
+ elsif figure.class.to_s == "Graphics::Rectangle"
+ draw_rectangle figure
+ end
+ end
+ def draw_line(start_point, end_point)
+ x, y, z, t = start_point.x, start_point.y, end_point.x, end_point.y
+ steep = (t - y).abs > (z - x).abs
+ if steep
+ x, y = y, x
+ z, t = t, z
+ end
+ if x > z then deltas(z, t, x, y, steep) else deltas(x, y, z, t,steep) end
+ end
+ def deltas(x, y, z, t, steep)
+ delta_x = z - x
+ delta_y = (t - y).abs
+ error = delta_x / 2
+ y_step = y < t ? 1 : -1
+ increment_y = y
+ action(x, y, z, t, delta_x, delta_y, error, increment_y, y_step, steep)
+ end
+ def action(x, y, z, t, delta_x, delta_y, error, increment_y, y_step, steep)
+ x.upto(z) do |x|
+ steep ? set_pixel(increment_y, x) : set_pixel(x, increment_y)
+ error -= delta_y
+ if error <= 0
+ increment_y += y_step
+ error += delta_x
+ end
+ end
+ end
+ def draw_rectangle(rectangle)
+ draw_line rectangle.top_left, rectangle.bottom_left
+ draw_line rectangle.top_left, rectangle.top_right
+ draw_line rectangle.top_right, rectangle.bottom_right
+ draw_line rectangle.bottom_left, rectangle.bottom_right
+ end
+ private
+ def set_pixel(x, y)
+ @matrix[y,x] = true
+ end
+ end
+ class Point
+ def initialize(x, y)
+ @x = x
+ @y = y
+ end
+ attr_reader :x
+ attr_reader :y
+ def ==(point)
+ if @x == point.x and @y == point.y then true else false end
+ end
+ def eql?(point)
+ if @x == point.x and @y == point.y then true else false end
+ end
+ def hash
+ [@x, @y].hash
+ end
+ end
+ class Line
+ def initialize(from, to)
+ @from = from
+ @to = to
+ if from.x > to.x or from.y > to.y then @from, @to = @to, @from end
+ end
+ attr_reader :from
+ attr_reader :to
+ def ==(line)
+ if @from == line.from and @to == line.to then true else false end
+ end
+ def eql?(line)
+ if @from == line.from and @to == line.to then true else false end
+ end
+ def hash
+ [@from, @to].hash
+ end
+ end
+ class Rectangle
+ def initialize(left, right)
+ @left = left
+ @right = right
+ if left.y > right.y
+ @left, @right = @right, @left
+ elsif left.x > right.x and left.y == right.y
+ @left, @right = @right, @left
+ end
+ end
+ attr_reader :left
+ attr_reader :right
+ def top_left
+ if @left.x <= right.x
+ @left
+ else
+ Point.new right.x, left.y
+ end
+ end
+ def bottom_left
+ if @left.x >= right.x
+ @left
+ else
+ Point.new right.x, left.y
+ end
+ end
+ def top_right
+ if @left.x >= right.x
+ @right
+ else
+ Point.new left.x, right.y
+ end
+ end
+ def bottom_right
+ if @left.x <= right.x
+ @right
+ else
+ Point.new left.x, right.y
+ end
+ end
+ def ==(rectangle)
+ if self.top_left==rectangle.top_left and self.bottom_right==rectangle.bottom_right
+ true
+ else
+ false
+ end
+ end
+ def eql?(rectangle)
+ if self.top_left==rectangle.top_left and self.bottom_right==rectangle.bottom_right
+ true
+ else
+ false
+ end
+ end
+ def hash
+ [self.top_left,self.top_right,self.bottom_left,self.bottom_right].hash
+ end
+ end
+ module Renderers
+ class Html
+ def Html.render(matrix)
+ new=matrix.collect{|item|if item==true then item="<b></b>"else item="<i></i>"end}
+ "<!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\">
+ #{new.to_a.map! {|row| row.join('')}.join("<br>\n")}
+ </div>
+ </body>
+ </html>"
+ end
+ end
+ class Ascii
+ def Ascii.render(matrix)
+ parsed_matrix=matrix.collect{|item|if item==true then item="@" else item="-" end}
+ parsed_matrix.to_a.map {|row| row.join('')}
+ end
+ end
+ end
+end