Решение на Трета задача от Валентин Ейткен

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

Към профила на Валентин Ейткен

Резултати

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

Код

module Graphics
class Canvas
attr_reader :width, :height
def initialize(width, height)
@width, @height = width, height
@container = Array.new(height) { Array.new width, false }
end
def set_pixel(x, y)
@container[y][x] = true
end
def pixel_at?(x, y)
@container[y][x]
end
def draw(figure)
figure.rasterize.each do |point|
draw_point(Point.new(figure.start.x + point.x, figure.start.y + point.y))
end
end
def render_as renderer
if renderer == Renderers::Ascii
map_lane('@', '-')
elsif renderer == Renderers::Html
output = <<HTML
<!DOCTYPE html>
<html>
<head>
<title>Rendered Canvas</title>
<style type="text/css">
.canvas {
font-size: 1px;
line-height: 1px;
}
.canvas * {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 5px;
}
.canvas i {
background-color: #eee;
}
.canvas b {
background-color: #333;
}
</style>
</head>
<body>
<div class="canvas">
HTML
output += "#{map_lane('<b></b>', '<i></i>', '<br>')}</div></body></html>"
end
end
def map_lane(filled, empty, row_delimiter = "\n")
@container.map do |row|
map_lane_row(row, filled, empty)
end.join row_delimiter
end
private
def map_lane_row(row, filled, empty)
row.map { |s| s ? filled : empty }.join
end
def draw_point(point)
@container[point.y][point.x] = true
end
end
module Renderers
class Ascii
end
class Html
end
end
class Point
attr_reader :x, :y
def initialize(x, y)
@x, @y = x, y
end
def ==(object)
if object.kind_of? Point
object.x == x && object.y == y
else
false
end
end
def eql?(point)
point.hash == hash
end
def hash
x * 100 + y
end
def rasterize
[Point.new(0, 0)]
end
def start
self
end
end
class Line
def initialize(first, second)
@first, @second = first, second
end
def from
if @first.x < @second.x
@first
elsif @first.x > @second.x
@second
else
y_from
end
end
def to
if @first.x > @second.x
@first
elsif @first.x < @second.x
@second
else
y_to
end
end
def start
from
end
def rasterize
if from.x == to.x
(from.y..to.y).map { |y| Point.new(0, y - from.y) }
elsif from.y == to.y
(from.x..to.x).map { |x| Point.new(x - from.x, 0) }
end
end
def ==(line)
from == to
end
def eql?(line)
line.hash == hash
end
def hash
from.hash * 1000 + to.hash
end
private
def y_from
if @first.y < @second.y
@first
else
@second
end
end
def y_to
if @first.y > @second.y
@first
else
@second
end
end
end
class Rectangle
attr_reader :left, :right
def initialize(first, second)
diagonal = Line.new(first, second)
@left = diagonal.from
@right = diagonal.to
end
def top_left
left.y < right.y && left || Point.new(left.x, right.y)
end
def top_right
right.y < left.y && right || Point.new(right.x, left.y)
end
def bottom_left
left.y > right.y && left || Point.new(left.x, right.y)
end
def bottom_right
right.y > left.y && right || Point.new(right.x, left.y)
end
def ==(rectangle)
top_left == rectangle.top_left and bottom_right == rectangle.bottom_right
end
def eql?(rectangle)
rectangle.hash == hash
end
def hash
Line.new(top_left, bottom_right).hash
end
end
end

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

