Решение на Трета задача от Пепа Симеонова

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

Към профила на Пепа Симеонова

Резултати

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

Код

module Graphics
class Canvas
def initialize width,height
@canvas = Array.new width*height
@height = height
@width = width
end
attr_reader :width
attr_reader :height
attr_accessor :canvas
def set_pixel x,y
canvas[x*y] = true
end
def pixel_at? x,y
return canvas[x*y] == true
end
def draw figure
if figure.instance_of? Point
draw_point figure
end
if figure.instance_of? Line
draw_line figure
end
if figure.instance_of? Rectangle
draw_rectangle figure
end
end
def render_as renderer
renderer.new self
end
def draw_point point
set_pixel point.x,point.y
end
def draw_line line
range_horizontal = Range.new(line.from.x,line.to.x)
range_horizontal.each{|i| set_pixel i,line.from.y}
range_vertical = Range.new(line.from.y,line.to.y)
range_vertical.each{|i| set_pixel i,line.from.x }
end
def draw_rectangle rectangle
draw_line Line.new(rectangle.top_left,rectangle.top_right)
draw_line Line.new(rectangle.top_left,rectangle.bottom_left)
draw_line Line.new(rectangle.top_right,rectangle.bottom_right)
draw_line Line.new(rectangle.bottom_left,rectangle.bottom_right)
end
end
class Point
def initialize x,y
@x = x
@y = y
end
attr_reader :x
attr_reader :y
def hash
hash = {:x => @x, :y => @y}
end
def ==(other)
self.x == other.x && self.y == other.y
end
alias eql? ==
def eql?(other)
self.==(other)
end
end
class Line
def initialize from,to
if from.x != to.x && from.x <= to.x && from.y <= to.y
@from = from
@to = to
elsif from.x != to.x
@from = to
@to = from
end
end
attr_reader :from
attr_reader :to
def hash
{:from => @from, :to => @to}
end
def ==(other)
(self.from == other.from && self.to == other.to) ||
(self.from == other.to && self.to == other.from)
end
alias eql? ==
def eql?(other)
self.==(other)
end
end
class Rectangle
def initialize left,right
@left = left
@right = right
end
attr_reader :left
attr_reader :right
def top_left
if left.y < right.y
left
else
Point.new left.x,right.y
end
end
def top_right
if right.y < left.y
right
else
Point.new right.x,left.y
end
end
def bottom_left
if left.y < right.y
Point.new left.x,right.y
else
left
end
end
def bottom_right
if right.y < left.y
Point.new right.x,left.y
else
right
end
end
def ==(other)
self.top_left == other.top_left &&
self.top_right == other.top_right&&
self.bottom_left == other.bottom_left &&
self.bottom_right == other.bottom_right
end
alias eql? ==
def eql?(other)
self.==(other)
end
end
#class Renderers
#def Ascii canvas
# canvas.canvas.each_slice(canvas.width) do |slice|
#print "\n"
#slice.each{|x| if x == true then print "@" else print "-" end}
#end
#end
#def Html canvas
# canvas.canvas.each_slice(çanvas.width) do |slice|
# print "<br>"
# slice.each{|x| if x == true then print "<b></b>" else print "<i></i>" end}
#end
#end
#end
end

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

........F.FFFFFFFFFFFFFFFFFF...........F...FF...........F..FF.FF...FF

