Class: Theseus::Formatters::PNG::Upsilon

Inherits:
Theseus::Formatters::PNG show all
Defined in:
lib/theseus/formatters/png/upsilon.rb

Overview

Renders a UpsilonMaze to a PNG canvas. Does not currently support the :wall_width option.

You will almost never access this class directly. Instead, use UpsilonMaze#to(:png, options) to return the raw PNG data directly.

Constant Summary

Constants inherited from Theseus::Formatters::PNG

ANY_E, ANY_N, ANY_S, ANY_W, DEFAULTS

Instance Attribute Summary

Attributes inherited from Theseus::Formatters::PNG

#options

Instance Method Summary collapse

Methods inherited from Theseus::Formatters::PNG

#clamp, #color_at, #fill_poly, #fill_rect, #line, #move, #to_blob

Constructor Details

#initialize(maze, options = {}) ⇒ Upsilon

Create and return a fully initialized PNG::Upsilon object, with the maze rendered. To get the maze data, call #to_blob.

See Theseus::Formatters::PNG for a list of all supported options.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/theseus/formatters/png/upsilon.rb', line 16

def initialize(maze, options={})
  super

  width = @options[:outer_padding] * 2 + (3 * maze.width + 1) * @options[:cell_size] / 4
  height = @options[:outer_padding] * 2 + (3 * maze.height + 1) * @options[:cell_size] / 4

  canvas = ChunkyPNG::Image.new(width, height, @options[:background])

  metrics = { size: @options[:cell_size] - @options[:cell_padding] * 2 }
  metrics[:s4] = metrics[:size] / 4.0
  metrics[:inc] = 3 * @options[:cell_size] / 4.0

  maze.height.times do |y|
    py = @options[:outer_padding] + y * metrics[:inc]
    maze.row_length(y).times do |x|
      cell = maze[x, y]
      next if cell == 0

      px = @options[:outer_padding] + x * metrics[:inc]

      if (y + x) % 2 == 0
        draw_octogon_cell(canvas, [x, y], px, py, cell, metrics)
      else
        draw_square_cell(canvas, [x, y], px, py, cell, metrics)
      end
    end
  end

  @blob = canvas.to_blob
end