........F.F..FFF.FFFF.........................FFF....................

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 `rasterize' for #<Graphics::Rectangle:0xba57a0e8>
     # /tmp/d20131223-4637-kocnc0/solution.rb:19:in `draw'
     # /tmp/d20131223-4637-kocnc0/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: canvas.set_pixel 4, 4
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     # /tmp/d20131223-4637-kocnc0/solution.rb:11:in `set_pixel'
     # /tmp/d20131223-4637-kocnc0/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)>'

  3) Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
     Failure/Error: canvas.draw make_line(make_point(1, 1), make_point(8, 3))
     NoMethodError:
       undefined method `each' for nil:NilClass
     # /tmp/d20131223-4637-kocnc0/solution.rb:19:in `draw'
     # /tmp/d20131223-4637-kocnc0/spec.rb:98: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 lines with a significant slope, with swapped ends
     Failure/Error: canvas.draw make_line(make_point(3, 8), make_point(1, 1))
     NoMethodError:
       undefined method `each' for nil:NilClass
     # /tmp/d20131223-4637-kocnc0/solution.rb:19:in `draw'
     # /tmp/d20131223-4637-kocnc0/spec.rb:111: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 multiple lines
     Failure/Error: canvas.draw make_line(make_point(1, 1), make_point(8, 3))
     NoMethodError:
       undefined method `each' for nil:NilClass
     # /tmp/d20131223-4637-kocnc0/solution.rb:19:in `draw'
     # /tmp/d20131223-4637-kocnc0/spec.rb:129: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 rectangles works with simple rects
     Failure/Error: canvas.draw make_rectangle(make_point(1, 1), make_point(8, 3))
     NoMethodError:
       undefined method `rasterize' for #<Graphics::Rectangle:0xba52a32c>
     # /tmp/d20131223-4637-kocnc0/solution.rb:19:in `draw'
     # /tmp/d20131223-4637-kocnc0/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)>'

  7) 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 `rasterize' for #<Graphics::Rectangle:0xba528d10>
     # /tmp/d20131223-4637-kocnc0/solution.rb:19:in `draw'
     # /tmp/d20131223-4637-kocnc0/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)>'

  8) 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 `rasterize' for #<Graphics::Rectangle:0xba523e00>
     # /tmp/d20131223-4637-kocnc0/solution.rb:19:in `draw'
     # /tmp/d20131223-4637-kocnc0/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)>'

  9) 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 `rasterize' for #<Graphics::Rectangle:0xba522ce4>
     # /tmp/d20131223-4637-kocnc0/solution.rb:19:in `draw'
     # /tmp/d20131223-4637-kocnc0/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)>'

  10) 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-kocnc0/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)>'

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

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

Finished in 0.08122 seconds
69 examples, 12 failures

Failed examples:

rspec /tmp/d20131223-4637-kocnc0/spec.rb:203 # Graphics Canvas drawing of shapes and rasterization renders multiple drawn shapes
rspec /tmp/d20131223-4637-kocnc0/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-kocnc0/spec.rb:96 # Graphics Canvas drawing of shapes and rasterization of lines works with lines with a small slope
rspec /tmp/d20131223-4637-kocnc0/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-kocnc0/spec.rb:127 # Graphics Canvas drawing of shapes and rasterization of lines works with multiple lines
rspec /tmp/d20131223-4637-kocnc0/spec.rb:154 # Graphics Canvas drawing of shapes and rasterization of rectangles works with simple rects
rspec /tmp/d20131223-4637-kocnc0/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-kocnc0/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-kocnc0/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-kocnc0/spec.rb:431 # Graphics shapes Line comparison for equality is true if line ends are the same
rspec /tmp/d20131223-4637-kocnc0/spec.rb:438 # Graphics shapes Line comparison for equality is true if line ends are the same, even if swapped
rspec /tmp/d20131223-4637-kocnc0/spec.rb:445 # Graphics shapes Line comparison for equality is true if line is vertical and the bottom is given first

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

Валентин обнови решението на 22.12.2013 08:02 (преди почти 11 години)

