Class Fractals::Renderers::Base
In: lib/fractals/renderers.rb
Parent: Object

Inherited by Fractal, Renderers::Base includes traits common to each of the rendering modules.

Methods

Attributes

algorithm  [RW]  The renderer’s coloring algorithm.
bailout  [RW]  The number that determines if an iteration is approaching inifinity.
height  [RW]  The height of the fractal image.
last_iteration  [RW]  The last iteration number of a complex coordinate. Determined by the in_set? method.
magnification  [RW]  The magnification level of the fractal in powers of 100.
max_iterations  [RW]  The maximum number of iterations to perform on an expression.
set_color  [RW]  The color of complex coordinates inside the fractal set. Expects an array of RGB values [R, G, B].
theme  [RW]  The coloring theme applied to complex coordinates that lie outside of the fractal set.
width  [RW]  The width of the fractal image.

Public Class methods

Includes the provided module. Use the renderer= method for setting the renderer at runtime.

[Source]

# File lib/fractals/renderers.rb, line 81
      def self.acts_as_renderer(renderer)
        include renderer
      end

Sets the default property values.

[Source]

# 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

Public Instance methods

Determines if a complex coordinate lies within the fractal’s set.

[Source]

# 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

Loops through each x, y value pair yielding the pair and its RGB color value as an array [R, G, B].

[Source]

# 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

Extends the Renderers::Base class with the provided module.

Example:

mandelbrot = Mandelbrot.new
mandelbrot.renderer = Renderers::RMagickRenderer

[Source]

# File lib/fractals/renderers.rb, line 75
      def renderer=(renderer)
        self.extend renderer
      end

Determines the location of an x, y value pair on the complex coordinate plane.

[Source]

# 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

[Validate]