class Vamp::Graphic::Transfer

Transfer dotter data into ASCII

Constants

BACKSLASH
BACKTICK
DOUBLEQUOTES
FULLSTOP
HASH
MINUS
PIPE
SINGLEQUOTE
SLASH
SPACE
STAR
UNDERSCORE

Attributes

char_height[R]
char_width[R]
context[R]
mapping[R]

Public Class Methods

new(context) click to toggle source
# File lib/vamp/graphic/transfer.rb, line 83
def initialize(context)
  @context = context
  @char_width = 3
  @char_height = 3
  @mapping = {
      " "  => create_pattern(SPACE),
      "/"  => create_pattern(SLASH),
      "\\" => create_pattern(BACKSLASH),
      "`"  => create_pattern(BACKTICK),
      "|"  => create_pattern(PIPE),
      "-"  => create_pattern(MINUS),
      "_"  => create_pattern(UNDERSCORE),
      "."  => create_pattern(FULLSTOP),
      "\"" => create_pattern(DOUBLEQUOTES),
      "'"  => create_pattern(SINGLEQUOTE),
      "*"  => create_pattern(STAR),
      "\#" => create_pattern(HASH),
  }
end

Public Instance Methods

ascii() click to toggle source
# File lib/vamp/graphic/transfer.rb, line 158
def ascii
  a = ""
  (context.height / char_height).times do |y|
    (context.width / char_width).times do |x|
      a += get_matching(get_pattern(x * char_width, y * char_height))
    end
    a += "\n"
  end
  a.chomp
end
create_data(pattern) click to toggle source
# File lib/vamp/graphic/transfer.rb, line 103
def create_data(pattern)
  a = pattern.split("\n")
  fail "pattern has wrong height" if a.size != char_height
  char_height.times do |dy|
    fail "pattern has wrong width" if a[dy].size != char_width
  end
  a
end
create_pattern(pattern) click to toggle source
# File lib/vamp/graphic/transfer.rb, line 112
def create_pattern(pattern)
  char = TextDotter.new(char_width, char_height)
  a = create_data(pattern)
  char_height.times do |y|
    char_width.times do |x|
      char.dot(x, y) if a[y][x] == "X"
    end
  end
  char
end
difference(pattern1, pattern2) click to toggle source
# File lib/vamp/graphic/transfer.rb, line 138
def difference(pattern1, pattern2)
  m = 0
  char_width.times do |dx|
    char_height.times do |dy|
      m += 1 if pattern1.dot?(dx, dy) != pattern2.dot?(dx, dy)
    end
  end
  m
end
get_matching(pattern) click to toggle source
# File lib/vamp/graphic/transfer.rb, line 148
def get_matching(pattern)
  ranking = {}
  mapping.each do |k, v|
    r = difference(pattern, v)
    ranking[k] = r
  end
  ranking.min_by{|k, v| v}[0]
end
get_pattern(x, y) click to toggle source
# File lib/vamp/graphic/transfer.rb, line 123
def get_pattern(x, y)
  pattern = ""
  char_height.times do |dy|
    char_width.times do |dx|
      if context.in?(x + dx, y + dy)
        pattern += (context.dot?(x + dx, y + dy) ? "X" : "_")
      else
        pattern += "_"
      end
    end
    pattern += "\n"
  end
  create_pattern(pattern.strip)
end