Class: Vamp::Graphic::Transfer5

Inherits:
Object
  • Object
show all
Defined in:
lib/vamp/graphic/transfer5.rb

Overview

Transfer dotter data into ASCII

Constant Summary collapse

SPACE =
<<-'END'
_____
_____
_____
_____
_____
END
SLASH =
<<-'END'
___X_
__X__
_X___
X____
_____
END
BACKSLASH =
<<-'END'
X____
_X___
__X__
___X_
_____
END
BACKTICK =
<<-'END'
_X___
_____
_____
_____
_____
END
PIPE =
<<-'END'
__X__
__X__
__X__
__X__
_____
END
MINUS =
<<-'END'
_____
_____
XXXX_
_____
_____
END
UNDERSCORE =
<<-'END'
_____
_____
_____
_____
XXXXX
END
FULLSTOP =
<<-'END'
_____
_____
_____
__X__
_____
END
DOUBLEQUOTES =
<<-'END'
_X_X_
_____
_____
_____
_____
END
SINGLEQUOTE =
<<-'END'
__X__
_____
_____
_____
_____
END
STAR =
<<-'END'
_____
__X__
_XXX_
__X__
_____
END
HASH =
<<-'END'
_____
_XXX_
_XXX_
_XXX_
_____
END

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Transfer5

Returns a new instance of Transfer5.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/vamp/graphic/transfer5.rb', line 107

def initialize(context)
  @context = context
  @char_width = 5
  @char_height = 5
  @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

Instance Attribute Details

#char_heightObject (readonly)

Returns the value of attribute char_height.



7
8
9
# File 'lib/vamp/graphic/transfer5.rb', line 7

def char_height
  @char_height
end

#char_widthObject (readonly)

Returns the value of attribute char_width.



6
7
8
# File 'lib/vamp/graphic/transfer5.rb', line 6

def char_width
  @char_width
end

#contextObject (readonly)

Returns the value of attribute context.



8
9
10
# File 'lib/vamp/graphic/transfer5.rb', line 8

def context
  @context
end

#mappingObject (readonly)

Returns the value of attribute mapping.



9
10
11
# File 'lib/vamp/graphic/transfer5.rb', line 9

def mapping
  @mapping
end

Instance Method Details

#asciiObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/vamp/graphic/transfer5.rb', line 186

def ascii
  a = ""
  (context.height / char_height).times do |y|
    (context.width / char_width).times do |x|
      m = get_matching(get_pattern(x * char_width, y * char_height))
      if (m[1] > 0)
        n = []
        n << get_matching(get_pattern(x * char_width + 1, y * char_height), true)
        n << get_matching(get_pattern(x * char_width - 1, y * char_height), true)
        n << get_matching(get_pattern(x * char_width, y * char_height + 1), true)
        n << get_matching(get_pattern(x * char_width, y * char_height - 1), true)
=begin
        n << get_matching(get_pattern(x * char_width + 2, y * char_height), true)
        n << get_matching(get_pattern(x * char_width - 2, y * char_height), true)
        n << get_matching(get_pattern(x * char_width, y * char_height + 2), true)
        n << get_matching(get_pattern(x * char_width, y * char_height - 2), true)
=end
        n.each do |v|
          if v[1] < m[1]
            m = v
          end
        end
      end
      a += m[0]
    end
    a += "\n"
  end
  a.chomp
end

#create_data(pattern) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/vamp/graphic/transfer5.rb', line 127

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) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/vamp/graphic/transfer5.rb', line 136

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) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/vamp/graphic/transfer5.rb', line 162

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, without_blank = false) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/vamp/graphic/transfer5.rb', line 172

def get_matching(pattern, without_blank = false)
  ranking = {}
  mapping.each do |k, v|
    next if without_blank and v == " "
    r = difference(pattern, v)
    ranking[k] = r
  end
  match = ranking.min_by{|k, v| v}
  # require "pp"
  # pp match
  match[0..1]
end

#get_pattern(x, y) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/vamp/graphic/transfer5.rb', line 147

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