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

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

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

Резултати

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

Код

module Graphics
module Renderers
class Ascii
def self.render(canvas)
to_array = canvas.table.flatten.map do |element|
element == true ? '@' : '-'
end
to_ascii_string = ""
to_array.each_slice(canvas.width) do |element|
to_ascii_string << element.join << "\n"
end
to_ascii_string[0..-2]
end
end
class Html
def self.write_beg()
"<!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.write_end()
string_end = "\n<div> </body> </html>"
end
def self.to_string_array(array)
array.flatten.map do |element|
element == true ? '<b></b>' : '<i></i>'
end
end
def self.add_end_symbol(array, width, height)
array.each_with_index do |item, index|
if (index + 1) % width == 0 and index < width * height - 1
item << '<br>'
end
end
end
def self.render(canvas)
string_array = to_string_array(canvas.table)
string_array = add_end_symbol(string_array, canvas.width, canvas.height)
string_mid = ''
string_array.each_slice(canvas.width) do |element|
string_mid << "\n" << element.join
end
write_beg + string_mid + write_end
end
end
end
class Canvas
attr_accessor :width, :height, :table
def initialize(width, height)
@width = width
@height = height
@table = Array.new(height) { |i| Array.new(width) { |j| false } }
end
def set_pixel(x, y)
@table[y][x] = true
end
def bigger_number(first_number, second_number)
first_number < second_number ? 1 : -1
end
def if_steep(x, y, steep)
if steep
set_pixel(y, x)
else
set_pixel(x, y)
end
end
def set_values(from, to)
x_0, y_0, x_1, y_1 = from.x, from.y, to.x, to.y
steep = ((y_1 - y_0).abs) > ((x_1 - x_0).abs)
x_0, y_0, x_1, y_1 = y_0, x_0, y_1, x_1 if steep
x_0, x_1, y_0, y_1 = x_1, x_0, y_1, y_0 if x_0 > x_1
delta_x, delta_y, y = x_1 - x_0, (y_1 - y_0).abs, y_0
[x_0, x_1, y_0, y_1, delta_x, delta_y, y, steep]
end
def set_line(from, to)
x_0, x_1, y_0, y_1, delta_x, delta_y, y, steep = set_values(from, to)
error, y_step = (delta_x / 2).to_i, bigger_number(y_0, y_1)
(x_0..x_1).each do |x|
if_steep(x, y, steep)
error -= delta_y
y, error = y + y_step, error + delta_x if error < 1
end
end
def pixel_at?(x, y)
@table[x][y]
end
def render_as(renderer)
renderer.render(self)
end
def draw(figure)
figure.draw_figure(self)
end
end
class Point
attr_accessor :point_hash
def initialize(x, y)
@point_hash = { :x => x, :y => y }
end
def x
@point_hash[:x]
end
def y
@point_hash[:y]
end
def ==(second_point)
self.point_hash == second_point.point_hash
end
alias eql? ==
def draw_figure(canvas)
canvas.set_pixel(self.x, self.y)
end
end
class Line
attr_accessor :line_hash
def check_y(first_point, second_point)
if(first_point.y >= second_point.y)
@line_hash = { :from => first_point, :to => second_point }
else
@line_hash = { :from => second_point, :to => first_point }
end
end
def initialize(first_point, second_point)
if(first_point.x < second_point.x)
@line_hash = { :from => first_point, :to => second_point }
elsif (first_point.x == second_point.x)
check_y(first_point, second_point)
else
@line_hash = { :from => second_point, :to => first_point }
end
end
def from
@line_hash[:from]
end
def to
@line_hash[:to]
end
def ==(second_line)
line_hash == second_line.line_hash
end
alias eql? ==
def draw_figure(canvas)
canvas.set_line(from, to)
end
end
class Rectangle
attr_accessor :rectangle_hash
def check_y(first_point, second_point)
if(first_point.y >= second_point.y)
@rectangle_hash = { :left => first_point, :right => second_point }
else
@rectangle_hash = { :left => second_point, :right => first_point }
end
end
def initialize(first_point, second_point)
if(first_point.x < second_point.x)
@rectangle_hash = { :left => first_point, :right => second_point }
elsif (first_point.x == second_point.x)
check_y(first_point, second_point)
else
@rectangle_hash = { :left => second_point, :right => first_point }
end
end
def left
@rectangle_hash[:left]
end
def right
@rectangle_hash[:right]
end
def top_left
if @rectangle_hash[:left].y < @rectangle_hash[:right].y
@rectangle_hash[:left]
else
Point.new(@rectangle_hash[:left].x, @rectangle_hash[:right].y)
end
end
def bottom_left
if @rectangle_hash[:left].y > @rectangle_hash[:right].y
@rectangle_hash[:left]
else
Point.new(@rectangle_hash[:left].x, @rectangle_hash[:right].y)
end
end
def top_right
if @rectangle_hash[:left].y > @rectangle_hash[:right].y
@rectangle_hash[:right]
else
Point.new( @rectangle_hash[:right].x, @rectangle_hash[:left].y)
end
end
def bottom_right
if @rectangle_hash[:left].y < @rectangle_hash[:right].y
@rectangle_hash[:right]
else
Point.new(@rectangle_hash[:right].x, @rectangle_hash[:left].y)
end
end
def ==(second_rectangle)
top_left_bottom_right = Rectangle.new(second_rectangle.top_left,
second_rectangle.bottom_right)
bottom_left_top_right = Rectangle.new(second_rectangle.top_right,
second_rectangle.bottom_left)
rectangle_hash == top_left_bottom_right.rectangle_hash or
rectangle_hash == bottom_left_top_right.rectangle_hash
end
alias eql? ==
def draw_figure(canvas)
canvas.set_line(top_left, bottom_left)
canvas.set_line(top_left, top_right)
canvas.set_line(top_right, bottom_right)
canvas.set_line(bottom_right, bottom_left)
end
end
end

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

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

