Решение на Трета задача от Сашо Михайлов

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

Към профила на Сашо Михайлов

Резултати

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

Код

module Graphics
class Point
def initialize(x, y)
@x = x
@y = y
end
def draw(canvas)
canvas.set_pixel(@x,@y)
end
def ==(other)
return true if (@x == other.x && @y == other.y)
return false
end
alias eql ==
attr_reader :x,:y
end
class Line
def help_initialize(arr,x,y,st_x,st_y,err)
line=lambda{|x,y,st_x,st_y,err,arr|x,y=(arr[2]-arr[0]).abs,-(arr[3]-arr[1]).abs
st_x,st_y,err=arr[0]<arr[2] ? 1: -1,arr[1]<arr[3] ? 1: -1,x+y
err,arr[0]=err+y,arr[0]+st_x if 2*err>=y
err,arr[1] = err+x,arr[1]+st_y if 2*err <= x}
begin
line.call(x,y,st_x,st_y,err,arr)
@arr<<Point.new(arr[0],arr[1])end until(arr[0]==arr[2]&&arr[1]==arr[3])
end
def initialize(start,stop)
@arr,limits,devx,devy,step_x,step_y,err = [],[],1,1,1,1,1
if (start.x == stop.x && start.y == stop.y)
@arr << Point.new(start.x,start.y)
else
limits << start.x << start.y << stop.x << stop.y
@arr << Point.new(limits[0],limits[1])
help_initialize(limits,devx,devy,step_x,step_y,err)
end
end
def from
@arr.first
end
def to
@arr.last
end
def draw(canvas)
@arr.each{|point| canvas.set_pixel(point.x,point.y)}
end
def ==(other)
self.from == self.to
end
alias eql ==
end
class Rectangle
def sequence(first,second)
points = []
if (first.y > second.y)
points << Point.new(first.x,second.y)<<second<<Point.new(second.x,first.y)<<first
else
points << first << Point.new(second.x,first.y) << second
points << Point.new(first.x,second.y)
end
points
end
def initialize(first,second)
@points = sequence(first,second)
end
def draw(canvas)
canvas.draw(Line.new(@points[0],@points[1]))
canvas.draw(Line.new(@points[1],@points[2]))
canvas.draw(Line.new(@points[2],@points[3]))
canvas.draw(Line.new(@points[3],@points[0]))
end
def left
@points[0]
end
def right
@points[2]
end
def ==(other)
return self.right == self.left
end
alias eql ==
end
module Renderers
class Ascii
def self.function(text,width,height,canvas)
width.times { |n| text +='@'if canvas.pixel_at?(n,height)
text +='-'if not canvas.pixel_at?(n,height)}
text
end
def self.draw(canvas)
@text,text = '',''
line=lambda{|width,height| @text += function(text,width,height,canvas)}
canvas.height.times{ |i| line.call canvas.width,i
@text += "\n"}
@text
end
end
class Html
def self.function(text,width,height,canvas)
width.times { |n| text +='<b></b>'if canvas.pixel_at?(n,height)
text +='<i></i>'if not canvas.pixel_at?(n,height)}
text
end
def self.draw(canvas)
text, @text = '', "<!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\">"
line=lambda{|width,height| @text += function(text,width,height,canvas)}
canvas.height.times{ |i| line.call canvas.width,i
@text += "<br>\n"}
@text+= "</div>
</body>
</html>"
end
end
end
class Canvas
def initialize(width, height)
@array = Array.new(height) { Array.new(width) }
0.upto(height-1){|column| 0.upto(width-1){|line| @array[column][line] = 0} }
end
def set_pixel(x, y)
input_pixel = lambda {|column,line| @array[column][line]=1 if (column==y && line==x)}
0.upto(height-1){|column| width.times{|line| input_pixel.call(column,line)} }
end
def pixel_at?(x, y)
return true if @array[y][x] == 1
return false
end
def width
@array[0].length
end
def height
length = 0
@array.each{|index| length+=1}
length
end
def draw (figure)
figure.draw(self)
end
def render_as(ascii)
ascii.draw(self)
end
end
end

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

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

