Class: Fractal::Generator

Inherits:
Object
  • Object
show all
Includes:
Math
Defined in:
lib/fractal/generator.rb

Direct Known Subclasses

IslandGenerator

Constant Summary collapse

INITIAL_RANGE =
400

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Math

max, min, pot

Constructor Details

#initialize(options = {}) ⇒ Generator

Returns a new instance of Generator.



36
37
38
39
40
41
42
43
44
45
# File 'lib/fractal/generator.rb', line 36

def initialize(options = {})
  options.reverse_merge! default_options
  width, height = options[:width], options[:height]
  @map = Fractal::Map.new(pot(max(width, height)) + 1)
  @random = options[:seed] ? Random.new(options[:seed]) : Random.new
  @seed = @random.seed
  @smoothness = options[:smoothness] || 2
  generate
  @map.truncate(width, height)
end

Instance Attribute Details

#mapObject (readonly)

Returns the value of attribute map.



30
31
32
# File 'lib/fractal/generator.rb', line 30

def map
  @map
end

#randomObject (readonly)

Returns the value of attribute random.



30
31
32
# File 'lib/fractal/generator.rb', line 30

def random
  @random
end

#seedObject (readonly)

Returns the value of attribute seed.



30
31
32
# File 'lib/fractal/generator.rb', line 30

def seed
  @seed
end

#smoothnessObject (readonly)

Returns the value of attribute smoothness.



30
31
32
# File 'lib/fractal/generator.rb', line 30

def smoothness
  @smoothness
end

Class Method Details

.default_image_optionsObject



23
24
25
# File 'lib/fractal/generator.rb', line 23

def default_image_options
  { :alpha => false, :island => false, :high_color => 'ffffff', :low_color => '000000' }
end

.image(options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fractal/generator.rb', line 3

def image(options = {})
  options.reverse_merge! default_image_options
  klass = options[:island] ? Fractal::IslandGenerator : self
  
  fractal = klass.new options
                
  if options[:alpha]
    image = Magick::Image.new(fractal.width, fractal.height) { self.background_color = 'transparent' }
    image.import_pixels 0, 0, fractal.width, fractal.height, 'IA', fractal.bytes.collect { |b| [b,b] }.flatten.pack('C*')
  else
    image = Magick::Image.new(fractal.width, fractal.height)
    image.import_pixels 0, 0, fractal.width, fractal.height, 'I', fractal.bytes.pack('C*')
  end
  
  options[:low_color] = '000000' if options[:low_color].blank?
  options[:high_color] = 'ffffff' if options[:high_color].blank?
  image = image.level_colors("##{options[:low_color]}", "##{options[:high_color]}", true)
  image
end

Instance Method Details

#bytesObject



34
# File 'lib/fractal/generator.rb', line 34

def bytes;  map.bytes;  end

#heightObject



33
# File 'lib/fractal/generator.rb', line 33

def height; map.height; end

#to_sObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fractal/generator.rb', line 47

def to_s
  "".tap do |result|
    for x in 0...width
      for y in 0...height
        result.concat map[x, y].to_s[0..6].rjust(7)
        result.concat " "
      end
      result.concat "\n"
    end
  end
end

#widthObject



32
# File 'lib/fractal/generator.rb', line 32

def width;  map.width;  end