Failures:

  1) Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
     Failure/Error: canvas.draw make_rectangle(make_point(2, 2), make_point(12, 12))
     NoMethodError:
       undefined method `x' for nil:NilClass
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:44:in `draw_line'
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:52:in `draw_rectangle'
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:31:in `draw'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:205: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 = canvas.render_as(Graphics::Renderers::Ascii)
     NameError:
       uninitialized constant Graphics::Renderers
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:623:in `check_rendering_of'
     # /tmp/d20131223-4637-1sz0zpj/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 = canvas.render_as(Graphics::Renderers::Ascii)
     NameError:
       uninitialized constant Graphics::Renderers
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:623:in `check_rendering_of'
     # /tmp/d20131223-4637-1sz0zpj/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: canvas.draw make_line(make_point(1, 0), make_point(1, 6))
     NoMethodError:
       undefined method `x' for nil:NilClass
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:44:in `draw_line'
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:28:in `draw'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:82: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 = canvas.render_as(Graphics::Renderers::Ascii)
     NameError:
       uninitialized constant Graphics::Renderers
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:623:in `check_rendering_of'
     # /tmp/d20131223-4637-1sz0zpj/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 = canvas.render_as(Graphics::Renderers::Ascii)
     NameError:
       uninitialized constant Graphics::Renderers
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:623:in `check_rendering_of'
     # /tmp/d20131223-4637-1sz0zpj/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: canvas.draw make_line(make_point(1, 0), make_point(1, 3))
     NoMethodError:
       undefined method `x' for nil:NilClass
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:44:in `draw_line'
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:28:in `draw'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:130: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: canvas.draw make_line(make_point(1, 1), make_point(1, 1))
     NoMethodError:
       undefined method `x' for nil:NilClass
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:44:in `draw_line'
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:28:in `draw'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:143: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: canvas.draw make_rectangle(make_point(1, 1), make_point(8, 3))
     NoMethodError:
       undefined method `x' for nil:NilClass
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:44:in `draw_line'
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:52:in `draw_rectangle'
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:31:in `draw'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:156: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: canvas.draw make_rectangle(make_point(1, 3), make_point(8, 1))
     NoMethodError:
       undefined method `x' for nil:NilClass
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:44:in `draw_line'
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:52:in `draw_rectangle'
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:31:in `draw'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:169: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: canvas.draw make_rectangle(make_point(1, 1), make_point(8, 1))
     NoMethodError:
       undefined method `x' for nil:NilClass
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:44:in `draw_line'
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:52:in `draw_rectangle'
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:31:in `draw'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:182: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: canvas.draw make_rectangle(make_point(1, 1), make_point(1, 1))
     NoMethodError:
       undefined method `x' for nil:NilClass
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:44:in `draw_line'
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:51:in `draw_rectangle'
     # /tmp/d20131223-4637-1sz0zpj/solution.rb:31:in `draw'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:193:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  13) Graphics Renderers Ascii renders a grid of the size of the canvas
     Failure/Error: let(:ascii) { Graphics::Renderers::Ascii }
     NameError:
       uninitialized constant Graphics::Renderers
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:234:in `block (4 levels) in <top (required)>'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:238:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  14) Graphics Renderers Ascii renders blank canvases
     Failure/Error: let(:ascii) { Graphics::Renderers::Ascii }
     NameError:
       uninitialized constant Graphics::Renderers
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:234:in `block (4 levels) in <top (required)>'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:245:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  15) Graphics Renderers Ascii renders simple canvases
     Failure/Error: let(:ascii) { Graphics::Renderers::Ascii }
     NameError:
       uninitialized constant Graphics::Renderers
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:234:in `block (4 levels) in <top (required)>'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:256:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  16) Graphics Renderers Html returns html
     Failure/Error: let(:html)        { Graphics::Renderers::Html }
     NameError:
       uninitialized constant Graphics::Renderers
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:265:in `block (4 levels) in <top (required)>'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:272:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  17) Graphics Renderers Html renders a grid of the size of the canvas
     Failure/Error: let(:html)        { Graphics::Renderers::Html }
     NameError:
       uninitialized constant Graphics::Renderers
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:265:in `block (4 levels) in <top (required)>'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:309:in `html_rendering_of'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:281:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  18) Graphics Renderers Html renders simple canvases
     Failure/Error: let(:html)        { Graphics::Renderers::Html }
     NameError:
       uninitialized constant Graphics::Renderers
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:265:in `block (4 levels) in <top (required)>'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:309:in `html_rendering_of'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:291:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  19) Graphics Renderers Html returns the same rendering when called twice
     Failure/Error: let(:html)        { Graphics::Renderers::Html }
     NameError:
       uninitialized constant Graphics::Renderers
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:265:in `block (4 levels) in <top (required)>'
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:302:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  20) Graphics shapes 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-1sz0zpj/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)>'

  21) Graphics shapes Line initialization with swapped points puts the top point of vertical lines in the from field
     Failure/Error: vertical_line.from.x.should eq 1
     NoMethodError:
       undefined method `x' for nil:NilClass
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:412: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 initialization with swapped points puts the bottom point of vertical lines in the to field
     Failure/Error: vertical_line.to.x.should eq 1
     NoMethodError:
       undefined method `x' for nil:NilClass
     # /tmp/d20131223-4637-1sz0zpj/spec.rb:417: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)>'

  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-1sz0zpj/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 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-1sz0zpj/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)>'

  25) 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-1sz0zpj/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)>'

  26) 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: -520675836
            got: -644236716
       
       (compared using ==)
     # /tmp/d20131223-4637-1sz0zpj/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)>'

  27) 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: -937538790
            got: -775931542
       
       (compared using ==)
     # /tmp/d20131223-4637-1sz0zpj/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)>'

  28) Graphics shapes Rectangle corners bottom right
     Failure/Error: rect.bottom_right.x.should eq 5
       
       expected: 5
            got: 1
       
       (compared using ==)
     # /tmp/d20131223-4637-1sz0zpj/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)>'

  29) Graphics shapes Rectangle corners bottom left
     Failure/Error: rect.bottom_left.x.should eq 1
       
       expected: 1
            got: 5
       
       (compared using ==)
     # /tmp/d20131223-4637-1sz0zpj/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.08319 seconds
69 examples, 29 failures

Failed examples:

rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:203 # Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:69 # Graphics Canvas drawing of shapes and rasterization of lines works with simple horizontal lines
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:80 # Graphics Canvas drawing of shapes and rasterization of lines works with vertical lines
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:96 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
rspec /tmp/d20131223-4637-1sz0zpj/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-1sz0zpj/spec.rb:127 # Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:141 # Graphics Canvas drawing of shapes and rasterization of lines draws lines with two equal ends as points
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:154 # Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
rspec /tmp/d20131223-4637-1sz0zpj/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-1sz0zpj/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-1sz0zpj/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-1sz0zpj/spec.rb:237 # Graphics Renderers Ascii renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:244 # Graphics Renderers Ascii renders blank canvases
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:252 # Graphics Renderers Ascii renders simple canvases
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:271 # Graphics Renderers Html returns html
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:280 # Graphics Renderers Html renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:287 # Graphics Renderers Html renders simple canvases
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:298 # Graphics Renderers Html returns the same rendering when called twice
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:382 # Graphics shapes Line initialization returns its from and to points via getters
rspec /tmp/d20131223-4637-1sz0zpj/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-1sz0zpj/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-1sz0zpj/spec.rb:504 # Graphics shapes Rectangle initialization puts the leftmost point in its left field
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:529 # Graphics shapes Rectangle comparison for equality is true if rectangle points are the same, even if swapped
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:536 # Graphics shapes Rectangle comparison for equality is true for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:552 # Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:559 # Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:587 # Graphics shapes Rectangle corners bottom right
rspec /tmp/d20131223-4637-1sz0zpj/spec.rb:593 # Graphics shapes Rectangle corners bottom left

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

Пепа обнови решението на 22.12.2013 23:27 (преди почти 11 години)

+module Graphics
+
+ class Canvas
+
+ def initialize width,height
+ @canvas = Array.new width*height
+ @height = height
+ @width = width
+ end
+
+ attr_reader :width
+ attr_reader :height
+ attr_accessor :canvas
+
+ def set_pixel x,y
+ canvas[x*y] = true
+ end
+
+ def pixel_at? x,y
+ return canvas[x*y] == true
+ end
+
+ def draw figure
+ if figure.instance_of? Point
+ draw_point figure
+ end
+ if figure.instance_of? Line
+ draw_line figure
+ end
+ if figure.instance_of? Rectangle
+ draw_rectangle figure
+ end
+ end
+
+ def render_as renderer
+ renderer.new self
+ end
+
+ def draw_point point
+ set_pixel point.x,point.y
+ end
+
+ def draw_line line
+ range_horizontal = Range.new(line.from.x,line.to.x)
+ range_horizontal.each{|i| set_pixel i,line.from.y}
+ range_vertical = Range.new(line.from.y,line.to.y)
+ range_vertical.each{|i| set_pixel i,line.from.x }
+ end
+
+ def draw_rectangle rectangle
+ draw_line Line.new(rectangle.top_left,rectangle.top_right)
+ draw_line Line.new(rectangle.top_left,rectangle.bottom_left)
+ draw_line Line.new(rectangle.top_right,rectangle.bottom_right)
+ draw_line Line.new(rectangle.bottom_left,rectangle.bottom_right)
+ end
+ end
+
+ class Point
+ def initialize x,y
+ @x = x
+ @y = y
+ end
+
+ attr_reader :x
+ attr_reader :y
+
+ def hash
+ hash = {:x => @x, :y => @y}
+ end
+
+ def ==(other)
+ self.x == other.x && self.y == other.y
+ end
+
+ alias eql? ==
+ def eql?(other)
+ self.==(other)
+ end
+
+ end
+
+ class Line
+ def initialize from,to
+ if from.x != to.x && from.x <= to.x && from.y <= to.y
+ @from = from
+ @to = to
+ elsif from.x != to.x
+ @from = to
+ @to = from
+ end
+ end
+
+ attr_reader :from
+ attr_reader :to
+
+ def hash
+ {:from => @from, :to => @to}
+ end
+
+ def ==(other)
+ (self.from == other.from && self.to == other.to) ||
+ (self.from == other.to && self.to == other.from)
+ end
+
+ alias eql? ==
+ def eql?(other)
+ self.==(other)
+ end
+ end
+
+ class Rectangle
+ def initialize left,right
+ @left = left
+ @right = right
+ end
+ attr_reader :left
+ attr_reader :right
+
+ def top_left
+ if left.y < right.y
+ left
+ else
+ Point.new left.x,right.y
+ end
+ end
+
+ def top_right
+ if right.y < left.y
+ right
+ else
+ Point.new right.x,left.y
+ end
+ end
+
+ def bottom_left
+ if left.y < right.y
+ Point.new left.x,right.y
+ else
+ left
+ end
+ end
+
+ def bottom_right
+ if right.y < left.y
+ Point.new right.x,left.y
+ else
+ right
+ end
+ end
+
+ def ==(other)
+ self.top_left == other.top_left &&
+ self.top_right == other.top_right&&
+ self.bottom_left == other.bottom_left &&
+ self.bottom_right == other.bottom_right
+ end
+
+ alias eql? ==
+ def eql?(other)
+ self.==(other)
+ end
+ end
+
+ #class Renderers
+ #def Ascii canvas
+ # canvas.canvas.each_slice(canvas.width) do |slice|
+ #print "\n"
+ #slice.each{|x| if x == true then print "@" else print "-" end}
+ #end
+ #end
+
+ #def Html canvas
+ # canvas.canvas.each_slice(çanvas.width) do |slice|
+ # print "<br>"
+ # slice.each{|x| if x == true then print "<b></b>" else print "<i></i>" end}
+ #end
+ #end
+ #end
+end