+# not ready
+module Graphics
+ class Canvas
+ attr_reader :width, :height
+
+ def initialize(width, height)
+ @width, @height = width, height
+ @container = Array.new(height) { Array.new width, false }
+ end
+
+ # задава пикселът, идентифициран от координатите x и y е "запълнен".
+ def set_pixel(x, y)
+ @container[y][x] = true
+ end
+
+ # проверява "запълнен" ли е пиксела идентифициран от координатите x и y
+ def pixel_at?(x, y)
+ @container[y][x]
+ end
+
+ def draw
+ end
+
+ #
+ # === Args
+ # +renderer+::
+ # sdsdfsf
+ #
+ # === Description
+ #
+ def render_as renderer
+ if renderer == Renderers::Ascii
+ map_lane('@', '-')
+ elsif renderer == Renderers::Html
+ output = <<HTML
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Rendered Canvas</title>
+ <style type="text/css">
+ .canvas {
+ font-size: 1px;
+ line-height: 1px;
+ }
+ .canvas * {
+ display: inline-block;
+ width: 10px;
+ height: 10px;
+ border-radius: 5px;
+ }
+ .canvas i {
+ background-color: #eee;
+ }
+ .canvas b {
+ background-color: #333;
+ }
+ </style>
+</head>
+<body>
+ <div class="canvas">
+HTML
+ output += "#{map_lane('<b></b>', '<i></i>', '<br>')}</div></body></html>"
+ end
+ end
+
+ def map_lane(filled, empty, row_delimiter = "\n")
+ @container.map do |row|
+ map_lane_row(row, filled, empty)
+ end.join row_delimiter
+ end
+
+ private
+ def map_lane_row(row, filled, empty)
+ row.map { |s| s ? filled : empty }.join
+ end
+ end
+
+ module Renderers
+ class Ascii
+ end
+
+ class Html
+ end
+ end
+
+ class Point
+ attr_reader :x, :y
+
+ def initialize(x, y)
+ @x, @y = x, y
+ end
+
+ def ==(object)
+ if object.kind_of? Point
+ object.x == x && object.y == y
+ else
+ false
+ end
+ end
+ end
+
+ class Line
+ def initialize(one_point, another_point)
+ @one_point, @another_point = one_point, another_point
+ end
+
+ def from
+ if @one_point.x < @another_point.x
+ @one_point
+ elsif @one_point.x > @another_point.x
+ @another_point
+ else
+ y_from
+ end
+ end
+
+ def to
+ if @one_point.x > @another_point.x
+ @one_point
+ elsif @one_point.x < @another_point.x
+ @another_point
+ else
+ end
+ end
+
+ private
+ def y_from
+ if @one_point.y < @another_point.y
+ @one_point
+ else
+ @another_point
+ end
+ end
+
+ def y_to
+ if @one_point.y > @another_point.y
+ @one_point
+ else
+ @another_point
+ end
+ end
+ end
+
+ class Rectangle
+ attr_reader :left, :right
+
+ def initialize(one_point, another_point)
+ diagonal = Line.new(one_point, another_point)
+ @left = diagonal.from
+ @right = diagonal.to
+ end
+
+ def top_left
+ end
+
+ def top_right
+ end
+
+ def bottom_left
+ end
+
+ def bottom_right
+ end
+ end
+end

Бележки:

  • Виждам коментара "not ready", но гледай да разкараш всички коментари в окончателния вариант :) Добрият и ясен код няма нужда от такива.
  • Помисли дали няма друг по-оптимален начин в Ruby за реализация вътрешното представяне на пано, от масив от масиви. Ruby не е C и има по-удобни структури от данни, за които дори няма нужда да require-ваш нищо :)
  • Също така, pixel_at? може да връща нещо, което се оценява като истина или лъжа; не е задължително да е true/false.
  • Помисли кой трбява да носи отговорността за "рендериране" на пано. Самото пано или рендерера? Помисли кой обект каква отговорност носи. Една основна идея на ОО-програмирането е да разпределиш различните отговорности между различни обекти. Стреми се всеки обект да отговаря за едно конкретно нещо и само за него. Напълно нормално е да имаш голям брой малки обекти в системата, с тясно специализирани отговорности. В момента класовете ти Html и Ascii са два празни такива.
  • map_lane? Какво е "lane"? Платно, лента? Може би line? Същото и за map_lane_row, което може би има нещо общо с "пиксел".
  • В случая е удачно HTML кодът на Html да се пази в константа/и в Html.
  • Няма нужда да проверяваш за типа на обекта при сравнение на фигури. Ще сравняваме в тестовете винаги фигури от един и същи тип. Също така, не виждам да си имплементирал eql? и hash.
  • one_point и another_point не са достатъчно добри имена. Ако са просто две точки, кръсти ги first и second, или нещо такова. "One" и "another" не са подходящи в случая.
  • Оставяй по един празен ред след private.
  • else клонът в Line#to е празен. Ако искаш да върнеш nil, просто го пропускаш.

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