Failures:

  1) Graphics Canvas does not expose setters for the width or height
     Failure/Error: canvas.should_not respond_to :width=
       expected #<Graphics::Canvas:0xb9014dc0 @width=30, @height=20, @table=[[false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]]> not to respond to :width=
     # /tmp/d20131223-4637-1b6h8wj/spec.rb:23: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 allows setting a pixel at a given x and y
     Failure/Error: canvas.pixel_at?(3, 5).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-1b6h8wj/spec.rb:40: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 drawing of shapes and rasterization of points works for a single one
     Failure/Error: canvas.pixel_at?(2, 4).should be_true
       expected: true value
            got: false
     # /tmp/d20131223-4637-1b6h8wj/spec.rb:48: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 points works for multiple ones
     Failure/Error: canvas.set_pixel 4, 4
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     # /tmp/d20131223-4637-1b6h8wj/solution.rb:67:in `set_pixel'
     # /tmp/d20131223-4637-1b6h8wj/spec.rb:57:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  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----------"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,6 @@
        ----------
        -@@-------
       ----@@@@---
       --------@@-
       +---@@@----
       +------@@@-
        ----------
     # /tmp/d20131223-4637-1b6h8wj/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-1b6h8wj/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----------"
       
       (compared using ==)
       
       Diff:
       @@ -4,7 +4,7 @@
        --@-------
        --@-------
        --@-------
       ---@-------
       +---@------
        ---@------
        ---@------
        ----------
     # /tmp/d20131223-4637-1b6h8wj/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-1b6h8wj/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----------"
       
       (compared using ==)
       
       Diff:
       @@ -1,6 +1,6 @@
        -@--------
        -@@-------
       --@-@@@@---
       --@-----@@-
       +-@-@@@----
       +-@----@@@-
        ----------
     # /tmp/d20131223-4637-1b6h8wj/spec.rb:624:in `check_rendering_of'
     # /tmp/d20131223-4637-1b6h8wj/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 Renderers Html renders a grid of the size of the canvas
     Failure/Error: rendering.match(/<divclass="canvas">(.*?)<\/div>/)[1]
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20131223-4637-1b6h8wj/spec.rb:310:in `html_rendering_of'
     # /tmp/d20131223-4637-1b6h8wj/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)>'

  9) Graphics Renderers Html renders simple canvases
     Failure/Error: rendering.match(/<divclass="canvas">(.*?)<\/div>/)[1]
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20131223-4637-1b6h8wj/spec.rb:310:in `html_rendering_of'
     # /tmp/d20131223-4637-1b6h8wj/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)>'

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

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

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

  13) 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: 194093949
            got: 958979791
       
       (compared using ==)
     # /tmp/d20131223-4637-1b6h8wj/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)>'

  14) 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: 668329441
            got: 863740393
       
       (compared using ==)
     # /tmp/d20131223-4637-1b6h8wj/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)>'

  15) 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: 730533475
            got: -771140177
       
       (compared using ==)
     # /tmp/d20131223-4637-1b6h8wj/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)>'

