Решение на Трета задача от Кристиян Азманов

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

Към профила на Кристиян Азманов

Резултати

  • 5 точки от тестове
  • 1 отнета точка
  • 4 точки общо
  • 59 успешни тест(а)
  • 10 неуспешни тест(а)

Код

module Graphics
class Point
attr_reader :x, :y
def initialize(x, y)
@x, @y = x, y
end
def ==(other_point)
[@x, @y] == [other_point.x, other_point.y]
end
alias :eql? :==
def hash()
"x#{@x}y#{@y}".hash()
end
end
class Line
attr_reader :from, :to
def initialize(point_one, point_two)
if point_one == point_two then
@from, @to = point_one, point_one
else
@from = get_from_point(point_one, point_two)
@to = point_one == @from ? point_two : point_one
end
end
def ==(other_line)
[@from, @to] == [other_line.from, other_line.to]
end
alias :eql? :==
def hash()
"x0#{@from.x}y0#{@from.y}_x1#{@to.x}y1#{@to.y}".hash()
end
def get_from_point(first_point, second_point)
if first_point.x != second_point.x then
first_point.x < second_point.x ? first_point : second_point
elsif first_point.y != second_point.y then
first_point.y < second_point.y ? first_point : second_point
end
end
end
class Rectangle
attr_reader :left, :right
def initialize(point_one, point_two)
if point_one == point_two then
@left, @right = point_one, point_onew
else
@left = get_from_point(point_one, point_two)
@right = point_one == @left ? point_two : point_one
end
end
def top_left
(right.y - left.y) < 0 ? Point.new(left.x, right.y) : left
end
def top_right
(right.y - left.y) > 0 ? Point.new(right.x, left.y) : right
end
def bottom_left
(right.y - left.y) > 0 ? Point.new(left.x, right.y) : left
end
def bottom_right
(right.y - left.y) < 0 ? Point.new(right.x, left.y) : right
end
def ==(another_one)
@left == another_one.left and @right == another_one.right
end
alias :eql? :==
def hash()
"x0#{left.x}y0#{left.y}_x1#{right.x}y1#{right.y}".hash()
end
def get_from_point(first_point, second_point)
if first_point.x != second_point.x then
first_point.x < second_point.x ? first_point : second_point
elsif first_point.y != second_point.y then
first_point.y < second_point.y ? first_point : second_point
end
end
end
module Renderers
class Ascii
def self.ascii_render(canvas)
a= canvas.map do |arr|
Graphics::Renderers::Ascii.ascii_render_helper(arr)
end.join("\n")
end
def self.ascii_render_helper(array)
array.join.gsub(/(true|false)/) do |x|
if x =~ /false/ then '-'
elsif x =~ /true/ then '@'
end
end
end
end
HTML_BEGIN = "<!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_END = " </div>
</body>
</html>"
class Html
def self.html_render(canvas)
puts Graphics::Renderers::HTML_BEGIN
canvas.each_with_index do |row, index|
Graphics::Renderers::Html.html_render_helper(row, index, canvas)
end
puts Graphics::Renderers::HTML_END
end
def self.html_render_helper(row, index, canvas)
if index == canvas.size - 1 then
puts row.join.gsub('true','<b></b>').gsub('false','<i></i>')
else
puts row.join.gsub('true','<b></b>').gsub('false','<i></i>') + '<br>'
end
end
end
end
module Drawing
def self.draw_rectangle(canvas, rectangle)
draw_line(canvas, Line.new(rectangle.top_left, rectangle.top_right))
draw_line(canvas, Line.new(rectangle.top_left, rectangle.bottom_left))
draw_line(canvas, Line.new(rectangle.bottom_left, rectangle.bottom_right))
draw_line(canvas, Line.new(rectangle.top_right, rectangle.bottom_right))
end
def self.draw_line(canvas, line)
canvas[line.from.y][line.from.x]=true if (line.from == line.to)
way = (line.to.x - line.from.x).abs >= (line.to.y - line.from.y).abs ? 'x' : 'y'
draw_first_type(canvas, line, way) if (way == 'x' and line.from.y <= line.to.y)
draw_second_type(canvas, line, way) if (way =='y' and line.from.y <= line.to.y)
draw_third_type(canvas, line, way) if (way == 'x' and line.from.y > line.to.y)
draw_fourth_type(canvas, line, way) if (way == 'y' and line.from.y > line.to.y)
end
def self.draw_first_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y)
length = way == 'x' ? dx : dy
first_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.first_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y][x+=1], f = true, f - [a, b].min
else canvas[y += 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_second_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y)
length = way == 'x' ? dx : dy
second_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.second_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y+=1][x], f = true, (f - [a, b].min)
else canvas[y += 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_third_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y).abs
length = way == 'x' ? dx : dy
third_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.third_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y][x+=1], f = true, f - [a, b].min
else canvas[y -= 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_fourth_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y).abs
length = way == 'x' ? dx : dy
fourth_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.fourth_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y-=1][x], f = true, f - [a, b].min
else canvas[y -= 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
end
class Canvas
attr_reader :width, :height, :canvas
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
@canvas = Array.new(canvas_height){Array.new(canvas_width, false)}
end
def set_pixel(x, y)
@canvas[x][y] = true
end
def pixel_at?(x, y)
@canvas[x][y] == true
end
def draw(figure)
set_pixel(figure.y, figure.x) if (figure.instance_of? Point)
Drawing::draw_line(@canvas, figure) if (figure.instance_of? Line)
Drawing::draw_rectangle(@canvas, figure) if (figure.instance_of? Rectangle)
end
def render_as(renderer)
renderer.html_render(@canvas) if renderer == Graphics::Renderers::Html
renderer.ascii_render(@canvas) if renderer == Graphics::Renderers::Ascii
end
end
end

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

.........FF.........F..F<!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">
<i></i><i></i><i></i><i></i><br>
<i></i><i></i><i></i><i></i><br>
<i></i><i></i><i></i><i></i>
  </div>
</body>
</html>
F<!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">
<i></i><i></i><i></i><i></i><br>
<i></i><i></i><i></i><i></i><br>
<i></i><i></i><i></i><i></i>
  </div>
</body>
</html>
F<!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">
<i></i><i></i><i></i><i></i><br>
<i></i><b></b><b></b><i></i><br>
<i></i><i></i><i></i><i></i>
  </div>
</body>
</html>
F<!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">
<i></i><i></i><i></i><i></i><br>
<i></i><b></b><b></b><i></i><br>
<i></i><i></i><i></i><i></i>
  </div>
</body>
</html>
F................................F..F.....

Failures:

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

  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-1cyax33/solution.rb:246:in `set_pixel'
     # /tmp/d20131223-4637-1cyax33/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 rectangles works with rects with a zero width and height as a single point
     Failure/Error: Graphics::Rectangle.new(*args)
     NameError:
       undefined local variable or method `point_onew' for #<Graphics::Rectangle:0xba389478>
     # /tmp/d20131223-4637-1cyax33/solution.rb:53:in `initialize'
     # /tmp/d20131223-4637-1cyax33/spec.rb:615:in `new'
     # /tmp/d20131223-4637-1cyax33/spec.rb:615:in `make_rectangle'
     # /tmp/d20131223-4637-1cyax33/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)>'

  4) Graphics Renderers Ascii renders simple canvases
     Failure/Error: canvas.set_pixel 3, 2
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     # /tmp/d20131223-4637-1cyax33/solution.rb:246:in `set_pixel'
     # /tmp/d20131223-4637-1cyax33/spec.rb:254:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) Graphics Renderers Html returns html
     Failure/Error: html.gsub(/\s+/, '').downcase
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20131223-4637-1cyax33/spec.rb:314:in `normalize_html'
     # /tmp/d20131223-4637-1cyax33/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)>'

  6) Graphics Renderers Html renders a grid of the size of the canvas
     Failure/Error: html.gsub(/\s+/, '').downcase
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20131223-4637-1cyax33/spec.rb:314:in `normalize_html'
     # /tmp/d20131223-4637-1cyax33/spec.rb:309:in `html_rendering_of'
     # /tmp/d20131223-4637-1cyax33/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)>'

  7) Graphics Renderers Html renders simple canvases
     Failure/Error: html.gsub(/\s+/, '').downcase
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20131223-4637-1cyax33/spec.rb:314:in `normalize_html'
     # /tmp/d20131223-4637-1cyax33/spec.rb:309:in `html_rendering_of'
     # /tmp/d20131223-4637-1cyax33/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)>'

  8) Graphics Renderers Html returns the same rendering when called twice
     Failure/Error: html.gsub(/\s+/, '').downcase
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # /tmp/d20131223-4637-1cyax33/spec.rb:314:in `normalize_html'
     # /tmp/d20131223-4637-1cyax33/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)>'

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

  10) 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: -767886716
            got: -390236355
       
       (compared using ==)
     # /tmp/d20131223-4637-1cyax33/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.08323 seconds
