Class: Vamp::Graphic::Transfer
- Inherits:
-
Object
- Object
- Vamp::Graphic::Transfer
- Defined in:
- lib/vamp/graphic/transfer.rb
Overview
Transfer dotter data into ASCII
Constant Summary collapse
- SPACE =
<<-'END' ___ ___ ___ END
- SLASH =
<<-'END' __X _X_ X__ END
- BACKSLASH =
<<-'END' X__ _X_ __X END
- BACKTICK =
<<-'END' X__ ___ ___ END
- PIPE =
<<-'END' _X_ _X_ _X_ END
- MINUS =
<<-'END' ___ XXX ___ END
- UNDERSCORE =
<<-'END' ___ ___ XXX 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
-
#char_height ⇒ Object
readonly
Returns the value of attribute char_height.
-
#char_width ⇒ Object
readonly
Returns the value of attribute char_width.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#mapping ⇒ Object
readonly
Returns the value of attribute mapping.
Instance Method Summary collapse
- #ascii ⇒ Object
- #create_data(pattern) ⇒ Object
- #create_pattern(pattern) ⇒ Object
- #difference(pattern1, pattern2) ⇒ Object
- #get_matching(pattern) ⇒ Object
- #get_pattern(x, y) ⇒ Object
-
#initialize(context) ⇒ Transfer
constructor
A new instance of Transfer.
Constructor Details
#initialize(context) ⇒ Transfer
Returns a new instance of Transfer.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# 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 |
Instance Attribute Details
#char_height ⇒ Object (readonly)
Returns the value of attribute char_height.
7 8 9 |
# File 'lib/vamp/graphic/transfer.rb', line 7 def char_height @char_height end |
#char_width ⇒ Object (readonly)
Returns the value of attribute char_width.
6 7 8 |
# File 'lib/vamp/graphic/transfer.rb', line 6 def char_width @char_width end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
8 9 10 |
# File 'lib/vamp/graphic/transfer.rb', line 8 def context @context end |
#mapping ⇒ Object (readonly)
Returns the value of attribute mapping.
9 10 11 |
# File 'lib/vamp/graphic/transfer.rb', line 9 def mapping @mapping end |
Instance Method Details
#ascii ⇒ Object
158 159 160 161 162 163 164 165 166 167 |
# 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) ⇒ Object
103 104 105 106 107 108 109 110 |
# 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) ⇒ Object
112 113 114 115 116 117 118 119 120 121 |
# 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) ⇒ Object
138 139 140 141 142 143 144 145 146 |
# 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) ⇒ Object
148 149 150 151 152 153 154 155 |
# 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) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# 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 |