Failures:

  1) Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "@@@@@@@@@@@@@@@\n@-------------@\n@-@@@@@@@@@@@-@\n@-@---------@-@\n@-@------@@-@-@\n@-@---@@@---@-@\n@-@-@@------@-@\n@-@---------@-@\n@-@-@@@@----@-@\n@-@-@-------@-@\n@-@---------@-@\n@-@---------@-@\n@-@@@@@@@@@@@-@\n@-------------@\n@@@@@@@@@@@@@@@"
            got: "@@@@@@@@@@@@@@@\n@-------------@\n@-@@@@@@@@@@@-@\n@-@---------@-@\n@-@----@@@@-@-@\n@-@--@@-----@-@\n@-@-@-------@-@\n@-@---------@-@\n@-@-@@@@----@-@\n@-@-@-------@-@\n@-@---------@-@\n@-@---------@-@\n@-@@@@@@@@@@@-@\n@-------------@\n@@@@@@@@@@@@@@@\n"
       
       (compared using ==)
       
       Diff:
       @@ -2,9 +2,9 @@
        @-------------@
        @-@@@@@@@@@@@-@
        @-@---------@-@
       -@-@------@@-@-@
       -@-@---@@@---@-@
       -@-@-@@------@-@
       +@-@----@@@@-@-@
       +@-@--@@-----@-@
       +@-@-@-------@-@
        @-@---------@-@
        @-@-@@@@----@-@
        @-@-@-------@-@
     # /tmp/d20131223-4637-b8vg8l/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-b8vg8l/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: ascii.should eq rendering(expected)
       
       expected: "@---\n@---\n-@@-\n----"
            got: "@---\n@---\n-@@-\n----\n"
       
       (compared using ==)
       
       Diff:
     # /tmp/d20131223-4637-b8vg8l/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-b8vg8l/spec.rb:59:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Graphics Canvas drawing of shapes and rasterization of lines works with simple horizontal lines
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "--------\n---@@@@-\n--------"
            got: "--------\n---@@@@-\n--------\n"
       
       (compared using ==)
       
       Diff:
     # /tmp/d20131223-4637-b8vg8l/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-b8vg8l/spec.rb:73:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Graphics Canvas drawing of shapes and rasterization of lines works with vertical lines
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "-@------\n-@------\n-@------\n-@------\n-@------\n-@------\n-@------\n--------"
            got: "-@------\n-@------\n-@------\n-@------\n-@------\n-@------\n-@------\n--------\n"
       
       (compared using ==)
       
       Diff:
     # /tmp/d20131223-4637-b8vg8l/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-b8vg8l/spec.rb:84:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "----------\n-@@-------\n---@@@@---\n-------@@-\n----------"
            got: "----------\n-@--------\n--@@@-----\n-----@@@@-\n----------\n"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,6 @@
        ----------
       --@@-------
       ----@@@@---
       --------@@-
       +-@--------
       +--@@@-----
       +-----@@@@-
        ----------
     # /tmp/d20131223-4637-b8vg8l/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-b8vg8l/spec.rb:100:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) Graphics Canvas drawing of shapes and rasterization of lines works with lines with a significant slope, with swapped ends
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "----------\n-@--------\n-@--------\n--@-------\n--@-------\n--@-------\n--@-------\n---@------\n---@------\n----------"
            got: "----------\n-@--------\n-@--------\n--@-------\n--@-------\n---@------\n---@------\n---@------\n---@------\n----------\n"
       
       (compared using ==)
       
       Diff:
       @@ -3,8 +3,8 @@
        -@--------
        --@-------
        --@-------
       ---@-------
       ---@-------
       +---@------
       +---@------
        ---@------
        ---@------
        ----------
     # /tmp/d20131223-4637-b8vg8l/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-b8vg8l/spec.rb:113:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "-@--------\n-@@-------\n-@-@@@@---\n-@-----@@-\n----------"
            got: "-@--------\n-@--------\n-@@@@-----\n-@---@@@@-\n----------\n"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,6 @@
        -@--------
       --@@-------
       --@-@@@@---
       --@-----@@-
       +-@--------
       +-@@@@-----
       +-@---@@@@-
        ----------
     # /tmp/d20131223-4637-b8vg8l/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-b8vg8l/spec.rb:132:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "---\n-@-\n---"
            got: "---\n-@-\n---\n"
       
       (compared using ==)
       
       Diff:
     # /tmp/d20131223-4637-b8vg8l/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-b8vg8l/spec.rb:145:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  9) Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "----------\n-@@@@@@@@-\n-@------@-\n-@@@@@@@@-\n----------"
            got: "----------\n-@@@@@@@@-\n-@------@-\n-@@@@@@@@-\n----------\n"
       
       (compared using ==)
       
       Diff:
     # /tmp/d20131223-4637-b8vg8l/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-b8vg8l/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"
       
       (compared using ==)
       
       Diff:
     # /tmp/d20131223-4637-b8vg8l/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-b8vg8l/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"
       
       (compared using ==)
       
       Diff:
     # /tmp/d20131223-4637-b8vg8l/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-b8vg8l/spec.rb:184:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  12) Graphics Canvas drawing of shapes and rasterization of rectangles works with rects with a zero width and height as a single point
     Failure/Error: ascii.should eq rendering(expected)
       
       expected: "---\n-@-\n---"
            got: "---\n-@-\n---\n"
       
       (compared using ==)
       
       Diff:
     # /tmp/d20131223-4637-b8vg8l/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-b8vg8l/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 blank canvases
     Failure/Error: canvas.render_as(ascii).should eq rendering('
       
       expected: "----\n----\n----"
            got: "----\n----\n----\n"
       
       (compared using ==)
       
       Diff:
     # /tmp/d20131223-4637-b8vg8l/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:
     # /tmp/d20131223-4637-b8vg8l/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 simple canvases
     Failure/Error: html_rendering_of(canvas).should eq [
       
       expected: "<i></i><i></i><i></i><i></i><br><i></i><b></b><i></i><i></i><br><i></i><b></b><i></i><i></i>"
            got: "<i></i><i></i><i></i><i></i><br><i></i><b></b><i></i><i></i><br><i></i><b></b><i></i><i></i><br>"
       
       (compared using ==)
     # /tmp/d20131223-4637-b8vg8l/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)>'

  16) Graphics shapes Point comparison for equality works for eql? as well
     Failure/Error: a1.should eql a2
       
       expected: #<Graphics::Point:0xb9a6f798 @x=4, @y=5>
            got: #<Graphics::Point:0xb9a6f824 @x=4, @y=5>
       
       (compared using eql?)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Graphics::Point:0xb9a6f798 @x=4, @y=5>
       +#<Graphics::Point:0xb9a6f824 @x=4, @y=5>
     # /tmp/d20131223-4637-b8vg8l/spec.rb:356:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  17) Graphics shapes Point comparison for equality returns the same hash for the same points
     Failure/Error: a1.hash.should eq a2.hash
       
       expected: -1036243150
            got: 625270446
       
       (compared using ==)
     # /tmp/d20131223-4637-b8vg8l/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-b8vg8l/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-b8vg8l/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-b8vg8l/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-b8vg8l/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 true if line ends are the same
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-b8vg8l/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)>'

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

  24) Graphics shapes Line comparison for equality is true if line is vertical and the bottom is given first
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-b8vg8l/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)>'

  25) Graphics shapes Line comparison for equality works with eql? as well
     Failure/Error: a.should eql b
       
       expected: #<Graphics::Line:0xb9a21480 @arr=[#<Graphics::Point:0xb9a21444 @x=1, @y=1>, #<Graphics::Point:0xb9a213e0 @x=2, @y=2>, #<Graphics::Point:0xb9a213a4 @x=3, @y=3>, #<Graphics::Point:0xb9a21354 @x=4, @y=4>, #<Graphics::Point:0xb9a212f0 @x=5, @y=5>, #<Graphics::Point:0xb9a212a0 @x=6, @y=6>, #<Graphics::Point:0xb9a21264 @x=7, @y=7>, #<Graphics::Point:0xb9a21228 @x=7, @y=8>, #<Graphics::Point:0xb9a211ec @x=8, @y=9>, #<Graphics::Point:0xb9a211c4 @x=8, @y=10>, #<Graphics::Point:0xb9a21174 @x=9, @y=11>, #<Graphics::Point:0xb9a2114c @x=9, @y=12>, #<Graphics::Point:0xb9a210ac @x=10, @y=13>, #<Graphics::Point:0xb9a21084 @x=10, @y=14>]>
            got: #<Graphics::Line:0xb9a218e0 @arr=[#<Graphics::Point:0xb9a2187c @x=1, @y=1>, #<Graphics::Point:0xb9a21818 @x=2, @y=2>, #<Graphics::Point:0xb9a217b4 @x=3, @y=3>, #<Graphics::Point:0xb9a21778 @x=4, @y=4>, #<Graphics::Point:0xb9a2173c @x=5, @y=5>, #<Graphics::Point:0xb9a21700 @x=6, @y=6>, #<Graphics::Point:0xb9a216c4 @x=7, @y=7>, #<Graphics::Point:0xb9a2169c @x=7, @y=8>, #<Graphics::Point:0xb9a21660 @x=8, @y=9>, #<Graphics::Point:0xb9a21624 @x=8, @y=10>, #<Graphics::Point:0xb9a215e8 @x=9, @y=11>, #<Graphics::Point:0xb9a21584 @x=9, @y=12>, #<Graphics::Point:0xb9a21534 @x=10, @y=13>, #<Graphics::Point:0xb9a2150c @x=10, @y=14>]>
       
       (compared using eql?)
       
       Diff:
       
       @@ -1,17 +1,17 @@
       -#<Graphics::Line:0xb9a21480
       +#<Graphics::Line:0xb9a218e0
         @arr=
       -  [#<Graphics::Point:0xb9a21444 @x=1, @y=1>,
       -   #<Graphics::Point:0xb9a213e0 @x=2, @y=2>,
       -   #<Graphics::Point:0xb9a213a4 @x=3, @y=3>,
       -   #<Graphics::Point:0xb9a21354 @x=4, @y=4>,
       -   #<Graphics::Point:0xb9a212f0 @x=5, @y=5>,
       -   #<Graphics::Point:0xb9a212a0 @x=6, @y=6>,
       -   #<Graphics::Point:0xb9a21264 @x=7, @y=7>,
       -   #<Graphics::Point:0xb9a21228 @x=7, @y=8>,
       -   #<Graphics::Point:0xb9a211ec @x=8, @y=9>,
       -   #<Graphics::Point:0xb9a211c4 @x=8, @y=10>,
       -   #<Graphics::Point:0xb9a21174 @x=9, @y=11>,
       -   #<Graphics::Point:0xb9a2114c @x=9, @y=12>,
       -   #<Graphics::Point:0xb9a210ac @x=10, @y=13>,
       -   #<Graphics::Point:0xb9a21084 @x=10, @y=14>]>
       +  [#<Graphics::Point:0xb9a2187c @x=1, @y=1>,
       +   #<Graphics::Point:0xb9a21818 @x=2, @y=2>,
       +   #<Graphics::Point:0xb9a217b4 @x=3, @y=3>,
       +   #<Graphics::Point:0xb9a21778 @x=4, @y=4>,
       +   #<Graphics::Point:0xb9a2173c @x=5, @y=5>,
       +   #<Graphics::Point:0xb9a21700 @x=6, @y=6>,
       +   #<Graphics::Point:0xb9a216c4 @x=7, @y=7>,
       +   #<Graphics::Point:0xb9a2169c @x=7, @y=8>,
       +   #<Graphics::Point:0xb9a21660 @x=8, @y=9>,
       +   #<Graphics::Point:0xb9a21624 @x=8, @y=10>,
       +   #<Graphics::Point:0xb9a215e8 @x=9, @y=11>,
       +   #<Graphics::Point:0xb9a21584 @x=9, @y=12>,
       +   #<Graphics::Point:0xb9a21534 @x=10, @y=13>,
       +   #<Graphics::Point:0xb9a2150c @x=10, @y=14>]>
     # /tmp/d20131223-4637-b8vg8l/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)>'

  26) 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: 1017548530
            got: -332937188
       
       (compared using ==)
     # /tmp/d20131223-4637-b8vg8l/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)>'

  27) 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-b8vg8l/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)>'

  28) Graphics shapes Rectangle comparison for equality is true if rectangle points are the same
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-b8vg8l/spec.rb:526:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  29) Graphics shapes Rectangle comparison for equality is true if rectangle points are the same, even if swapped
     Failure/Error: (a == b).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-b8vg8l/spec.rb:533:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  30) 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-b8vg8l/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)>'

  31) Graphics shapes Rectangle comparison for equality works with eql? as well
     Failure/Error: a.should eql b
       
       expected: #<Graphics::Rectangle:0xb99448b4 @points=[#<Graphics::Point:0xb9944918 @x=1, @y=1>, #<Graphics::Point:0xb994488c @x=10, @y=1>, #<Graphics::Point:0xb99448f0 @x=10, @y=14>, #<Graphics::Point:0xb9944878 @x=1, @y=14>]>
            got: #<Graphics::Rectangle:0xb994497c @points=[#<Graphics::Point:0xb99449e0 @x=1, @y=1>, #<Graphics::Point:0xb9944954 @x=10, @y=1>, #<Graphics::Point:0xb99449b8 @x=10, @y=14>, #<Graphics::Point:0xb9944940 @x=1, @y=14>]>
       
       (compared using eql?)
       
       Diff:
       
       @@ -1,7 +1,7 @@
       -#<Graphics::Rectangle:0xb99448b4
       +#<Graphics::Rectangle:0xb994497c
         @points=
       -  [#<Graphics::Point:0xb9944918 @x=1, @y=1>,
       -   #<Graphics::Point:0xb994488c @x=10, @y=1>,
       -   #<Graphics::Point:0xb99448f0 @x=10, @y=14>,
       -   #<Graphics::Point:0xb9944878 @x=1, @y=14>]>
       +  [#<Graphics::Point:0xb99449e0 @x=1, @y=1>,
       +   #<Graphics::Point:0xb9944954 @x=10, @y=1>,
       +   #<Graphics::Point:0xb99449b8 @x=10, @y=14>,
       +   #<Graphics::Point:0xb9944940 @x=1, @y=14>]>
     # /tmp/d20131223-4637-b8vg8l/spec.rb:548:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  32) 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: 161511618
            got: -146458402
       
       (compared using ==)
     # /tmp/d20131223-4637-b8vg8l/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)>'

  33) 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: 75073768
            got: -518359590
       
       (compared using ==)
     # /tmp/d20131223-4637-b8vg8l/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)>'

  34) Graphics shapes Rectangle corners top left
     Failure/Error: rect.top_left.x.should eq 1
     NoMethodError:
       undefined method `top_left' for #<Graphics::Rectangle:0xb99068e8>
     # /tmp/d20131223-4637-b8vg8l/spec.rb:577: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)>'

  35) Graphics shapes Rectangle corners top right
     Failure/Error: rect.top_right.x.should eq 5
     NoMethodError:
       undefined method `top_right' for #<Graphics::Rectangle:0xb9905178>
     # /tmp/d20131223-4637-b8vg8l/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)>'

  36) Graphics shapes Rectangle corners bottom right
     Failure/Error: rect.bottom_right.x.should eq 5
     NoMethodError:
       undefined method `bottom_right' for #<Graphics::Rectangle:0xb9904624>
     # /tmp/d20131223-4637-b8vg8l/spec.rb:589: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)>'

  37) Graphics shapes Rectangle corners bottom left
     Failure/Error: rect.bottom_left.x.should eq 1
     NoMethodError:
       undefined method `bottom_left' for #<Graphics::Rectangle:0xb990333c>
     # /tmp/d20131223-4637-b8vg8l/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.15065 seconds