69 examples, 10 failures

Failed examples:

rspec /tmp/d20131223-4637-1cyax33/spec.rb:45 # Graphics Canvas drawing of shapes and rasterization of points works for a single one
rspec /tmp/d20131223-4637-1cyax33/spec.rb:51 # Graphics Canvas drawing of shapes and rasterization of points works for multiple ones
rspec /tmp/d20131223-4637-1cyax33/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-1cyax33/spec.rb:252 # Graphics Renderers Ascii renders simple canvases
rspec /tmp/d20131223-4637-1cyax33/spec.rb:271 # Graphics Renderers Html returns html
rspec /tmp/d20131223-4637-1cyax33/spec.rb:280 # Graphics Renderers Html renders a grid of the size of the canvas
rspec /tmp/d20131223-4637-1cyax33/spec.rb:287 # Graphics Renderers Html renders simple canvases
rspec /tmp/d20131223-4637-1cyax33/spec.rb:298 # Graphics Renderers Html returns the same rendering when called twice
rspec /tmp/d20131223-4637-1cyax33/spec.rb:536 # Graphics shapes Rectangle comparison for equality is true for rectangles defined with different diagonal corners
rspec /tmp/d20131223-4637-1cyax33/spec.rb:559 # Graphics shapes Rectangle comparison for equality returns the same hash for rectangles defined with different diagonal corners

История (17 версии и 9 коментара)

Кристиян обнови решението на 15.12.2013 21:19 (преди почти 11 години)

+module Graphics
+ class Point
+ end
+
+ class Line
+ end
+
+ class Rectangle
+ end
+
+ class Canvas
+ attr_reader :@width, :height
+ def initialize(canvas_width, canvas_height)
+ @width, @height = canvas_width, canvas_height
+ end
+ end
+end

Кристиян обнови решението на 15.12.2013 21:33 (преди почти 11 години)

module Graphics
class Point
end
class Line
end
class Rectangle
end
class Canvas
attr_reader :@width, :height
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
end
+
+ def set_pixel(x, y)
+
+ end
+
+ def pixel_at?(x, y)
+
+ end
end
end

Кристиян обнови решението на 15.12.2013 21:49 (преди почти 11 години)

module Graphics
class Point
end
class Line
end
class Rectangle
end
class Canvas
attr_reader :@width, :height
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
end
def set_pixel(x, y)
end
def pixel_at?(x, y)
end
+
+
+ end
+
+ module Renderers
+
end
end

Кристиян обнови решението на 15.12.2013 22:37 (преди почти 11 години)

module Graphics
class Point
end
class Line
end
class Rectangle
end
class Canvas
attr_reader :@width, :height
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
+ @canvas = Array.new(canvas_width){Array.new(canvas_height, 'empty')}
end
def set_pixel(x, y)
-
+ @canvas[x][y] = "filled"
end
def pixel_at?(x, y)
-
+ return @canvas[x][y] == "filled"
end
+ def draw(figure)
+ end
end
module Renderers
end
end

Кристиян обнови решението на 15.12.2013 23:48 (преди почти 11 години)

module Graphics
class Point
+ attr_reader :@x, @y
+ def initialize(x, y)
+ @x, @y = x, y
+ end
end
class Line
end
class Rectangle
end
class Canvas
attr_reader :@width, :height
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
@canvas = Array.new(canvas_width){Array.new(canvas_height, 'empty')}
end
def set_pixel(x, y)
@canvas[x][y] = "filled"
end
def pixel_at?(x, y)
return @canvas[x][y] == "filled"
end
def draw(figure)
+
+ end
+
+ def render_as(rendering_type)
end
end
module Renderers
end
end

Кристиян обнови решението на 16.12.2013 00:05 (преди почти 11 години)

module Graphics
class Point
attr_reader :@x, @y
def initialize(x, y)
@x, @y = x, y
end
+
+ def equal(other)
+ return [@x, @y] == [other.x, other.y]
+ end
end
class Line
end
class Rectangle
end
class Canvas
attr_reader :@width, :height
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
@canvas = Array.new(canvas_width){Array.new(canvas_height, 'empty')}
end
def set_pixel(x, y)
@canvas[x][y] = "filled"
end
def pixel_at?(x, y)
return @canvas[x][y] == "filled"
end
def draw(figure)
end
def render_as(rendering_type)
end
end
module Renderers
end
end

Кристиян обнови решението на 16.12.2013 00:05 (преди почти 11 години)

module Graphics
class Point
attr_reader :@x, @y
def initialize(x, y)
@x, @y = x, y
end
- def equal(other)
- return [@x, @y] == [other.x, other.y]
+ def equal(other_point)
+ return [@x, @y] == [other_point.x, other_point.y]
end
+
+ alias :eql? :==
end
class Line
end
class Rectangle
end
class Canvas
attr_reader :@width, :height
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
@canvas = Array.new(canvas_width){Array.new(canvas_height, 'empty')}
end
def set_pixel(x, y)
@canvas[x][y] = "filled"
end
def pixel_at?(x, y)
return @canvas[x][y] == "filled"
end
def draw(figure)
end
def render_as(rendering_type)
end
end
module Renderers
end
end

Имаш сериозни проблеми с идентацията и спазването на конвенциите. Постарай се да ги оправиш във финалното си решение, в противен случай ще бъда принуден да ти взема наказателно точки.

Ако имаш нужда от помощ с редактора, питай във форума.

Кристиян обнови решението на 18.12.2013 21:22 (преди почти 11 години)

module Graphics
class Point
- attr_reader :@x, @y
+ attr_reader :x, :y
def initialize(x, y)
@x, @y = x, y
end
- def equal(other_point)
+ def ==(other_point)
return [@x, @y] == [other_point.x, other_point.y]
end
alias :eql? :==
+
+ def hash()
+ return "x#{@x}y#{@y}"
+ end
end
class Line
+ attr_reader :from_point, :to_point
+ def initialize(point_one, point_two)
+ @from_point = get_from_point(point_one, point_two)
+ @to_point = get_to_point(point_one, point_two)
+ end
end
class Rectangle
end
class Canvas
- attr_reader :@width, :height
+ attr_reader :width, :height
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
@canvas = Array.new(canvas_width){Array.new(canvas_height, 'empty')}
end
def set_pixel(x, y)
@canvas[x][y] = "filled"
end
def pixel_at?(x, y)
return @canvas[x][y] == "filled"
end
def draw(figure)
end
def render_as(rendering_type)
end
end
- module Renderers
-
+ def get_from_point(first_point, second_point)
+ if first_point.x != second_point.x then
+ return first_point.x < second_point.x ? first_point : second_point
+ else
+ return first_point.y < second_point.y ? first_point : second_point
+ end
end
end

Не знам повече, какво да правя с идентацията. Щом решението ми е прието на сайта, това не означава ли, че е окей?

Просто когато правя влагане давам 2 спейса, между дефинираните методи добавям нов ред, между бинарните операции оставям място, спазвам начина на именуване на променливи и константи. Вече наистина почва да ме ядосва това нещо с идентацията, защото всеки път си мисля, че съм го разбрал и все нещо не е наред. Взимайте ми точки ако искате.

Кристиян обнови решението на 18.12.2013 21:30 (преди почти 11 години)

