Class: Fractals::Renderers::Base
- Inherits:
-
Object
- Object
- Fractals::Renderers::Base
- Defined in:
- lib/fractals/renderers.rb
Overview
Inherited by Fractal, Renderers::Base includes traits common to each of the rendering modules.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#algorithm ⇒ Object
The renderer’s coloring algorithm.
-
#bailout ⇒ Object
The number that determines if an iteration is approaching inifinity.
-
#height ⇒ Object
The height of the fractal image.
-
#last_iteration ⇒ Object
The last iteration number of a complex coordinate.
-
#magnification ⇒ Object
The magnification level of the fractal in powers of 100.
-
#max_iterations ⇒ Object
The maximum number of iterations to perform on an expression.
-
#set_color ⇒ Object
The color of complex coordinates inside the fractal set.
-
#theme ⇒ Object
The coloring theme applied to complex coordinates that lie outside of the fractal set.
-
#width ⇒ Object
The width of the fractal image.
Class Method Summary collapse
-
.acts_as_renderer(renderer) ⇒ Object
Includes the provided module.
Instance Method Summary collapse
-
#in_set?(c) ⇒ Boolean
Determines if a complex coordinate lies within the fractal’s set.
-
#initialize(bailout = 2, max_iterations = 50, algorithm = Algorithms::EscapeTime) ⇒ Base
constructor
Sets the default property values.
-
#render ⇒ Object
Loops through each x, y value pair yielding the pair and its RGB color value as an array [R, G, B].
-
#renderer=(renderer) ⇒ Object
Extends the Renderers::Base class with the provided module.
-
#where_is?(x, y) ⇒ Boolean
Determines the location of an x, y value pair on the complex coordinate plane.
Constructor Details
#initialize(bailout = 2, max_iterations = 50, algorithm = Algorithms::EscapeTime) ⇒ Base
Sets the default property values.
36 37 38 39 40 41 42 |
# File 'lib/fractals/renderers.rb', line 36 def initialize(bailout=2, max_iterations=50, algorithm=Algorithms::EscapeTime) @bailout, @max_iterations = bailout, max_iterations @algorithm = algorithm @width, @height = 300, 300 @magnification = 1.0 @theme, @set_color = Themes::Fire, [0, 0, 0] end |
Instance Attribute Details
#algorithm ⇒ Object
The renderer’s coloring algorithm.
21 22 23 |
# File 'lib/fractals/renderers.rb', line 21 def algorithm @algorithm end |
#bailout ⇒ Object
The number that determines if an iteration is approaching inifinity.
14 15 16 |
# File 'lib/fractals/renderers.rb', line 14 def bailout @bailout end |
#height ⇒ Object
The height of the fractal image.
25 26 27 |
# File 'lib/fractals/renderers.rb', line 25 def height @height end |
#last_iteration ⇒ Object
The last iteration number of a complex coordinate. Determined by the in_set? method.
19 20 21 |
# File 'lib/fractals/renderers.rb', line 19 def last_iteration @last_iteration end |
#magnification ⇒ Object
The magnification level of the fractal in powers of 100.
27 28 29 |
# File 'lib/fractals/renderers.rb', line 27 def magnification @magnification end |
#max_iterations ⇒ Object
The maximum number of iterations to perform on an expression.
16 17 18 |
# File 'lib/fractals/renderers.rb', line 16 def max_iterations @max_iterations end |
#set_color ⇒ Object
The color of complex coordinates inside the fractal set. Expects an array of RGB values [R, G, B].
33 34 35 |
# File 'lib/fractals/renderers.rb', line 33 def set_color @set_color end |
#theme ⇒ Object
The coloring theme applied to complex coordinates that lie outside of the fractal set.
30 31 32 |
# File 'lib/fractals/renderers.rb', line 30 def theme @theme end |
#width ⇒ Object
The width of the fractal image.
23 24 25 |
# File 'lib/fractals/renderers.rb', line 23 def width @width end |
Class Method Details
.acts_as_renderer(renderer) ⇒ Object
Includes the provided module. Use the renderer= method for setting the renderer at runtime.
81 82 83 |
# File 'lib/fractals/renderers.rb', line 81 def self.acts_as_renderer(renderer) include renderer end |
Instance Method Details
#in_set?(c) ⇒ Boolean
Determines if a complex coordinate lies within the fractal’s set.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/fractals/renderers.rb', line 45 def in_set?(c) @args[:c] = c iterate(@max_iterations) do |i, z| if z.abs > @bailout then @last_iteration = i return false end end return true end |
#render ⇒ Object
Loops through each x, y value pair yielding the pair and its RGB color value as an array [R, G, B].
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/fractals/renderers.rb', line 58 def render (0...@width).each do |x| (0...@height).each do |y| if !in_set?(where_is?(x, y)) then yield x, y, @theme.call(@algorithm.call(self)) else yield x, y, @set_color end end end end |
#renderer=(renderer) ⇒ Object
Extends the Renderers::Base class with the provided module.
Example:
mandelbrot = Mandelbrot.new<br /> mandelbrot.renderer = Renderers::RMagickRenderer
75 76 77 |
# File 'lib/fractals/renderers.rb', line 75 def renderer=(renderer) self.extend renderer end |
#where_is?(x, y) ⇒ Boolean
Determines the location of an x, y value pair on the complex coordinate plane.
87 88 89 90 |
# File 'lib/fractals/renderers.rb', line 87 def where_is?(x, y) Complex(@c.real - (@width / 2 * scale) + (x * scale), @c.image - (@height / 2 * scale) + (y * scale)) end |