69 examples, 37 failures

Failed examples:

rspec /tmp/d20131223-4637-b8vg8l/spec.rb:203 # Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:69 # Graphics Canvas drawing of shapes and rasterization of lines works with simple horizontal lines
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:80 # Graphics Canvas drawing of shapes and rasterization of lines works with vertical lines
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:96 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
rspec /tmp/d20131223-4637-b8vg8l/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-b8vg8l/spec.rb:127 # Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:141 # Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:154 # Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
rspec /tmp/d20131223-4637-b8vg8l/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-b8vg8l/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-b8vg8l/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-b8vg8l/spec.rb:244 # Graphics Renderers Ascii renders blank canvases
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:252 # Graphics Renderers Ascii renders simple canvases
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:287 # Graphics Renderers Html renders simple canvases
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:355 # Graphics shapes Point comparison for equality works for eql? as well
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:360 # Graphics shapes Point comparison for equality returns the same hash for the same points
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:401 # Graphics shapes Line initialization with swapped points puts the leftmost point in the from field
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:406 # Graphics shapes Line initialization with swapped points puts the rightmost point in the to field
rspec /tmp/d20131223-4637-b8vg8l/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-b8vg8l/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-b8vg8l/spec.rb:431 # Graphics shapes Line comparison for equality is true if line ends are the same
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:438 # Graphics shapes Line comparison for equality is true if line ends are the same, even if swapped
rspec /tmp/d20131223-4637-b8vg8l/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-b8vg8l/spec.rb:452 # Graphics shapes Line comparison for equality works with eql? as well
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:461 # Graphics shapes Line comparison for equality returns the same hash if the lines are the same
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:504 # Graphics shapes Rectangle initialization puts the leftmost point in its left field
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:522 # Graphics shapes Rectangle comparison for equality is true if rectangle points are the same
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:529 # Graphics shapes Rectangle comparison for equality is true if rectangle points are the same, even if swapped
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:536 # Graphics shapes Rectangle comparison for equality is true for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:543 # Graphics shapes Rectangle comparison for equality works with eql? as well
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:552 # Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:559 # Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:575 # Graphics shapes Rectangle corners top left
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:581 # Graphics shapes Rectangle corners top right
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:587 # Graphics shapes Rectangle corners bottom right
rspec /tmp/d20131223-4637-b8vg8l/spec.rb:593 # Graphics shapes Rectangle corners bottom left

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