module Graphics
class Point
attr_reader :x, :y
def initialize(x, y)
- @x, @y = x, y
- end
+ @x, @y = x, y
+ end
- def ==(other_point)
+ def ==(other_point)
return [@x, @y] == [other_point.x, other_point.y]
- end
+ end
- alias :eql? :==
+ alias :eql? :==
- def hash()
+ def hash()
return "x#{@x}y#{@y}"
- end
+ end
end
class Line
attr_reader :from_point, :to_point
def initialize(point_one, point_two)
- @from_point = get_from_point(point_one, point_two)
- @to_point = get_to_point(point_one, point_two)
- end
+ @from_point = get_from_point(point_one, point_two)
+ @to_point = get_to_point(point_one, point_two)
+ end
end
class Rectangle
end
class Canvas
attr_reader :width, :height
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
- @canvas = Array.new(canvas_width){Array.new(canvas_height, 'empty')}
+ @canvas = Array.new(canvas_width){Array.new(canvas_height, 'empty')}
end
- def set_pixel(x, y)
+ def set_pixel(x, y)
@canvas[x][y] = "filled"
- end
+ end
- def pixel_at?(x, y)
+ def pixel_at?(x, y)
return @canvas[x][y] == "filled"
- end
+ end
- def draw(figure)
+ def draw(figure)
- end
+ end
- def render_as(rendering_type)
+ def render_as(rendering_type)
- end
+ end
end
def get_from_point(first_point, second_point)
if first_point.x != second_point.x then
- return first_point.x < second_point.x ? first_point : second_point
- else
- return first_point.y < second_point.y ? first_point : second_point
- end
+ return first_point.x < second_point.x ? first_point : second_point
+ else
+ return first_point.y < second_point.y ? first_point : second_point
+ end
end
end

Няма нужда да се ядосваш и да хабиш нерви :) Просто редакторът ти явно не е добре настроен. Виж тази тема от минали години за повече подробности. Също, може да споделиш какъв редактор ползваш и да видим как да го настроим. Ако можеш, направо ме хвани някое междучасие и ще решим проблема.

И трябва да кажа, че последното решение, което си пратил, е с идеална идентация. Значи може :)

Ето ти още малко бележки по решението:

  • Навсякъде, където ползваш return в това решение, няма нужда от експлицитен return; пропуска се
  • Няма нужда да ползваш низовете "filled" и "empty", за да отбележиш двата типа пиксели; тук е подходящо или това да са символи, а не низове, или да ползваш нещо, което се оценява на истина за запълнени пиксели и нещо, което да се оценява на лъжа, за празни такива; този последен вариант би бил най-оптимален
  • Ред 55 – не се пише then в if-ове, освен ако не са едноредови (а дори и едноредовите се ползват много рядко):

      if condition then something
      elsif another_condition then something_else
      else the_default_action
    
  • Методът hash трябва да връща число; ти си тръгнал по прав път, за да подсигуриш, че хешът ще е различна стойност за различни обекти, но не си стигнал докрай; от така конструирания обект, в твоя случай низ (на ред 15), трябва да получиш число; може да се възползваш от факта, че String#hash работи и да делегираш към него :)

  • Виждам, че ползваш двумерен масив, за да пазиш данните на паното; това ще работи, но ми се струва, че в Ruby може да измислиш и друго, по-оптимално представяне; засега може да го оставиш така и да си доимплементираш задачата, а ако ти остане време, виж дали ще можеш да измислиш нещо друго като вътрешно представяне на пано

Махнах return-ите, смених "filled" и "empty" с true, false.

Но, за hash() Нещо не го схващам това със стринга. Когато съм създал обект от клас Point, самият клас имплицитно не наследява ли от Object? Object#hash има дефиниран hash() метод, който ми се струва, че мога да ползвам чрез super.hash() в моя клас и някакси да добавя координатите на точката към това Fixnum число.

Относно hash – да, Object#hash работи и е наследено в Point. По подразбиране, обаче, е дефинирано така, че за всяка инстанция ще имаш различен хеш. А ние искаме при различни инстанции, но с равни координати, да имаме еднакъв хеш.

Ако успееш да го направиш по твоя начин, окей. Нямам възражения :) Просто на мен не ми идва на ум как. Ако не успееш по твоя начин, може да помислиш как да се опреш на hash методите от други класове в Ruby.

Кристиян обнови решението на 21.12.2013 02:29 (преди почти 11 години)

module Graphics
class Point
attr_reader :x, :y
def initialize(x, y)
@x, @y = x, y
end
def ==(other_point)
- return [@x, @y] == [other_point.x, other_point.y]
+ [@x, @y] == [other_point.x, other_point.y]
end
alias :eql? :==
def hash()
- return "x#{@x}y#{@y}"
+ "x#{@x}y#{@y}"
end
end
class Line
- attr_reader :from_point, :to_point
+ attr_reader :from, :to
def initialize(point_one, point_two)
- @from_point = get_from_point(point_one, point_two)
- @to_point = get_to_point(point_one, point_two)
+ if point_one == point_two then
+ @from, @to = point_one, point_one
+ else
+ @from = get_from_point(point_one, point_two)
+ @to = ([point_one, point_two] - [@from])[0]
+ end
end
+
+ def ==(other_line)
+ [@from, @to] == [other_line.from, other_line.to]
+ end
+
+ alias :eql? :==
+
+ def hash()
+ #TODO
+ end
end
class Rectangle
+ attr_reader :left, :right
+ def initialize(point_one, point_two)
+ if point_one == point_two then
+ @left, @right = point_one, point_onew
+ else
+ @left = get_from_point(point_one, point_two)
+ @right = ([point_one, point_tow] - [@left])[0]
+ end
+ end
+
+ def top_left
+ (right.y - left.y) < 0 ? Point.new(left.x, right.y) : left
+ end
+
+ def top_right
+ (right.y - left.y) > 0 ? Point.new(left.y, right.x) : right
+ end
+
+ def bottom_left
+ (right.y - left.y) > 0 ? Point.new(left.x, right.y) : left
+ end
+
+ def bottom_right
+ (right.y - left.y) < 0 ? Point.new(right.x, left.y) : right
+ end
+
+ def ==(other_rectangle)
+ top_left == other.top_left and bottom_right == other.bottom_right
+ end
+
+ alias :eql? :==
+
+ def hash()
+ #TODO
+ end
end
class Canvas
attr_reader :width, :height
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
- @canvas = Array.new(canvas_width){Array.new(canvas_height, 'empty')}
+ @canvas = Array.new(canvas_width){Array.new(canvas_height, false)}
end
def set_pixel(x, y)
- @canvas[x][y] = "filled"
+ @canvas[x][y] = true
end
def pixel_at?(x, y)
- return @canvas[x][y] == "filled"
+ @canvas[x][y] == true
end
def draw(figure)
-
+ Drawing::draw_point(@canvas, figure) if (figure.instance_of? Point)
+ Drawing::draw_line(@canvas, figure) if (figure.instance_of? Line)
end
def render_as(rendering_type)
end
end
def get_from_point(first_point, second_point)
if first_point.x != second_point.x then
- return first_point.x < second_point.x ? first_point : second_point
- else
- return first_point.y < second_point.y ? first_point : second_point
+ first_point.x < second_point.x ? first_point : second_point
+ elsif first_point.y != second_point.y then
+ first_point.y < second_point.y ? first_point : second_point
+ end
+ end
+
+ module Drawing
+ def draw_point(canvas, point)
+ canvas[point.x][point.y] = true
+ end
+
+ def draw_line(canvas, line)
end
end
end

Кристиян обнови решението на 22.12.2013 12:00 (преди почти 11 години)

