Class: Fractal::Generator
- Inherits:
-
Object
- Object
- Fractal::Generator
- Includes:
- Math
- Defined in:
- lib/fractal/generator.rb
Direct Known Subclasses
Constant Summary collapse
- INITIAL_RANGE =
400
Instance Attribute Summary collapse
-
#map ⇒ Object
readonly
Returns the value of attribute map.
-
#random ⇒ Object
readonly
Returns the value of attribute random.
-
#seed ⇒ Object
readonly
Returns the value of attribute seed.
-
#smoothness ⇒ Object
readonly
Returns the value of attribute smoothness.
Class Method Summary collapse
Instance Method Summary collapse
- #bytes ⇒ Object
- #height ⇒ Object
-
#initialize(options = {}) ⇒ Generator
constructor
A new instance of Generator.
- #to_s ⇒ Object
- #width ⇒ Object
Methods included from Math
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( = {}) .reverse_merge! width, height = [:width], [:height] @map = Fractal::Map.new(pot(max(width, height)) + 1) @random = [:seed] ? Random.new([:seed]) : Random.new @seed = @random.seed @smoothness = [:smoothness] || 2 generate @map.truncate(width, height) end |
Instance Attribute Details
#map ⇒ Object (readonly)
Returns the value of attribute map.
30 31 32 |
# File 'lib/fractal/generator.rb', line 30 def map @map end |
#random ⇒ Object (readonly)
Returns the value of attribute random.
30 31 32 |
# File 'lib/fractal/generator.rb', line 30 def random @random end |
#seed ⇒ Object (readonly)
Returns the value of attribute seed.
30 31 32 |
# File 'lib/fractal/generator.rb', line 30 def seed @seed end |
#smoothness ⇒ Object (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_options ⇒ Object
23 24 25 |
# File 'lib/fractal/generator.rb', line 23 def { :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( = {}) .reverse_merge! klass = [:island] ? Fractal::IslandGenerator : self fractal = klass.new if [: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 [:low_color] = '000000' if [:low_color].blank? [:high_color] = 'ffffff' if [:high_color].blank? image = image.level_colors("##{[:low_color]}", "##{[:high_color]}", true) image end |
Instance Method Details
#bytes ⇒ Object
34 |
# File 'lib/fractal/generator.rb', line 34 def bytes; map.bytes; end |
#height ⇒ Object
33 |
# File 'lib/fractal/generator.rb', line 33 def height; map.height; end |
#to_s ⇒ Object
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 |
#width ⇒ Object
32 |
# File 'lib/fractal/generator.rb', line 32 def width; map.width; end |