Сашо обнови решението на 22.12.2013 17:03 (преди почти 11 години)

+module Graphics
+class Point
+ def initialize(x, y)
+ @x = x
+ @y = y
+ end
+ def draw(canvas)
+ canvas.set_pixel(@x,@y)
+ end
+ def ==(other)
+ return true if (@x == other.x && @y == other.y)
+ return false
+ end
+ alias eql ==
+ attr_reader :x,:y
+end
+class Line
+ def help_initialize(limit)
+ dev_x,dev_y=(limit[2]-limit[0]).abs,-(limit[3]-limit[1]).abs
+ step_x,step_y,err=limit[0]<limit[2] ? 1: -1,limit[1]<limit[3] ? 1: -1,dev_x+dev_y
+ change=lambda{|err,x,y,limit| err,limit[0]=err+y,limit[0]+step_x if 2*err>=y
+ err,limit[1] = err+x,limit[1]+step_y if 2*err <= x}
+ begin
+ change.call(err,dev_x,dev_y,limit)
+ @arr<<Point.new(limit[0],limit[1])end until(limit[0]==limit[2]&&limit[1]==limit[3])
+ end
+ def initialize(start,stop)
+ @arr,limits = [],[]
+ if (start.x == stop.x && start.y == stop.y)
+ @arr << Point.new(start.x,start.y)
+ else
+ limits << start.x << start.y << stop.x << stop.y
+ @arr << Point.new(limits[0],limits[1])
+ help_initialize(limits)
+ end
+ end
+ def from
+ @arr.first
+ end
+ def to
+ @arr.last
+ end
+ def draw(canvas)
+ @arr.each{|point| canvas.set_pixel(point.x,point.y)}
+ end
+ def ==(other)
+ self.from == self.to
+ end
+ alias eql ==
+end
+
+class Rectangle
+ def sequence(first,second)
+ points = []
+ if (first.y > second.y)
+ points << Point.new(first.x,second.y)<<second<<Point.new(second.x,second.y)<<first
+ else
+ points << first << Point.new(second.x,first.y) << second
+ points << Point.new(first.x,second.y)
+ end
+ points
+ end
+ def initialize(first,second)
+ @points = sequence(first,second)
+ end
+ def draw(canvas)
+ canvas.draw(Line.new(@points[0],@points[1]))
+ canvas.draw(Line.new(@points[1],@points[2]))
+ canvas.draw(Line.new(@points[2],@points[3]))
+ canvas.draw(Line.new(@points[3],@points[0]))
+ end
+ def left
+ @points[0]
+ end
+ def right
+ @points[2]
+ end
+ def ==(other)
+ return self.right == self.left
+ end
+ alias eql ==
+end
+module Renderers
+ class Ascii
+ def self.function(text,width,height,canvas)
+ width.times { |n| text +='@'if canvas.pixel_at?(n,height)
+ text +='-'if not canvas.pixel_at?(n,height)}
+ text
+ end
+ def self.draw(canvas)
+ @text,text = '',''
+ line=lambda{|width,height| @text += function(text,width,height,canvas)}
+ canvas.height.times{ |i| line.call canvas.width,i
+ @text += "\n"}
+ @text
+ end
+ end
+
+ class Html
+ def self.function(text,width,height,canvas)
+ width.times { |n| text +='<b></b>'if canvas.pixel_at?(n,height)
+ text +='<i></i>'if not canvas.pixel_at?(n,height)}
+ text
+ end
+ def self.draw(canvas)
+ text, @text = '', "<!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\">"
+ line=lambda{|width,height| @text += function(text,width,height,canvas)}
+ canvas.height.times{ |i| line.call canvas.width,i
+ @text += "<br>\n"}
+ @text+= "</div>
+ </body>
+ </html>"
+ end
+ end
+end
+class Canvas
+ def initialize(width, height)
+ @array = Array.new(height) { Array.new(width) }
+ 0.upto(height-1){|column| 0.upto(width-1){|line| @array[column][line] = 0} }
+ end
+ def set_pixel(x, y)
+ input_pixel = lambda {|column,line| @array[column][line]=1 if (column==y && line==x)}
+ 0.upto(height-1){|column| width.times{|line| input_pixel.call(column,line)} }
+ end
+ def pixel_at?(x, y)
+ return true if @array[y][x] == 1
+ return false
+ end
+ def width
+ @array[0].length
+ end
+ def height
+ length = 0
+ @array.each{|index| length+=1}
+ length
+ end
+ def draw (figure)
+ figure.draw(self)
+ end
+ def render_as(ascii)
+ ascii.draw(self)
+ end
+end
+end