module Graphics
class Point
attr_reader :x, :y
def initialize(x, y)
@x, @y = x, y
end
def ==(other_point)
[@x, @y] == [other_point.x, other_point.y]
end
alias :eql? :==
def hash()
"x#{@x}y#{@y}"
end
end
class Line
attr_reader :from, :to
def initialize(point_one, point_two)
if point_one == point_two then
@from, @to = point_one, point_one
else
@from = get_from_point(point_one, point_two)
- @to = ([point_one, point_two] - [@from])[0]
+ @to = point_one == @from ? point_two : point_one
end
end
def ==(other_line)
[@from, @to] == [other_line.from, other_line.to]
end
alias :eql? :==
def hash()
#TODO
end
+
+ def get_from_point(first_point, second_point)
+ if first_point.x != second_point.x then
+ first_point.x < second_point.x ? first_point : second_point
+ elsif first_point.y != second_point.y then
+ first_point.y < second_point.y ? first_point : second_point
+ end
+ end
end
class Rectangle
attr_reader :left, :right
def initialize(point_one, point_two)
if point_one == point_two then
@left, @right = point_one, point_onew
else
@left = get_from_point(point_one, point_two)
@right = ([point_one, point_tow] - [@left])[0]
end
end
def top_left
(right.y - left.y) < 0 ? Point.new(left.x, right.y) : left
end
def top_right
(right.y - left.y) > 0 ? Point.new(left.y, right.x) : right
end
def bottom_left
(right.y - left.y) > 0 ? Point.new(left.x, right.y) : left
end
def bottom_right
(right.y - left.y) < 0 ? Point.new(right.x, left.y) : right
end
def ==(other_rectangle)
top_left == other.top_left and bottom_right == other.bottom_right
end
alias :eql? :==
def hash()
#TODO
end
end
+ module Drawing
+ def self.draw_line(canvas, line)
+ canvas[line.from.y][line.from.x]=true if (line.from == line.to)
+ way = (line.to.x - line.from.x) >= (line.to.y - line.from.y) ? 'x' : 'y'
+ draw_first_type(canvas, line, way) if (way == 'x' and line.from.y <= line.to.y)
+ end
+ #----------------------------------- first type line --------------------------------
+ def self.draw_first_type(canvas, line, way)
+ dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y)
+ length = way == 'x' ? dx : dy
+ first_type(line.from.x, line.from.y, (length-2), dx, dy, canvas)
+ end
+
+ def self.first_type(x, y, length, a, b, canvas)
+ f = ([a, b].max)/2.0
+ for i in (0..(length))
+ if((f - [a, b].min) >= 0) then canvas[y][x+=1], f = true, f - [a, b].min
+ else canvas[y += 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
+ end
+ end
+ end
+ #----------------------------------- first type line --------------------------------
+ end
+
class Canvas
- attr_reader :width, :height
+ attr_reader :width, :height, :canvas
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
- @canvas = Array.new(canvas_width){Array.new(canvas_height, false)}
+ @canvas = Array.new(canvas_height){Array.new(canvas_width, false)}
end
def set_pixel(x, y)
@canvas[x][y] = true
end
def pixel_at?(x, y)
@canvas[x][y] == true
end
def draw(figure)
- Drawing::draw_point(@canvas, figure) if (figure.instance_of? Point)
+ set_pixel(figure.x, figure.y) if (figure.instance_of? Point)
Drawing::draw_line(@canvas, figure) if (figure.instance_of? Line)
end
def render_as(rendering_type)
- end
- end
-
- def get_from_point(first_point, second_point)
- if first_point.x != second_point.x then
- first_point.x < second_point.x ? first_point : second_point
- elsif first_point.y != second_point.y then
- first_point.y < second_point.y ? first_point : second_point
- end
- end
-
- module Drawing
- def draw_point(canvas, point)
- canvas[point.x][point.y] = true
- end
-
- def draw_line(canvas, line)
end
end
end

Кристиян обнови решението на 22.12.2013 14:56 (преди почти 11 години)

module Graphics
class Point
attr_reader :x, :y
def initialize(x, y)
@x, @y = x, y
end
def ==(other_point)
[@x, @y] == [other_point.x, other_point.y]
end
alias :eql? :==
def hash()
"x#{@x}y#{@y}"
end
end
class Line
attr_reader :from, :to
def initialize(point_one, point_two)
if point_one == point_two then
@from, @to = point_one, point_one
else
@from = get_from_point(point_one, point_two)
@to = point_one == @from ? point_two : point_one
end
end
def ==(other_line)
[@from, @to] == [other_line.from, other_line.to]
end
alias :eql? :==
def hash()
#TODO
end
def get_from_point(first_point, second_point)
if first_point.x != second_point.x then
first_point.x < second_point.x ? first_point : second_point
elsif first_point.y != second_point.y then
first_point.y < second_point.y ? first_point : second_point
end
end
end
class Rectangle
attr_reader :left, :right
def initialize(point_one, point_two)
if point_one == point_two then
@left, @right = point_one, point_onew
else
@left = get_from_point(point_one, point_two)
@right = ([point_one, point_tow] - [@left])[0]
end
end
def top_left
(right.y - left.y) < 0 ? Point.new(left.x, right.y) : left
end
def top_right
(right.y - left.y) > 0 ? Point.new(left.y, right.x) : right
end
def bottom_left
(right.y - left.y) > 0 ? Point.new(left.x, right.y) : left
end
def bottom_right
(right.y - left.y) < 0 ? Point.new(right.x, left.y) : right
end
def ==(other_rectangle)
top_left == other.top_left and bottom_right == other.bottom_right
end
alias :eql? :==
def hash()
#TODO
end
end
module Drawing
def self.draw_line(canvas, line)
- canvas[line.from.y][line.from.x]=true if (line.from == line.to)
- way = (line.to.x - line.from.x) >= (line.to.y - line.from.y) ? 'x' : 'y'
- draw_first_type(canvas, line, way) if (way == 'x' and line.from.y <= line.to.y)
+ canvas[line.from.y][line.from.x]=true if (line.from == line.to)
+ way = (line.to.x - line.from.x).abs >= (line.to.y - line.from.y).abs ? 'x' : 'y'
+ draw_first_type(canvas, line, way) if (way == 'x' and line.from.y <= line.to.y)
+ draw_second_type(canvas, line, way) if (way =='y' and line.from.y <= line.to.y)
+ draw_third_type(canvas, line, way) if (way == 'x' and line.from.y > line.to.y)
+ draw_fourth_type(canvas, line, way) if (way == 'y' and line.from.y > line.to.y)
end
- #----------------------------------- first type line --------------------------------
+
def self.draw_first_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y)
length = way == 'x' ? dx : dy
first_type(line.from.x, line.from.y, (length-2), dx, dy, canvas)
end
def self.first_type(x, y, length, a, b, canvas)
f = ([a, b].max)/2.0
for i in (0..(length))
if((f - [a, b].min) >= 0) then canvas[y][x+=1], f = true, f - [a, b].min
else canvas[y += 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
- #----------------------------------- first type line --------------------------------
+
+ def self.draw_second_type(canvas, line, way)
+ dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y)
+ length = way == 'x' ? dx : dy
+ second_type(line.from.x, line.from.y, (length-2), dx, dy, canvas)
+ end
+
+ def self.second_type(x, y, length, a, b, canvas)
+ f = ([a, b].max)/2.0
+ for i in (0..(length))
+ if((f - [a, b].min) >= 0) then canvas[y+=1][x], f = true, f - [a, b].min
+ else canvas[y += 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
+ end
+ end
+ end
+
+ def self.draw_third_type(canvas, line, way)
+ dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y).abs
+ length = way == 'x' ? dx : dy
+ third_type(line.from.x, line.from.y, (length-2), dx, dy, canvas)
+ end
+
+ def self.third_type(x, y, length, a, b, canvas)
+ f = ([a, b].max)/2.0
+ for i in (0..(length))
+ if((f - [a, b].min) >= 0) then canvas[y][x+=1], f = true, f - [a, b].min
+ else canvas[y -= 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
+ end
+ end
+ end
+
+ def self.draw_fourth_type(canvas, line, way)
+ dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y).abs
+ length = way == 'x' ? dx : dy
+ fourth_type(line.from.x, line.from.y, (length-2), dx, dy, canvas)
+ end
+
+ def self.fourth_type(x, y, length, a, b, canvas)
+ f = ([a, b].max)/2.0
+ for i in (0..(length))
+ if((f - [a, b].min) >= 0) then canvas[y-=1][x], f = true, f - [a, b].min
+ else canvas[y -= 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
+ end
+ end
+ end
end
class Canvas
attr_reader :width, :height, :canvas
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
- @canvas = Array.new(canvas_height){Array.new(canvas_width, false)}
+ @canvas = Array.new(canvas_height){Array.new(canvas_width, false)}
end
def set_pixel(x, y)
@canvas[x][y] = true
end
def pixel_at?(x, y)
@canvas[x][y] == true
end
def draw(figure)
set_pixel(figure.x, figure.y) if (figure.instance_of? Point)
Drawing::draw_line(@canvas, figure) if (figure.instance_of? Line)
end
def render_as(rendering_type)
end
end
end

Кристиян обнови решението на 22.12.2013 17:53 (преди почти 11 години)

module Graphics
class Point
attr_reader :x, :y
def initialize(x, y)
@x, @y = x, y
end
def ==(other_point)
[@x, @y] == [other_point.x, other_point.y]
end
alias :eql? :==
def hash()
"x#{@x}y#{@y}"
end
end
class Line
attr_reader :from, :to
def initialize(point_one, point_two)
if point_one == point_two then
@from, @to = point_one, point_one
else
@from = get_from_point(point_one, point_two)
@to = point_one == @from ? point_two : point_one
end
end
def ==(other_line)
[@from, @to] == [other_line.from, other_line.to]
end
alias :eql? :==
def hash()
#TODO
end
def get_from_point(first_point, second_point)
if first_point.x != second_point.x then
first_point.x < second_point.x ? first_point : second_point
elsif first_point.y != second_point.y then
first_point.y < second_point.y ? first_point : second_point
end
end
end
class Rectangle
attr_reader :left, :right
def initialize(point_one, point_two)
if point_one == point_two then
@left, @right = point_one, point_onew
else
@left = get_from_point(point_one, point_two)
- @right = ([point_one, point_tow] - [@left])[0]
+ @right = point_one == @left ? point_two : point_one
end
end
def top_left
(right.y - left.y) < 0 ? Point.new(left.x, right.y) : left
end
def top_right
- (right.y - left.y) > 0 ? Point.new(left.y, right.x) : right
+ (right.y - left.y) > 0 ? Point.new(right.x, left.y) : right
end
def bottom_left
(right.y - left.y) > 0 ? Point.new(left.x, right.y) : left
end
def bottom_right
(right.y - left.y) < 0 ? Point.new(right.x, left.y) : right
end
- def ==(other_rectangle)
- top_left == other.top_left and bottom_right == other.bottom_right
+ def ==(another_one)
+ top_left == another_one.top_left and bottom_right == another_one.bottom_right
end
alias :eql? :==
def hash()
#TODO
end
+
+ def get_from_point(first_point, second_point)
+ if first_point.x != second_point.x then
+ first_point.x < second_point.x ? first_point : second_point
+ elsif first_point.y != second_point.y then
+ first_point.y < second_point.y ? first_point : second_point
+ end
+ end
end
module Drawing
+ def self.draw_rectangle(canvas, rectangle)
+ draw_line(canvas, Line.new(rectangle.top_left, rectangle.top_right))
+ draw_line(canvas, Line.new(rectangle.top_left, rectangle.bottom_left))
+ draw_line(canvas, Line.new(rectangle.bottom_left, rectangle.bottom_right))
+ draw_line(canvas, Line.new(rectangle.top_right, rectangle.bottom_right))
+ end
+
def self.draw_line(canvas, line)
canvas[line.from.y][line.from.x]=true if (line.from == line.to)
- way = (line.to.x - line.from.x).abs >= (line.to.y - line.from.y).abs ? 'x' : 'y'
- draw_first_type(canvas, line, way) if (way == 'x' and line.from.y <= line.to.y)
+ way = (line.to.x - line.from.x).abs >= (line.to.y - line.from.y).abs ? 'x' : 'y'
+ draw_first_type(canvas, line, way) if (way == 'x' and line.from.y <= line.to.y)
draw_second_type(canvas, line, way) if (way =='y' and line.from.y <= line.to.y)
draw_third_type(canvas, line, way) if (way == 'x' and line.from.y > line.to.y)
draw_fourth_type(canvas, line, way) if (way == 'y' and line.from.y > line.to.y)
end
def self.draw_first_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y)
length = way == 'x' ? dx : dy
- first_type(line.from.x, line.from.y, (length-2), dx, dy, canvas)
+ first_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.first_type(x, y, length, a, b, canvas)
- f = ([a, b].max)/2.0
+ f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
- if((f - [a, b].min) >= 0) then canvas[y][x+=1], f = true, f - [a, b].min
+ if((f - [a, b].min) > 0) then canvas[y][x+=1], f = true, f - [a, b].min
else canvas[y += 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_second_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y)
length = way == 'x' ? dx : dy
- second_type(line.from.x, line.from.y, (length-2), dx, dy, canvas)
+ second_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.second_type(x, y, length, a, b, canvas)
- f = ([a, b].max)/2.0
+ f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
- if((f - [a, b].min) >= 0) then canvas[y+=1][x], f = true, f - [a, b].min
+ if((f - [a, b].min) > 0) then canvas[y+=1][x], f = true, (f - [a, b].min)
else canvas[y += 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_third_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y).abs
length = way == 'x' ? dx : dy
- third_type(line.from.x, line.from.y, (length-2), dx, dy, canvas)
+ third_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.third_type(x, y, length, a, b, canvas)
- f = ([a, b].max)/2.0
+ f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
- if((f - [a, b].min) >= 0) then canvas[y][x+=1], f = true, f - [a, b].min
+ if((f - [a, b].min) > 0) then canvas[y][x+=1], f = true, f - [a, b].min
else canvas[y -= 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_fourth_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y).abs
length = way == 'x' ? dx : dy
- fourth_type(line.from.x, line.from.y, (length-2), dx, dy, canvas)
+ fourth_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.fourth_type(x, y, length, a, b, canvas)
- f = ([a, b].max)/2.0
+ f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
- if((f - [a, b].min) >= 0) then canvas[y-=1][x], f = true, f - [a, b].min
+ if((f - [a, b].min) > 0) then canvas[y-=1][x], f = true, f - [a, b].min
else canvas[y -= 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
end
class Canvas
attr_reader :width, :height, :canvas
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
@canvas = Array.new(canvas_height){Array.new(canvas_width, false)}
end
def set_pixel(x, y)
@canvas[x][y] = true
end
def pixel_at?(x, y)
@canvas[x][y] == true
end
def draw(figure)
- set_pixel(figure.x, figure.y) if (figure.instance_of? Point)
+ set_pixel(figure.y, figure.x) if (figure.instance_of? Point)
Drawing::draw_line(@canvas, figure) if (figure.instance_of? Line)
+ Drawing::draw_rectangle(@canvas, figure) if (figure.instance_of? Rectangle)
end
def render_as(rendering_type)
end
end
end

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

module Graphics
class Point
attr_reader :x, :y
def initialize(x, y)
@x, @y = x, y
end
def ==(other_point)
[@x, @y] == [other_point.x, other_point.y]
end
alias :eql? :==
def hash()
"x#{@x}y#{@y}"
end
end
class Line
attr_reader :from, :to
def initialize(point_one, point_two)
if point_one == point_two then
@from, @to = point_one, point_one
else
@from = get_from_point(point_one, point_two)
@to = point_one == @from ? point_two : point_one
end
end
def ==(other_line)
[@from, @to] == [other_line.from, other_line.to]
end
alias :eql? :==
def hash()
#TODO
end
def get_from_point(first_point, second_point)
if first_point.x != second_point.x then
first_point.x < second_point.x ? first_point : second_point
elsif first_point.y != second_point.y then
first_point.y < second_point.y ? first_point : second_point
end
end
end
class Rectangle
attr_reader :left, :right
def initialize(point_one, point_two)
if point_one == point_two then
@left, @right = point_one, point_onew
else
@left = get_from_point(point_one, point_two)
@right = point_one == @left ? point_two : point_one
end
end
def top_left
(right.y - left.y) < 0 ? Point.new(left.x, right.y) : left
end
def top_right
(right.y - left.y) > 0 ? Point.new(right.x, left.y) : right
end
def bottom_left
(right.y - left.y) > 0 ? Point.new(left.x, right.y) : left
end
def bottom_right
(right.y - left.y) < 0 ? Point.new(right.x, left.y) : right
end
def ==(another_one)
top_left == another_one.top_left and bottom_right == another_one.bottom_right
end
alias :eql? :==
def hash()
#TODO
end
def get_from_point(first_point, second_point)
if first_point.x != second_point.x then
first_point.x < second_point.x ? first_point : second_point
elsif first_point.y != second_point.y then
first_point.y < second_point.y ? first_point : second_point
end
end
end
+ module Renderers
+ class Ascii
+ def self.ascii_render(canvas)
+ canvas.each {|a| puts a.join.gsub('true','@').gsub('false','-')}
+ end
+ end
+
+ HTML_BEGIN = "<!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_END = " </div>
+</body>
+</html>
+"
+ class Html
+ def self.html_render(canvas)
+ puts Graphics::Renderers::HTML_BEGIN
+ canvas.each_with_index do |row, index|
+ Graphics::Renderers::Html.html_render_helper(row, index, canvas)
+ end
+ puts Graphics::Renderers::HTML_END
+ end
+
+ def self.html_render_helper(row, index, canvas)
+ if index == canvas.size - 1 then
+ puts row.join.gsub('true','<b></b>').gsub('false','<i></i>')
+ else
+ puts row.join.gsub('true','<b></b>').gsub('false','<i></i>') + '<br>'
+ end
+ end
+ end
+ end
+
module Drawing
def self.draw_rectangle(canvas, rectangle)
draw_line(canvas, Line.new(rectangle.top_left, rectangle.top_right))
draw_line(canvas, Line.new(rectangle.top_left, rectangle.bottom_left))
draw_line(canvas, Line.new(rectangle.bottom_left, rectangle.bottom_right))
draw_line(canvas, Line.new(rectangle.top_right, rectangle.bottom_right))
end
def self.draw_line(canvas, line)
canvas[line.from.y][line.from.x]=true if (line.from == line.to)
way = (line.to.x - line.from.x).abs >= (line.to.y - line.from.y).abs ? 'x' : 'y'
draw_first_type(canvas, line, way) if (way == 'x' and line.from.y <= line.to.y)
draw_second_type(canvas, line, way) if (way =='y' and line.from.y <= line.to.y)
draw_third_type(canvas, line, way) if (way == 'x' and line.from.y > line.to.y)
draw_fourth_type(canvas, line, way) if (way == 'y' and line.from.y > line.to.y)
end
def self.draw_first_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y)
length = way == 'x' ? dx : dy
first_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.first_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y][x+=1], f = true, f - [a, b].min
else canvas[y += 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_second_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y)
length = way == 'x' ? dx : dy
second_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.second_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y+=1][x], f = true, (f - [a, b].min)
else canvas[y += 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_third_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y).abs
length = way == 'x' ? dx : dy
third_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.third_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y][x+=1], f = true, f - [a, b].min
else canvas[y -= 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_fourth_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y).abs
length = way == 'x' ? dx : dy
fourth_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.fourth_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y-=1][x], f = true, f - [a, b].min
else canvas[y -= 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
end
class Canvas
attr_reader :width, :height, :canvas
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
@canvas = Array.new(canvas_height){Array.new(canvas_width, false)}
end
def set_pixel(x, y)
@canvas[x][y] = true
end
def pixel_at?(x, y)
@canvas[x][y] == true
end
def draw(figure)
set_pixel(figure.y, figure.x) if (figure.instance_of? Point)
Drawing::draw_line(@canvas, figure) if (figure.instance_of? Line)
Drawing::draw_rectangle(@canvas, figure) if (figure.instance_of? Rectangle)
end
- def render_as(rendering_type)
-
+ def render_as(renderer)
+ renderer.html_render(@canvas) if renderer == Graphics::Renderers::Html
+ renderer.ascii_render(@canvas) if renderer == Graphics::Renderers::Ascii
end
end
end

Кристиян обнови решението на 22.12.2013 21:59 (преди почти 11 години)

module Graphics
class Point
attr_reader :x, :y
def initialize(x, y)
@x, @y = x, y
end
def ==(other_point)
[@x, @y] == [other_point.x, other_point.y]
end
alias :eql? :==
def hash()
"x#{@x}y#{@y}"
end
end
class Line
attr_reader :from, :to
def initialize(point_one, point_two)
if point_one == point_two then
@from, @to = point_one, point_one
else
@from = get_from_point(point_one, point_two)
@to = point_one == @from ? point_two : point_one
end
end
def ==(other_line)
[@from, @to] == [other_line.from, other_line.to]
end
alias :eql? :==
def hash()
#TODO
end
def get_from_point(first_point, second_point)
if first_point.x != second_point.x then
first_point.x < second_point.x ? first_point : second_point
elsif first_point.y != second_point.y then
first_point.y < second_point.y ? first_point : second_point
end
end
end
class Rectangle
attr_reader :left, :right
def initialize(point_one, point_two)
if point_one == point_two then
@left, @right = point_one, point_onew
else
@left = get_from_point(point_one, point_two)
@right = point_one == @left ? point_two : point_one
end
end
def top_left
(right.y - left.y) < 0 ? Point.new(left.x, right.y) : left
end
def top_right
(right.y - left.y) > 0 ? Point.new(right.x, left.y) : right
end
def bottom_left
(right.y - left.y) > 0 ? Point.new(left.x, right.y) : left
end
def bottom_right
(right.y - left.y) < 0 ? Point.new(right.x, left.y) : right
end
def ==(another_one)
top_left == another_one.top_left and bottom_right == another_one.bottom_right
end
alias :eql? :==
def hash()
#TODO
end
def get_from_point(first_point, second_point)
if first_point.x != second_point.x then
first_point.x < second_point.x ? first_point : second_point
elsif first_point.y != second_point.y then
first_point.y < second_point.y ? first_point : second_point
end
end
end
module Renderers
class Ascii
def self.ascii_render(canvas)
- canvas.each {|a| puts a.join.gsub('true','@').gsub('false','-')}
+ a= canvas.map do |arr|
+ Graphics::Renderers::Ascii.ascii_render_helper(arr)
+ end.join("\n")
end
+
+ def self.ascii_render_helper(array)
+ array.join.gsub(/(true|false)/) do |x|
+ if x =~ /false/ then '-'
+ elsif x =~ /true/ then '@'
+ end
+ end
+ end
end
HTML_BEGIN = "<!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_END = " </div>
</body>
-</html>
-"
+</html>"
class Html
def self.html_render(canvas)
puts Graphics::Renderers::HTML_BEGIN
canvas.each_with_index do |row, index|
Graphics::Renderers::Html.html_render_helper(row, index, canvas)
end
puts Graphics::Renderers::HTML_END
end
def self.html_render_helper(row, index, canvas)
if index == canvas.size - 1 then
puts row.join.gsub('true','<b></b>').gsub('false','<i></i>')
else
puts row.join.gsub('true','<b></b>').gsub('false','<i></i>') + '<br>'
end
end
end
end
module Drawing
def self.draw_rectangle(canvas, rectangle)
draw_line(canvas, Line.new(rectangle.top_left, rectangle.top_right))
draw_line(canvas, Line.new(rectangle.top_left, rectangle.bottom_left))
draw_line(canvas, Line.new(rectangle.bottom_left, rectangle.bottom_right))
draw_line(canvas, Line.new(rectangle.top_right, rectangle.bottom_right))
end
def self.draw_line(canvas, line)
canvas[line.from.y][line.from.x]=true if (line.from == line.to)
way = (line.to.x - line.from.x).abs >= (line.to.y - line.from.y).abs ? 'x' : 'y'
draw_first_type(canvas, line, way) if (way == 'x' and line.from.y <= line.to.y)
draw_second_type(canvas, line, way) if (way =='y' and line.from.y <= line.to.y)
draw_third_type(canvas, line, way) if (way == 'x' and line.from.y > line.to.y)
draw_fourth_type(canvas, line, way) if (way == 'y' and line.from.y > line.to.y)
end
def self.draw_first_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y)
length = way == 'x' ? dx : dy
first_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.first_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y][x+=1], f = true, f - [a, b].min
else canvas[y += 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_second_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y)
length = way == 'x' ? dx : dy
second_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.second_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y+=1][x], f = true, (f - [a, b].min)
else canvas[y += 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_third_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y).abs
length = way == 'x' ? dx : dy
third_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.third_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y][x+=1], f = true, f - [a, b].min
else canvas[y -= 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_fourth_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y).abs
length = way == 'x' ? dx : dy
fourth_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.fourth_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y-=1][x], f = true, f - [a, b].min
else canvas[y -= 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
end
class Canvas
attr_reader :width, :height, :canvas
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
@canvas = Array.new(canvas_height){Array.new(canvas_width, false)}
end
def set_pixel(x, y)
@canvas[x][y] = true
end
def pixel_at?(x, y)
@canvas[x][y] == true
end
def draw(figure)
set_pixel(figure.y, figure.x) if (figure.instance_of? Point)
Drawing::draw_line(@canvas, figure) if (figure.instance_of? Line)
Drawing::draw_rectangle(@canvas, figure) if (figure.instance_of? Rectangle)
end
def render_as(renderer)
renderer.html_render(@canvas) if renderer == Graphics::Renderers::Html
renderer.ascii_render(@canvas) if renderer == Graphics::Renderers::Ascii
end
end
end

Кристиян обнови решението на 22.12.2013 22:23 (преди почти 11 години)

module Graphics
class Point
attr_reader :x, :y
def initialize(x, y)
@x, @y = x, y
end
def ==(other_point)
[@x, @y] == [other_point.x, other_point.y]
end
alias :eql? :==
def hash()
- "x#{@x}y#{@y}"
+ "x#{@x}y#{@y}".hash()
end
end
class Line
attr_reader :from, :to
def initialize(point_one, point_two)
if point_one == point_two then
@from, @to = point_one, point_one
else
@from = get_from_point(point_one, point_two)
@to = point_one == @from ? point_two : point_one
end
end
def ==(other_line)
[@from, @to] == [other_line.from, other_line.to]
end
alias :eql? :==
def hash()
- #TODO
+ "x0#{@from.x}y0#{@from.y}_x1#{@to.x}y1#{@to.y}".hash()
end
def get_from_point(first_point, second_point)
if first_point.x != second_point.x then
first_point.x < second_point.x ? first_point : second_point
elsif first_point.y != second_point.y then
first_point.y < second_point.y ? first_point : second_point
end
end
end
class Rectangle
attr_reader :left, :right
def initialize(point_one, point_two)
if point_one == point_two then
@left, @right = point_one, point_onew
else
@left = get_from_point(point_one, point_two)
@right = point_one == @left ? point_two : point_one
end
end
def top_left
(right.y - left.y) < 0 ? Point.new(left.x, right.y) : left
end
def top_right
(right.y - left.y) > 0 ? Point.new(right.x, left.y) : right
end
def bottom_left
(right.y - left.y) > 0 ? Point.new(left.x, right.y) : left
end
def bottom_right
(right.y - left.y) < 0 ? Point.new(right.x, left.y) : right
end
def ==(another_one)
top_left == another_one.top_left and bottom_right == another_one.bottom_right
end
alias :eql? :==
def hash()
- #TODO
+ "x0#{left.x}y0#{left.y}_x1#{right.x}y1#{right.y}".hash()
end
def get_from_point(first_point, second_point)
if first_point.x != second_point.x then
first_point.x < second_point.x ? first_point : second_point
elsif first_point.y != second_point.y then
first_point.y < second_point.y ? first_point : second_point
end
end
end
module Renderers
class Ascii
def self.ascii_render(canvas)
a= canvas.map do |arr|
Graphics::Renderers::Ascii.ascii_render_helper(arr)
end.join("\n")
end
def self.ascii_render_helper(array)
array.join.gsub(/(true|false)/) do |x|
if x =~ /false/ then '-'
elsif x =~ /true/ then '@'
end
end
end
end
HTML_BEGIN = "<!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_END = " </div>
</body>
</html>"
class Html
def self.html_render(canvas)
puts Graphics::Renderers::HTML_BEGIN
canvas.each_with_index do |row, index|
Graphics::Renderers::Html.html_render_helper(row, index, canvas)
end
puts Graphics::Renderers::HTML_END
end
def self.html_render_helper(row, index, canvas)
if index == canvas.size - 1 then
puts row.join.gsub('true','<b></b>').gsub('false','<i></i>')
else
puts row.join.gsub('true','<b></b>').gsub('false','<i></i>') + '<br>'
end
end
end
end
module Drawing
def self.draw_rectangle(canvas, rectangle)
draw_line(canvas, Line.new(rectangle.top_left, rectangle.top_right))
draw_line(canvas, Line.new(rectangle.top_left, rectangle.bottom_left))
draw_line(canvas, Line.new(rectangle.bottom_left, rectangle.bottom_right))
draw_line(canvas, Line.new(rectangle.top_right, rectangle.bottom_right))
end
def self.draw_line(canvas, line)
canvas[line.from.y][line.from.x]=true if (line.from == line.to)
way = (line.to.x - line.from.x).abs >= (line.to.y - line.from.y).abs ? 'x' : 'y'
draw_first_type(canvas, line, way) if (way == 'x' and line.from.y <= line.to.y)
draw_second_type(canvas, line, way) if (way =='y' and line.from.y <= line.to.y)
draw_third_type(canvas, line, way) if (way == 'x' and line.from.y > line.to.y)
draw_fourth_type(canvas, line, way) if (way == 'y' and line.from.y > line.to.y)
end
def self.draw_first_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y)
length = way == 'x' ? dx : dy
first_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.first_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y][x+=1], f = true, f - [a, b].min
else canvas[y += 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_second_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y)
length = way == 'x' ? dx : dy
second_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.second_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y+=1][x], f = true, (f - [a, b].min)
else canvas[y += 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_third_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y).abs
length = way == 'x' ? dx : dy
third_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.third_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y][x+=1], f = true, f - [a, b].min
else canvas[y -= 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_fourth_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y).abs
length = way == 'x' ? dx : dy
fourth_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.fourth_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y-=1][x], f = true, f - [a, b].min
else canvas[y -= 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
end
class Canvas
attr_reader :width, :height, :canvas
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
@canvas = Array.new(canvas_height){Array.new(canvas_width, false)}
end
def set_pixel(x, y)
@canvas[x][y] = true
end
def pixel_at?(x, y)
@canvas[x][y] == true
end
def draw(figure)
set_pixel(figure.y, figure.x) if (figure.instance_of? Point)
Drawing::draw_line(@canvas, figure) if (figure.instance_of? Line)
Drawing::draw_rectangle(@canvas, figure) if (figure.instance_of? Rectangle)
end
def render_as(renderer)
renderer.html_render(@canvas) if renderer == Graphics::Renderers::Html
renderer.ascii_render(@canvas) if renderer == Graphics::Renderers::Ascii
end
end
end

Кристиян обнови решението на 22.12.2013 22:36 (преди почти 11 години)

module Graphics
class Point
attr_reader :x, :y
def initialize(x, y)
@x, @y = x, y
end
def ==(other_point)
[@x, @y] == [other_point.x, other_point.y]
end
alias :eql? :==
def hash()
"x#{@x}y#{@y}".hash()
end
end
class Line
attr_reader :from, :to
def initialize(point_one, point_two)
if point_one == point_two then
@from, @to = point_one, point_one
else
@from = get_from_point(point_one, point_two)
@to = point_one == @from ? point_two : point_one
end
end
def ==(other_line)
[@from, @to] == [other_line.from, other_line.to]
end
alias :eql? :==
def hash()
"x0#{@from.x}y0#{@from.y}_x1#{@to.x}y1#{@to.y}".hash()
end
def get_from_point(first_point, second_point)
if first_point.x != second_point.x then
first_point.x < second_point.x ? first_point : second_point
elsif first_point.y != second_point.y then
first_point.y < second_point.y ? first_point : second_point
end
end
end
class Rectangle
attr_reader :left, :right
def initialize(point_one, point_two)
if point_one == point_two then
@left, @right = point_one, point_onew
else
@left = get_from_point(point_one, point_two)
@right = point_one == @left ? point_two : point_one
end
end
def top_left
(right.y - left.y) < 0 ? Point.new(left.x, right.y) : left
end
def top_right
(right.y - left.y) > 0 ? Point.new(right.x, left.y) : right
end
def bottom_left
(right.y - left.y) > 0 ? Point.new(left.x, right.y) : left
end
def bottom_right
(right.y - left.y) < 0 ? Point.new(right.x, left.y) : right
end
def ==(another_one)
- top_left == another_one.top_left and bottom_right == another_one.bottom_right
+ @left == another_one.left and @right == another_one.right
end
alias :eql? :==
def hash()
"x0#{left.x}y0#{left.y}_x1#{right.x}y1#{right.y}".hash()
end
def get_from_point(first_point, second_point)
if first_point.x != second_point.x then
first_point.x < second_point.x ? first_point : second_point
elsif first_point.y != second_point.y then
first_point.y < second_point.y ? first_point : second_point
end
end
end
module Renderers
class Ascii
def self.ascii_render(canvas)
a= canvas.map do |arr|
Graphics::Renderers::Ascii.ascii_render_helper(arr)
end.join("\n")
end
def self.ascii_render_helper(array)
array.join.gsub(/(true|false)/) do |x|
if x =~ /false/ then '-'
elsif x =~ /true/ then '@'
end
end
end
end
HTML_BEGIN = "<!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_END = " </div>
</body>
</html>"
class Html
def self.html_render(canvas)
puts Graphics::Renderers::HTML_BEGIN
canvas.each_with_index do |row, index|
Graphics::Renderers::Html.html_render_helper(row, index, canvas)
end
puts Graphics::Renderers::HTML_END
end
def self.html_render_helper(row, index, canvas)
if index == canvas.size - 1 then
puts row.join.gsub('true','<b></b>').gsub('false','<i></i>')
else
puts row.join.gsub('true','<b></b>').gsub('false','<i></i>') + '<br>'
end
end
end
end
module Drawing
def self.draw_rectangle(canvas, rectangle)
draw_line(canvas, Line.new(rectangle.top_left, rectangle.top_right))
draw_line(canvas, Line.new(rectangle.top_left, rectangle.bottom_left))
draw_line(canvas, Line.new(rectangle.bottom_left, rectangle.bottom_right))
draw_line(canvas, Line.new(rectangle.top_right, rectangle.bottom_right))
end
def self.draw_line(canvas, line)
canvas[line.from.y][line.from.x]=true if (line.from == line.to)
way = (line.to.x - line.from.x).abs >= (line.to.y - line.from.y).abs ? 'x' : 'y'
draw_first_type(canvas, line, way) if (way == 'x' and line.from.y <= line.to.y)
draw_second_type(canvas, line, way) if (way =='y' and line.from.y <= line.to.y)
draw_third_type(canvas, line, way) if (way == 'x' and line.from.y > line.to.y)
draw_fourth_type(canvas, line, way) if (way == 'y' and line.from.y > line.to.y)
end
def self.draw_first_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y)
length = way == 'x' ? dx : dy
first_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.first_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y][x+=1], f = true, f - [a, b].min
else canvas[y += 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_second_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y)
length = way == 'x' ? dx : dy
second_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.second_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y+=1][x], f = true, (f - [a, b].min)
else canvas[y += 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_third_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y).abs
length = way == 'x' ? dx : dy
third_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.third_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y][x+=1], f = true, f - [a, b].min
else canvas[y -= 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
def self.draw_fourth_type(canvas, line, way)
dx, dy = (line.to.x - line.from.x), (line.to.y - line.from.y).abs
length = way == 'x' ? dx : dy
fourth_type(line.from.x, line.from.y, (length-1), dx, dy, canvas)
end
def self.fourth_type(x, y, length, a, b, canvas)
f, canvas[y][x] = ([a, b].max)/2.0, true
for i in (0..(length))
if((f - [a, b].min) > 0) then canvas[y-=1][x], f = true, f - [a, b].min
else canvas[y -= 1][x += 1], f = true, (f - [a, b].min) + [a, b].max
end
end
end
end
class Canvas
attr_reader :width, :height, :canvas
def initialize(canvas_width, canvas_height)
@width, @height = canvas_width, canvas_height
@canvas = Array.new(canvas_height){Array.new(canvas_width, false)}
end
def set_pixel(x, y)
@canvas[x][y] = true
end
def pixel_at?(x, y)
@canvas[x][y] == true
end
def draw(figure)
set_pixel(figure.y, figure.x) if (figure.instance_of? Point)
Drawing::draw_line(@canvas, figure) if (figure.instance_of? Line)
Drawing::draw_rectangle(@canvas, figure) if (figure.instance_of? Rectangle)
end
def render_as(renderer)
renderer.html_render(@canvas) if renderer == Graphics::Renderers::Html
renderer.ascii_render(@canvas) if renderer == Graphics::Renderers::Ascii
end
end
end

Принуден съм да ти отнема точка заради неспазване на стилови конвенции и идентация, въпреки, че те бях предупредил за тези неща:

  • Проблем с идентацията на блок на ред 25-26, ред 55-56.
  • Все още оставяш then като пишеш if. Не се слага, изпуска се. Виж предните ми коментари,
  • Не изпускаш скоби там, където трябва - дефинициите на методи, както и извикаване на методи без аргументи почти винаги се пишат без скоби; пример на ред 14, 36, 82, 83 и други.
  • Слагай по един празен ред след attr_* дефиниции за четимост.
  • array е лошо име за променлива (ред 103).
  • HTML_BEGIN/HTML_END трябва да се намират в Renderers::Html, а не директно в Renderers. Освен това е по-добре да се казват HTML_HEADER/HTML_FOOTER и щеше да ти е по-удобно да ползваш други кавички, например единични.
  • Не ми харесва че правиш gsub на true/false в render-методите. По-добре е да обходиш пискелите и да ги map-неш на стринговото им представяне и след това да join-неш резултата.
  • Виж как съм реализирал аз hash методите, макар че и твоят вариант ще работи.
  • Променливата a не е нито добре кръстена, нито има смисъл от нея (ред 98).
  • На ред 99 може да замениш Graphics::Renderers::Ascii със self, защото това е контекстът, в който се намираш. А знаеш, че в този случай self. може да се изпусне.
  • ascii_render_helper е лошо име. Например, нещо като render_line_to_string щеше да е по-подходящо.
  • Вероятно си видял, но имаш puts в кода, а това е грешно (в HTML renderer-а). Не трябва да вадиш неща на екрана, трябва да връщаш низове.
  • В Ruby няма практика да има думата get_ в името на методите; тази дума просто генерира шум и не носи информация (визирам get_from_point, което на свой ред също не ми се струва достатъчно ясно име, обясняващо какво прави този метод).
  • Модулът Drawing не ми харесва. Изглежда ми просто като купчина методи, нахвърляни там, за да се заобиколи ограничението за брой методи в клас.
  • Като викаш класов метод, не ползвай ::. Ползва се само ., т.е. Drawing.draw_line (ред 255-256).
  • Като дефинираш модул само с класови методи, по-удобно е да ползваш трик като extend self в началото на модула. Например така:

      module Drawing
        extend self
    
        def foo
        end
    
        def bar
        end
      end
    
      Drawing.foo
      Drawing.bar
    

    Другият вариант е да ползваш class << self. И в двата случая няма да има нужда да пишеш def self.foo за всеки метод в модула.

    Но, пак, модул, събиращ купчина методи в него рядко е добър дизайн. if-овете в Canvas#draw трябва да ти подсказват това. А и дефиниции на методи като def self.fourth_type(x, y, length, a, b, canvas) и тяло, което е сложно за разбиране също не помагат. Пак те реферирам към моето решение, за да видиш как съм адресирал аз този проблем с растеризацията и "чертането" върху пано.

  • На ред 254-256 скобите в if-овете не се слагат така. Ако трябва да сложиш скоби, сложи ги около извикването на метода, т.е. if figure.instance_of?(Line).
  • Трябва да има интервал след { и преди } na red 242.
  • Можеше да ползваш хеш, а не списък от списъци за вътрешното представяне на пано. Пак, виж как съм го направил аз. Така нямаше да има нужда да инициализираш паното, защото nil се интерпретира като "лъжа" в Ruby.
  • Проверката на ред 250 е тафтология. Може да е само @canvas[x][y].

Започнах да си оправям решението като оправям всички неща, които са описани в последният коментар, но ми остана само това, че когато правя Вероятно си видял, но имаш puts в кода, а това е грешно (в HTML renderer-а). Не трябва да вадиш неща на екрана, трябва да връщаш низове.

Преправих си canvas-a да не съдържа true/false стойности, а го преправих на 'true'/'false'(стринговите им еквиваленти). Но когато почна да премахвам puts-овете, просто нищо не се изобразява на екрана(а уж е изпълним код). А и от тестовете, които са били пускани върху решението не мога да преценя дали това е било проблем. Другите неща г/д разбрах как да ги оправя и какъв е бил проблемът. Просто исках да попитам дали ще е проблем ако го оставя така?

Първо, стрингове "true"/"false" вместо самите true/false е по-лоша идея. Под "няма нужда да ползваш true/false" имах предвид, че може да ползваш true/nil. Виж нашето решение, например.

Второ, проблем ще е, ако оставиш puts в кода си. Ако искаш нещо да се изведе на екрана, ще трябва да го направиш така:

canvas = Graphics::Canvas.new ...
puts canvas.render_as(Graphics::Renderers::Ascii)

Като имай предвид, че този код не трябва да го има в решението ти, това не е част от него. Това е примерен код, който "клиентът" на твоя код би използвал. Пак те реферирам към нашето решение, както и тези на колегите. Много е важно да ги прочетеш внимателно и да ги разбереш изцяло. Това е смисълът на тази задача. Ако там не разбираш нещо, питаш (или правиш списък с неясни неща и питаш). Днес може да ме разпиташ на лекцията, както и сряда.

Също, този коментар тук го видях по случайност. По-добре пиши в темата във форума, или ни пиши директно на нас.