Class: Fractals::Renderers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/fractals/renderers.rb

Overview

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

Direct Known Subclasses

Fractal

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#algorithmObject

The renderer’s coloring algorithm.



21
22
23
# File 'lib/fractals/renderers.rb', line 21

def algorithm
  @algorithm
end

#bailoutObject

The number that determines if an iteration is approaching inifinity.



14
15
16
# File 'lib/fractals/renderers.rb', line 14

def bailout
  @bailout
end

#heightObject

The height of the fractal image.



25
26
27
# File 'lib/fractals/renderers.rb', line 25

def height
  @height
end

#last_iterationObject

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

#magnificationObject

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_iterationsObject

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_colorObject

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

#themeObject

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

#widthObject

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.

Returns:

  • (Boolean)


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

#renderObject

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.

Returns:

  • (Boolean)


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