Сашо обнови решението на 22.12.2013 17:33 (преди почти 11 години)

module Graphics
class Point
def initialize(x, y)
@x = x
@y = y
end
def draw(canvas)
canvas.set_pixel(@x,@y)
end
def ==(other)
return true if (@x == other.x && @y == other.y)
return false
end
alias eql ==
attr_reader :x,:y
end
class Line
def help_initialize(limit)
dev_x,dev_y=(limit[2]-limit[0]).abs,-(limit[3]-limit[1]).abs
step_x,step_y,err=limit[0]<limit[2] ? 1: -1,limit[1]<limit[3] ? 1: -1,dev_x+dev_y
change=lambda{|err,x,y,limit| err,limit[0]=err+y,limit[0]+step_x if 2*err>=y
err,limit[1] = err+x,limit[1]+step_y if 2*err <= x}
begin
change.call(err,dev_x,dev_y,limit)
@arr<<Point.new(limit[0],limit[1])end until(limit[0]==limit[2]&&limit[1]==limit[3])
end
def initialize(start,stop)
@arr,limits = [],[]
if (start.x == stop.x && start.y == stop.y)
@arr << Point.new(start.x,start.y)
else
limits << start.x << start.y << stop.x << stop.y
@arr << Point.new(limits[0],limits[1])
help_initialize(limits)
end
end
def from
@arr.first
end
def to
@arr.last
end
def draw(canvas)
@arr.each{|point| canvas.set_pixel(point.x,point.y)}
end
def ==(other)
self.from == self.to
end
alias eql ==
end
class Rectangle
def sequence(first,second)
points = []
if (first.y > second.y)
- points << Point.new(first.x,second.y)<<second<<Point.new(second.x,second.y)<<first
+ points << Point.new(first.x,second.y)<<second<<Point.new(second.x,first.y)<<first
else
points << first << Point.new(second.x,first.y) << second
points << Point.new(first.x,second.y)
end
points
end
def initialize(first,second)
@points = sequence(first,second)
end
def draw(canvas)
canvas.draw(Line.new(@points[0],@points[1]))
canvas.draw(Line.new(@points[1],@points[2]))
canvas.draw(Line.new(@points[2],@points[3]))
canvas.draw(Line.new(@points[3],@points[0]))
end
def left
@points[0]
end
def right
@points[2]
end
def ==(other)
return self.right == self.left
end
alias eql ==
end
module Renderers
class Ascii
def self.function(text,width,height,canvas)
width.times { |n| text +='@'if canvas.pixel_at?(n,height)
text +='-'if not canvas.pixel_at?(n,height)}
text
end
def self.draw(canvas)
@text,text = '',''
line=lambda{|width,height| @text += function(text,width,height,canvas)}
canvas.height.times{ |i| line.call canvas.width,i
@text += "\n"}
@text
end
end
class Html
def self.function(text,width,height,canvas)
width.times { |n| text +='<b></b>'if canvas.pixel_at?(n,height)
text +='<i></i>'if not canvas.pixel_at?(n,height)}
text
end
def self.draw(canvas)
text, @text = '', "<!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\">"
line=lambda{|width,height| @text += function(text,width,height,canvas)}
canvas.height.times{ |i| line.call canvas.width,i
@text += "<br>\n"}
@text+= "</div>
</body>
</html>"
end
end
end
class Canvas
def initialize(width, height)
@array = Array.new(height) { Array.new(width) }
0.upto(height-1){|column| 0.upto(width-1){|line| @array[column][line] = 0} }
end
def set_pixel(x, y)
input_pixel = lambda {|column,line| @array[column][line]=1 if (column==y && line==x)}
0.upto(height-1){|column| width.times{|line| input_pixel.call(column,line)} }
end
def pixel_at?(x, y)
return true if @array[y][x] == 1
return false
end
def width
@array[0].length
end
def height
length = 0
@array.each{|index| length+=1}
length
end
def draw (figure)
figure.draw(self)
end
def render_as(ascii)
ascii.draw(self)
end
end
end