-# not ready
module Graphics
class Canvas
attr_reader :width, :height
def initialize(width, height)
@width, @height = width, height
@container = Array.new(height) { Array.new width, false }
end
- # задава пикселът, идентифициран от координатите x и y е "запълнен".
def set_pixel(x, y)
@container[y][x] = true
end
- # проверява "запълнен" ли е пиксела идентифициран от координатите x и y
def pixel_at?(x, y)
@container[y][x]
end
- def draw
+ def draw(figure)
+ @container[figure.y][figure.x] = true
end
- #
- # === Args
- # +renderer+::
- # sdsdfsf
- #
- # === Description
- #
def render_as renderer
if renderer == Renderers::Ascii
map_lane('@', '-')
elsif renderer == Renderers::Html
output = <<HTML
<!DOCTYPE html>
<html>
<head>
<title>Rendered Canvas</title>
<style type="text/css">
.canvas {
font-size: 1px;
line-height: 1px;
}
.canvas * {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 5px;
}
.canvas i {
background-color: #eee;
}
.canvas b {
background-color: #333;
}
</style>
</head>
<body>
<div class="canvas">
HTML
output += "#{map_lane('<b></b>', '<i></i>', '<br>')}</div></body></html>"
end
end
def map_lane(filled, empty, row_delimiter = "\n")
@container.map do |row|
map_lane_row(row, filled, empty)
end.join row_delimiter
end
private
+
def map_lane_row(row, filled, empty)
row.map { |s| s ? filled : empty }.join
end
end
module Renderers
class Ascii
end
class Html
end
end
class Point
attr_reader :x, :y
def initialize(x, y)
@x, @y = x, y
end
def ==(object)
if object.kind_of? Point
object.x == x && object.y == y
else
false
end
end
+
+ def eql?(point)
+ point.hash == hash
+ end
+
+ def hash
+ x * 100 + y
+ end
+
+ def rasterize
+ [[x, y]]
+ end
end
class Line
def initialize(one_point, another_point)
@one_point, @another_point = one_point, another_point
end
def from
if @one_point.x < @another_point.x
@one_point
elsif @one_point.x > @another_point.x
@another_point
else
y_from
end
end
def to
if @one_point.x > @another_point.x
@one_point
elsif @one_point.x < @another_point.x
@another_point
else
+ y_to
end
end
+ def ==(line)
+ from == to
+ end
+
+ def eql?(line)
+ line.hash == hash
+ end
+
+ def hash
+ from.hash * 1000 + to.hash
+ end
+
private
+
def y_from
if @one_point.y < @another_point.y
@one_point
else
@another_point
end
end
def y_to
if @one_point.y > @another_point.y
@one_point
else
@another_point
end
end
end
class Rectangle
attr_reader :left, :right
def initialize(one_point, another_point)
diagonal = Line.new(one_point, another_point)
@left = diagonal.from
@right = diagonal.to
end
def top_left
+ left.y < right.y && left || Point.new(left.x, right.y)
end
def top_right
+ right.y < left.y && right || Point.new(right.x, left.y)
end
def bottom_left
+ left.y > right.y && left || Point.new(left.x, right.y)
end
def bottom_right
+ right.y > left.y && right || Point.new(right.x, left.y)
+ end
+
+ def ==(rectangle)
+ top_left == rectangle.top_left and bottom_right == rectangle.bottom_right
+ end
+
+ def eql?(rectangle)
+ rectangle.hash == hash
+ end
+
+ def hash
+ Line.new(top_left, bottom_right).hash
end
end
end

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

