class Rsirts::Renderer

Constants

DEFAULT_PATTERN_LENGTH
PATTERN_CHARS

Attributes

current_line[RW]
pattern[RW]
width[R]
zmap_lines[R]

Public Class Methods

new(zmap_lines) click to toggle source
# File lib/rsirts/renderer.rb, line 53
def initialize zmap_lines
  @zmap_lines = zmap_lines
  @width = zmap_lines[0].size
end

Public Instance Methods

generate() click to toggle source
# File lib/rsirts/renderer.rb, line 58
def generate
  zmap_lines.map do |line|
    translate_line line
  end
end

Private Instance Methods

append_pattern() click to toggle source
# File lib/rsirts/renderer.rb, line 93
def append_pattern
  pattern.size.times { current_line << pattern.next }
end
append_pattern_char() click to toggle source
# File lib/rsirts/renderer.rb, line 97
def append_pattern_char
  current_line << pattern.next
end
initialize_for_new_line() click to toggle source
# File lib/rsirts/renderer.rb, line 88
def initialize_for_new_line
  @current_line = nil.to_s
  @pattern = Ring.new(random_pattern DEFAULT_PATTERN_LENGTH)
end
lengthen_pattern(n) click to toggle source
# File lib/rsirts/renderer.rb, line 105
def lengthen_pattern n
  pattern.add random_pattern(n)
end
random_pattern(n) click to toggle source
# File lib/rsirts/renderer.rb, line 109
def random_pattern n
  chars = PATTERN_CHARS.dup
  [].tap do |pat|
    n.times do
      item = chars.sample
      pat << item
      chars.delete_at(chars.find_index(item))
    end
  end
end
shorten_pattern(n) click to toggle source
# File lib/rsirts/renderer.rb, line 101
def shorten_pattern n
  pattern.remove(n)
end
translate_line(line) click to toggle source
# File lib/rsirts/renderer.rb, line 69
def translate_line line
  initialize_for_new_line

  append_pattern

  old_z = nil
  line.each do |z; z_diff|
    z_diff = z - old_z if old_z
    begin
      shorten_pattern(z_diff) if z_diff > 0
      lengthen_pattern(-z_diff) if z_diff < 0
    end if z_diff
    append_pattern_char
    old_z = z
  end

  current_line
end