Сашо обнови решението на 22.12.2013 18:09 (преди почти 11 години)

module Graphics
class Point
- def initialize(x, y)
- @x = x
- @y = y
- end
- def draw(canvas)
- canvas.set_pixel(@x,@y)
- end
- def ==(other)
- return true if (@x == other.x && @y == other.y)
- return false
- end
- alias eql ==
- attr_reader :x,:y
+ def initialize(x, y)
+ @x = x
+ @y = y
+ end
+ def draw(canvas)
+ canvas.set_pixel(@x,@y)
+ end
+ def ==(other)
+ return true if (@x == other.x && @y == other.y)
+ return false
+ end
+ alias eql ==
+ attr_reader :x,:y
end
class Line
- def help_initialize(limit)
- dev_x,dev_y=(limit[2]-limit[0]).abs,-(limit[3]-limit[1]).abs
- step_x,step_y,err=limit[0]<limit[2] ? 1: -1,limit[1]<limit[3] ? 1: -1,dev_x+dev_y
- change=lambda{|err,x,y,limit| err,limit[0]=err+y,limit[0]+step_x if 2*err>=y
- err,limit[1] = err+x,limit[1]+step_y if 2*err <= x}
- begin
- change.call(err,dev_x,dev_y,limit)
- @arr<<Point.new(limit[0],limit[1])end until(limit[0]==limit[2]&&limit[1]==limit[3])
- end
- def initialize(start,stop)
- @arr,limits = [],[]
- if (start.x == stop.x && start.y == stop.y)
- @arr << Point.new(start.x,start.y)
- else
- limits << start.x << start.y << stop.x << stop.y
- @arr << Point.new(limits[0],limits[1])
- help_initialize(limits)
- end
- end
- def from
- @arr.first
- end
- def to
- @arr.last
- end
- def draw(canvas)
- @arr.each{|point| canvas.set_pixel(point.x,point.y)}
- end
- def ==(other)
- self.from == self.to
- end
- alias eql ==
+ def help_initialize(limit)
+ dev_x,dev_y=(limit[2]-limit[0]).abs,-(limit[3]-limit[1]).abs
+ step_x,step_y,err=limit[0]<limit[2] ? 1: -1,limit[1]<limit[3] ? 1: -1,dev_x+dev_y
+ change=lambda{|err,x,y,limit| err,limit[0]=err+y,limit[0]+step_x if 2*err>=y
+ err,limit[1] = err+x,limit[1]+step_y if 2*err <= x}
+ begin
+ change.call(err,dev_x,dev_y,limit)
+ @arr<<Point.new(limit[0],limit[1])end until(limit[0]==limit[2]&&limit[1]==limit[3])
+ end
+ def initialize(start,stop)
+ @arr,limits = [],[]
+ if (start.x == stop.x && start.y == stop.y)
+ @arr << Point.new(start.x,start.y)
+ else
+ limits << start.x << start.y << stop.x << stop.y
+ @arr << Point.new(limits[0],limits[1])
+ help_initialize(limits)
+ end
+ end
+ def from
+ @arr.first
+ end
+ def to
+ @arr.last
+ end
+ def draw(canvas)
+ @arr.each{|point| canvas.set_pixel(point.x,point.y)}
+ end
+ def ==(other)
+ self.from == self.to
+ end
+ alias eql ==
end
class Rectangle
- def sequence(first,second)
- points = []
- if (first.y > second.y)
- points << Point.new(first.x,second.y)<<second<<Point.new(second.x,first.y)<<first
- else
- points << first << Point.new(second.x,first.y) << second
- points << Point.new(first.x,second.y)
- end
- points
- end
- def initialize(first,second)
- @points = sequence(first,second)
- end
- def draw(canvas)
- canvas.draw(Line.new(@points[0],@points[1]))
- canvas.draw(Line.new(@points[1],@points[2]))
- canvas.draw(Line.new(@points[2],@points[3]))
- canvas.draw(Line.new(@points[3],@points[0]))
- end
- def left
- @points[0]
- end
- def right
- @points[2]
- end
- def ==(other)
- return self.right == self.left
- end
- alias eql ==
+ def sequence(first,second)
+ points = []
+ if (first.y > second.y)
+ points << Point.new(first.x,second.y)<<second<<Point.new(second.x,first.y)<<first
+ else
+ points << first << Point.new(second.x,first.y) << second
+ points << Point.new(first.x,second.y)
+ end
+ points
+ end
+ def initialize(first,second)
+ @points = sequence(first,second)
+ end
+ def draw(canvas)
+ canvas.draw(Line.new(@points[0],@points[1]))
+ canvas.draw(Line.new(@points[1],@points[2]))
+ canvas.draw(Line.new(@points[2],@points[3]))
+ canvas.draw(Line.new(@points[3],@points[0]))
+ end
+ def left
+ @points[0]
+ end
+ def right
+ @points[2]
+ end
+ def ==(other)
+ return self.right == self.left
+ end
+ alias eql ==
end
module Renderers
- class Ascii
- def self.function(text,width,height,canvas)
- width.times { |n| text +='@'if canvas.pixel_at?(n,height)
- text +='-'if not canvas.pixel_at?(n,height)}
- text
- end
- def self.draw(canvas)
- @text,text = '',''
- line=lambda{|width,height| @text += function(text,width,height,canvas)}
- canvas.height.times{ |i| line.call canvas.width,i
- @text += "\n"}
- @text
- end
- end
-
- class Html
- def self.function(text,width,height,canvas)
- width.times { |n| text +='<b></b>'if canvas.pixel_at?(n,height)
- text +='<i></i>'if not canvas.pixel_at?(n,height)}
- text
- end
- def self.draw(canvas)
- text, @text = '', "<!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\">"
- line=lambda{|width,height| @text += function(text,width,height,canvas)}
- canvas.height.times{ |i| line.call canvas.width,i
- @text += "<br>\n"}
- @text+= "</div>
- </body>
- </html>"
- end
- end
+ class Ascii
+ def self.function(text,width,height,canvas)
+ width.times { |n| text +='@'if canvas.pixel_at?(n,height)
+ text +='-'if not canvas.pixel_at?(n,height)}
+ text
+ end
+ def self.draw(canvas)
+ @text,text = '',''
+ line=lambda{|width,height| @text += function(text,width,height,canvas)}
+ canvas.height.times{ |i| line.call canvas.width,i
+ @text += "\n"}
+ @text
+ end
+ end
+ class Html
+ def self.function(text,width,height,canvas)
+ width.times { |n| text +='<b></b>'if canvas.pixel_at?(n,height)
+ text +='<i></i>'if not canvas.pixel_at?(n,height)}
+ text
+ end
+ def self.draw(canvas)
+ text, @text = '', "<!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\">"
+ line=lambda{|width,height| @text += function(text,width,height,canvas)}
+ canvas.height.times{ |i| line.call canvas.width,i
+ @text += "<br>\n"}
+ @text+= "</div>
+ </body>
+ </html>"
+ end
+ end
end
class Canvas
- def initialize(width, height)
- @array = Array.new(height) { Array.new(width) }
- 0.upto(height-1){|column| 0.upto(width-1){|line| @array[column][line] = 0} }
- end
- def set_pixel(x, y)
- input_pixel = lambda {|column,line| @array[column][line]=1 if (column==y && line==x)}
- 0.upto(height-1){|column| width.times{|line| input_pixel.call(column,line)} }
- end
- def pixel_at?(x, y)
- return true if @array[y][x] == 1
- return false
- end
- def width
- @array[0].length
- end
- def height
- length = 0
- @array.each{|index| length+=1}
- length
- end
- def draw (figure)
- figure.draw(self)
- end
- def render_as(ascii)
- ascii.draw(self)
- end
+ def initialize(width, height)
+ @array = Array.new(height) { Array.new(width) }
+ 0.upto(height-1){|column| 0.upto(width-1){|line| @array[column][line] = 0} }
+ end
+ def set_pixel(x, y)
+ input_pixel = lambda {|column,line| @array[column][line]=1 if (column==y && line==x)}
+ 0.upto(height-1){|column| width.times{|line| input_pixel.call(column,line)} }
+ end
+ def pixel_at?(x, y)
+ return true if @array[y][x] == 1
+ return false
+ end
+ def width
+ @array[0].length
+ end
+ def height
+ length = 0
+ @array.each{|index| length+=1}
+ length
+ end
+ def draw (figure)
+ figure.draw(self)
+ end
+ def render_as(ascii)
+ ascii.draw(self)
+ end
end
end