Finished in 0.08634 seconds
69 examples, 15 failures

Failed examples:

rspec /tmp/d20131223-4637-1b6h8wj/spec.rb:22 # Graphics Canvas does not expose setters for the width or height
rspec /tmp/d20131223-4637-1b6h8wj/spec.rb:37 # Graphics Canvas allows setting a pixel at a given x and y
rspec /tmp/d20131223-4637-1b6h8wj/spec.rb:45 # Graphics Canvas drawing of shapes and rasterization of points works for a single one
rspec /tmp/d20131223-4637-1b6h8wj/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-1b6h8wj/spec.rb:96 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
rspec /tmp/d20131223-4637-1b6h8wj/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-1b6h8wj/spec.rb:127 # Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
rspec /tmp/d20131223-4637-1b6h8wj/spec.rb:280 # Graphics Renderers Html renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-1b6h8wj/spec.rb:287 # Graphics Renderers Html renders simple canvases
rspec /tmp/d20131223-4637-1b6h8wj/spec.rb:360 # Graphics shapes Point comparison for equality returns the same hash for the same points
rspec /tmp/d20131223-4637-1b6h8wj/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-1b6h8wj/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-1b6h8wj/spec.rb:461 # Graphics shapes Line comparison for equality returns the same hash if the lines are the same
rspec /tmp/d20131223-4637-1b6h8wj/spec.rb:552 # Graphics shapes Rectangle comparison for equality returns the same hash if the rectangles are the same
rspec /tmp/d20131223-4637-1b6h8wj/spec.rb:559 # Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners

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

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