module Graphics
class Canvas
attr_reader :width, :height
def initialize(width, height)
@width, @height = width, height
@container = Array.new(height) { Array.new width, false }
end
def set_pixel(x, y)
@container[y][x] = true
end
def pixel_at?(x, y)
@container[y][x]
end
def draw(figure)
- @container[figure.y][figure.x] = true
+ figure.rasterize.each do |point|
+ draw_point(Point.new(figure.start.x + point.x, figure.start.y + point.y))
+ end
end
def render_as renderer
if renderer == Renderers::Ascii
map_lane('@', '-')
elsif renderer == Renderers::Html
output = <<HTML
<!DOCTYPE html>
<html>
<head>
<title>Rendered Canvas</title>
<style type="text/css">
.canvas {
font-size: 1px;
line-height: 1px;
}
.canvas * {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 5px;
}
.canvas i {
background-color: #eee;
}
.canvas b {
background-color: #333;
}
</style>
</head>
<body>
<div class="canvas">
HTML
output += "#{map_lane('<b></b>', '<i></i>', '<br>')}</div></body></html>"
end
end
def map_lane(filled, empty, row_delimiter = "\n")
@container.map do |row|
map_lane_row(row, filled, empty)
end.join row_delimiter
end
private
def map_lane_row(row, filled, empty)
row.map { |s| s ? filled : empty }.join
end
+
+ def draw_point(point)
+ @container[point.y][point.x] = true
+ end
end
module Renderers
class Ascii
end
class Html
end
end
class Point
attr_reader :x, :y
def initialize(x, y)
@x, @y = x, y
end
def ==(object)
if object.kind_of? Point
object.x == x && object.y == y
else
false
end
end
def eql?(point)
point.hash == hash
end
def hash
x * 100 + y
end
def rasterize
- [[x, y]]
+ [Point.new(0, 0)]
end
+
+ def start
+ self
+ end
end
class Line
- def initialize(one_point, another_point)
- @one_point, @another_point = one_point, another_point
+ def initialize(first, second)
+ @first, @second = first, second
end
def from
- if @one_point.x < @another_point.x
- @one_point
- elsif @one_point.x > @another_point.x
- @another_point
+ if @first.x < @second.x
+ @first
+ elsif @first.x > @second.x
+ @second
else
y_from
end
end
def to
- if @one_point.x > @another_point.x
- @one_point
- elsif @one_point.x < @another_point.x
- @another_point
+ if @first.x > @second.x
+ @first
+ elsif @first.x < @second.x
+ @second
else
y_to
end
end
+ def start
+ from
+ end
+
+ def rasterize
+ if from.x == to.x
+ (from.y..to.y).map { |y| Point.new(0, y - from.y) }
+ elsif from.y == to.y
+ (from.x..to.x).map { |x| Point.new(x - from.x, 0) }
+ end
+ end
+
def ==(line)
from == to
end
def eql?(line)
line.hash == hash
end
def hash
from.hash * 1000 + to.hash
end
private
def y_from
- if @one_point.y < @another_point.y
- @one_point
+ if @first.y < @second.y
+ @first
else
- @another_point
+ @second
end
end
def y_to
- if @one_point.y > @another_point.y
- @one_point
+ if @first.y > @second.y
+ @first
else
- @another_point
+ @second
end
end
end
class Rectangle
attr_reader :left, :right
- def initialize(one_point, another_point)
- diagonal = Line.new(one_point, another_point)
+ def initialize(first, second)
+ diagonal = Line.new(first, second)
@left = diagonal.from
@right = diagonal.to
end
def top_left
left.y < right.y && left || Point.new(left.x, right.y)
end
def top_right
right.y < left.y && right || Point.new(right.x, left.y)
end
def bottom_left
left.y > right.y && left || Point.new(left.x, right.y)
end
def bottom_right
right.y > left.y && right || Point.new(right.x, left.y)
end
def ==(rectangle)
top_left == rectangle.top_left and bottom_right == rectangle.bottom_right
end
def eql?(rectangle)
rectangle.hash == hash
end
def hash
Line.new(top_left, bottom_right).hash
end
end
end