Class: Carpet
- Inherits:
-
Object
- Object
- Carpet
- Defined in:
- lib/cantor_carpets.rb
Instance Attribute Summary collapse
-
#bits ⇒ Object
readonly
Returns the value of attribute bits.
-
#depth ⇒ Object
readonly
Returns the value of attribute depth.
-
#image ⇒ Object
readonly
Returns the value of attribute image.
-
#seed ⇒ Object
readonly
Returns the value of attribute seed.
Instance Method Summary collapse
- #calculate_bits(bit_array) ⇒ Object
- #compute_carpet ⇒ Object
- #create_image ⇒ Object
-
#initialize(seed, depth) ⇒ Carpet
constructor
A new instance of Carpet.
- #new_bit_array ⇒ Object
- #write_image(name = 'carpet.png') ⇒ Object
Constructor Details
#initialize(seed, depth) ⇒ Carpet
Returns a new instance of Carpet.
7 8 9 10 11 |
# File 'lib/cantor_carpets.rb', line 7 def initialize(seed, depth) @seed = seed @depth = depth compute_carpet end |
Instance Attribute Details
#bits ⇒ Object (readonly)
Returns the value of attribute bits.
5 6 7 |
# File 'lib/cantor_carpets.rb', line 5 def bits @bits end |
#depth ⇒ Object (readonly)
Returns the value of attribute depth.
5 6 7 |
# File 'lib/cantor_carpets.rb', line 5 def depth @depth end |
#image ⇒ Object (readonly)
Returns the value of attribute image.
5 6 7 |
# File 'lib/cantor_carpets.rb', line 5 def image @image end |
#seed ⇒ Object (readonly)
Returns the value of attribute seed.
5 6 7 |
# File 'lib/cantor_carpets.rb', line 5 def seed @seed end |
Instance Method Details
#calculate_bits(bit_array) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cantor_carpets.rb', line 23 def calculate_bits(bit_array) if bit_array.size == @seed.size return @seed end row_chunk = bit_array.row_size / @seed.row_size col_chunk = bit_array.column_size / @seed.column_size @seed.each_with_index do |e, index| xi = index[0] yi = index[1] row_range = (row_chunk * xi)...(row_chunk * (xi + 1)) column_range = (col_chunk * yi)...(col_chunk * (yi + 1)) if e == 1 bit_array[row_range, column_range] = calculate_bits(bit_array[row_range, column_range]) end end bit_array end |
#compute_carpet ⇒ Object
13 14 15 16 |
# File 'lib/cantor_carpets.rb', line 13 def compute_carpet @bits = calculate_bits(new_bit_array) @image = create_image end |
#create_image ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/cantor_carpets.rb', line 43 def create_image canvas = PNG::Canvas.new(@seed.column_size ** @depth, @seed.row_size ** @depth, PNG::Color::White) canvas.each do |x, y, color| canvas[x, y] = PNG::Color::Black if @bits[-y, x] == 1 end PNG.new(canvas) end |
#new_bit_array ⇒ Object
18 19 20 21 |
# File 'lib/cantor_carpets.rb', line 18 def new_bit_array Array2D.new(@seed.row_size ** @depth, @seed.column_size ** @depth, 0) end |
#write_image(name = 'carpet.png') ⇒ Object
53 54 55 |
# File 'lib/cantor_carpets.rb', line 53 def write_image(name='carpet.png') @image.save(name) end |