+module Graphics
+
+ module Renderers
+ class Ascii
+ def self.render(canvas)
+ to_array = canvas.table.flatten.map do |element|
+ element == true ? '@' : '-'
+ end
+ to_ascii_string = ""
+ to_array.each_slice(canvas.width) do |element|
+ to_ascii_string << element.join << "\n"
+ end
+ to_ascii_string[0..-2]
+ end
+ end
+
+ class Html
+ def self.write_beg()
+ "<!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.write_end()
+ string_end = "\n<div> </body> </html>"
+ end
+
+ def self.to_string_array(array)
+ array.flatten.map do |element|
+ element == true ? '<b></b>' : '<i></i>'
+ end
+ end
+
+ def self.add_end_symbol(array, width, height)
+ array.each_with_index do |item, index|
+ if (index + 1) % width == 0 and index < width * height - 1
+ item << '<br>'
+ end
+ end
+ end
+
+ def self.render(canvas)
+ string_array = to_string_array(canvas.table)
+ string_array = add_end_symbol(string_array, canvas.width, canvas.height)
+ string_mid = ''
+ string_array.each_slice(canvas.width) do |element|
+ string_mid << "\n" << element.join
+ end
+ write_beg + string_mid + write_end
+ end
+ end
+ end
+
+ class Canvas
+ attr_accessor :width, :height, :table
+
+ def initialize(width, height)
+ @width = width
+ @height = height
+ @table = Array.new(height) { |i| Array.new(width) { |j| false } }
+ end
+
+ def set_pixel(x, y)
+ @table[y][x] = true
+ end
+
+ def bigger_number(first_number, second_number)
+ first_number < second_number ? 1 : -1
+ end
+
+ def if_steep(x, y, steep)
+ if steep
+ set_pixel(y, x)
+ else
+ set_pixel(x, y)
+ end
+ end
+
+ def set_values(from, to)
+ x_0, y_0, x_1, y_1 = from.x, from.y, to.x, to.y
+ steep = ((y_1 - y_0).abs) > ((x_1 - x_0).abs)
+ x_0, y_0, x_1, y_1 = y_0, x_0, y_1, x_1 if steep
+ x_0, x_1, y_0, y_1 = x_1, x_0, y_1, y_0 if x_0 > x_1
+ delta_x, delta_y, y = x_1 - x_0, (y_1 - y_0).abs, y_0
+ [x_0, x_1, y_0, y_1, delta_x, delta_y, y, steep]
+ end
+
+ def set_line(from, to)
+ x_0, x_1, y_0, y_1, delta_x, delta_y, y, steep = set_values(from, to)
+ error, y_step = (delta_x / 2).to_i, bigger_number(y_0, y_1)
+ (x_0..x_1).each do |x|
+ if_steep(x, y, steep)
+ error -= delta_y
+ y, error = y + y_step, error + delta_x if error < 1
+ end
+ end
+
+ def pixel_at?(x, y)
+ @table[x][y]
+ end
+
+ def render_as(renderer)
+ renderer.render(self)
+ end
+
+ def draw(figure)
+ figure.draw_figure(self)
+ end
+ end
+
+ class Point
+ attr_accessor :point_hash
+
+ def initialize(x, y)
+ @point_hash = { :x => x, :y => y }
+ end
+
+ def x
+ @point_hash[:x]
+ end
+
+ def y
+ @point_hash[:y]
+ end
+
+ def ==(second_point)
+ self.point_hash == second_point.point_hash
+ end
+ alias eql? ==
+
+ def draw_figure(canvas)
+ canvas.set_pixel(self.x, self.y)
+ end
+ end
+
+ class Line
+ attr_accessor :line_hash
+
+ def check_y(first_point, second_point)
+ if(first_point.y >= second_point.y)
+ @line_hash = { :from => first_point, :to => second_point }
+ else
+ @line_hash = { :from => second_point, :to => first_point }
+ end
+ end
+
+ def initialize(first_point, second_point)
+ if(first_point.x < second_point.x)
+ @line_hash = { :from => first_point, :to => second_point }
+ elsif (first_point.x == second_point.x)
+ check_y(first_point, second_point)
+ else
+ @line_hash = { :from => second_point, :to => first_point }
+ end
+ end
+
+ def from
+ @line_hash[:from]
+ end
+
+ def to
+ @line_hash[:to]
+ end
+
+ def ==(second_line)
+ line_hash == second_line.line_hash
+ end
+ alias eql? ==
+
+ def draw_figure(canvas)
+ canvas.set_line(from, to)
+ end
+ end
+
+ class Rectangle
+ attr_accessor :rectangle_hash
+
+ def check_y(first_point, second_point)
+ if(first_point.y >= second_point.y)
+ @rectangle_hash = { :left => first_point, :right => second_point }
+ else
+ @rectangle_hash = { :left => second_point, :right => first_point }
+ end
+ end
+
+ def initialize(first_point, second_point)
+ if(first_point.x < second_point.x)
+ @rectangle_hash = { :left => first_point, :right => second_point }
+ elsif (first_point.x == second_point.x)
+ check_y(first_point, second_point)
+ else
+ @rectangle_hash = { :left => second_point, :right => first_point }
+ end
+ end
+
+ def left
+ @rectangle_hash[:left]
+ end
+
+ def right
+ @rectangle_hash[:right]
+ end
+
+ def top_left
+ if @rectangle_hash[:left].y < @rectangle_hash[:right].y
+ @rectangle_hash[:left]
+ else
+ Point.new(@rectangle_hash[:left].x, @rectangle_hash[:right].y)
+ end
+ end
+
+ def bottom_left
+ if @rectangle_hash[:left].y > @rectangle_hash[:right].y
+ @rectangle_hash[:left]
+ else
+ Point.new(@rectangle_hash[:left].x, @rectangle_hash[:right].y)
+ end
+ end
+
+ def top_right
+ if @rectangle_hash[:left].y > @rectangle_hash[:right].y
+ @rectangle_hash[:right]
+ else
+ Point.new( @rectangle_hash[:right].x, @rectangle_hash[:left].y)
+ end
+ end
+
+ def bottom_right
+ if @rectangle_hash[:left].y < @rectangle_hash[:right].y
+ @rectangle_hash[:right]
+ else
+ Point.new(@rectangle_hash[:right].x, @rectangle_hash[:left].y)
+ end
+ end
+
+ def ==(second_rectangle)
+ top_left_bottom_right = Rectangle.new(second_rectangle.top_left,
+ second_rectangle.bottom_right)
+ bottom_left_top_right = Rectangle.new(second_rectangle.top_right,
+ second_rectangle.bottom_left)
+ rectangle_hash == top_left_bottom_right.rectangle_hash or
+ rectangle_hash == bottom_left_top_right.rectangle_hash
+ end
+ alias eql? ==
+
+ def draw_figure(canvas)
+ canvas.set_line(top_left, bottom_left)
+ canvas.set_line(top_left, top_right)
+ canvas.set_line(top_right, bottom_right)
+ canvas.set_line(bottom_right, bottom_left)
+ end
+ end
+
+end