Сашо обнови решението на 22.12.2013 21:49 (преди почти 11 години)

module Graphics
class Point
def initialize(x, y)
@x = x
@y = y
end
def draw(canvas)
canvas.set_pixel(@x,@y)
end
def ==(other)
- return true if (@x == other.x && @y == other.y)
- return false
+ return true if (@x == other.x && @y == other.y)
+ return false
end
alias eql ==
attr_reader :x,:y
end
class Line
- def help_initialize(limit)
- dev_x,dev_y=(limit[2]-limit[0]).abs,-(limit[3]-limit[1]).abs
- step_x,step_y,err=limit[0]<limit[2] ? 1: -1,limit[1]<limit[3] ? 1: -1,dev_x+dev_y
- change=lambda{|err,x,y,limit| err,limit[0]=err+y,limit[0]+step_x if 2*err>=y
- err,limit[1] = err+x,limit[1]+step_y if 2*err <= x}
+ def help_initialize(arr,x,y,st_x,st_y,err)
+ line=lambda{|x,y,st_x,st_y,err,arr|x,y=(arr[2]-arr[0]).abs,-(arr[3]-arr[1]).abs
+ st_x,st_y,err=arr[0]<arr[2] ? 1: -1,arr[1]<arr[3] ? 1: -1,x+y
+ err,arr[0]=err+y,arr[0]+st_x if 2*err>=y
+ err,arr[1] = err+x,arr[1]+st_y if 2*err <= x}
begin
- change.call(err,dev_x,dev_y,limit)
- @arr<<Point.new(limit[0],limit[1])end until(limit[0]==limit[2]&&limit[1]==limit[3])
+ line.call(x,y,st_x,st_y,err,arr)
+ @arr<<Point.new(arr[0],arr[1])end until(arr[0]==arr[2]&&arr[1]==arr[3])
end
def initialize(start,stop)
- @arr,limits = [],[]
+ @arr,limits,devx,devy,step_x,step_y,err = [],[],1,1,1,1,1
if (start.x == stop.x && start.y == stop.y)
@arr << Point.new(start.x,start.y)
else
limits << start.x << start.y << stop.x << stop.y
@arr << Point.new(limits[0],limits[1])
- help_initialize(limits)
+ help_initialize(limits,devx,devy,step_x,step_y,err)
end
end
def from
@arr.first
end
def to
@arr.last
end
def draw(canvas)
@arr.each{|point| canvas.set_pixel(point.x,point.y)}
end
def ==(other)
self.from == self.to
end
alias eql ==
end
class Rectangle
def sequence(first,second)
points = []
if (first.y > second.y)
points << Point.new(first.x,second.y)<<second<<Point.new(second.x,first.y)<<first
else
points << first << Point.new(second.x,first.y) << second
points << Point.new(first.x,second.y)
end
points
end
def initialize(first,second)
@points = sequence(first,second)
end
def draw(canvas)
- canvas.draw(Line.new(@points[0],@points[1]))
- canvas.draw(Line.new(@points[1],@points[2]))
- canvas.draw(Line.new(@points[2],@points[3]))
- canvas.draw(Line.new(@points[3],@points[0]))
+ canvas.draw(Line.new(@points[0],@points[1]))
+ canvas.draw(Line.new(@points[1],@points[2]))
+ canvas.draw(Line.new(@points[2],@points[3]))
+ canvas.draw(Line.new(@points[3],@points[0]))
end
def left
@points[0]
end
def right
@points[2]
end
def ==(other)
return self.right == self.left
end
alias eql ==
end
module Renderers
class Ascii
def self.function(text,width,height,canvas)
width.times { |n| text +='@'if canvas.pixel_at?(n,height)
text +='-'if not canvas.pixel_at?(n,height)}
text
end
def self.draw(canvas)
@text,text = '',''
line=lambda{|width,height| @text += function(text,width,height,canvas)}
- canvas.height.times{ |i| line.call canvas.width,i
- @text += "\n"}
- @text
+ canvas.height.times{ |i| line.call canvas.width,i
+ @text += "\n"}
+ @text
end
end
class Html
def self.function(text,width,height,canvas)
width.times { |n| text +='<b></b>'if canvas.pixel_at?(n,height)
text +='<i></i>'if not canvas.pixel_at?(n,height)}
text
- end
- def self.draw(canvas)
- text, @text = '', "<!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 self.draw(canvas)
+ text, @text = '', "<!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\">"
line=lambda{|width,height| @text += function(text,width,height,canvas)}
canvas.height.times{ |i| line.call canvas.width,i
@text += "<br>\n"}
@text+= "</div>
</body>
</html>"
end
end
end
class Canvas
def initialize(width, height)
@array = Array.new(height) { Array.new(width) }
0.upto(height-1){|column| 0.upto(width-1){|line| @array[column][line] = 0} }
end
def set_pixel(x, y)
input_pixel = lambda {|column,line| @array[column][line]=1 if (column==y && line==x)}
0.upto(height-1){|column| width.times{|line| input_pixel.call(column,line)} }
end
def pixel_at?(x, y)
return true if @array[y][x] == 1
return false
end
def width
@array[0].length
end
def height
- length = 0
- @array.each{|index| length+=1}
- length
+ length = 0
+ @array.each{|index| length+=1}
+ length
end
def draw (figure)
figure.draw(self)
end
def render_as(ascii)
ascii.draw(self)
end
end
end