fractals 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. data/README +66 -0
  2. data/Rakefile +65 -0
  3. data/doc/classes/Fractals.html +169 -0
  4. data/doc/classes/Fractals/Algorithms.html +125 -0
  5. data/doc/classes/Fractals/BurningShip.html +172 -0
  6. data/doc/classes/Fractals/Fractal.html +261 -0
  7. data/doc/classes/Fractals/Julia.html +171 -0
  8. data/doc/classes/Fractals/Mandelbrot.html +172 -0
  9. data/doc/classes/Fractals/Newton.html +173 -0
  10. data/doc/classes/Fractals/Renderers.html +118 -0
  11. data/doc/classes/Fractals/Renderers/Base.html +496 -0
  12. data/doc/classes/Fractals/Renderers/JRubyRenderer.html +178 -0
  13. data/doc/classes/Fractals/Renderers/PNGRenderer.html +202 -0
  14. data/doc/classes/Fractals/Renderers/RMagickRenderer.html +210 -0
  15. data/doc/classes/Fractals/Themes.html +131 -0
  16. data/doc/created.rid +1 -0
  17. data/doc/files/README.html +180 -0
  18. data/doc/files/examples_rb.html +122 -0
  19. data/doc/files/gpl-2_0_txt.html +522 -0
  20. data/doc/files/lib/fractals/algorithms_rb.html +90 -0
  21. data/doc/files/lib/fractals/renderers_rb.html +104 -0
  22. data/doc/files/lib/fractals/themes_rb.html +90 -0
  23. data/doc/files/lib/fractals_rb.html +114 -0
  24. data/doc/fr_class_index.html +49 -0
  25. data/doc/fr_file_index.html +37 -0
  26. data/doc/fr_method_index.html +57 -0
  27. data/doc/index.html +21 -0
  28. data/doc/rdoc-style.css +299 -0
  29. data/examples.rb +101 -0
  30. data/gpl-2.0.txt +339 -0
  31. data/lib/fractals.rb +105 -0
  32. data/lib/fractals/algorithms.rb +35 -0
  33. data/lib/fractals/renderers.rb +169 -0
  34. data/lib/fractals/themes.rb +48 -0
  35. data/test/burning_ship_test.rb +19 -0
  36. data/test/julia_test.rb +20 -0
  37. data/test/mandelbrot_test.rb +19 -0
  38. data/test/newton_test.rb +20 -0
  39. data/test/renderer_test.rb +21 -0
  40. metadata +104 -0
@@ -0,0 +1,169 @@
1
+ #--
2
+ # Copyright (c) 2009 Ryan Baxter
3
+ #++
4
+
5
+ module Fractals
6
+ # Rendering modules for the PNG and RMagick libraries and JRuby.
7
+ module Renderers
8
+ begin; require 'rubygems'; rescue LoadError; end
9
+
10
+ # Inherited by Fractal, Renderers::Base includes traits common to each of
11
+ # the rendering modules.
12
+ class Base
13
+ # The number that determines if an iteration is approaching inifinity.
14
+ attr_accessor :bailout
15
+ # The maximum number of iterations to perform on an expression.
16
+ attr_accessor :max_iterations
17
+ # The last iteration number of a complex coordinate. Determined by the
18
+ # in_set? method.
19
+ attr_accessor :last_iteration
20
+ # The renderer's coloring algorithm.
21
+ attr_accessor :algorithm
22
+ # The width of the fractal image.
23
+ attr_accessor :width
24
+ # The height of the fractal image.
25
+ attr_accessor :height
26
+ # The magnification level of the fractal in powers of 100.
27
+ attr_accessor :magnification
28
+ # The coloring theme applied to complex coordinates that lie outside of
29
+ # the fractal set.
30
+ attr_accessor :theme
31
+ # The color of complex coordinates inside the fractal set. Expects an
32
+ # array of RGB values [R, G, B].
33
+ attr_accessor :set_color
34
+
35
+ # Sets the default property values.
36
+ def initialize(bailout=2, max_iterations=50, algorithm=Algorithms::EscapeTime)
37
+ @bailout, @max_iterations = bailout, max_iterations
38
+ @algorithm = algorithm
39
+ @width, @height = 300, 300
40
+ @magnification = 1.0
41
+ @theme, @set_color = Themes::Fire, [0, 0, 0]
42
+ end
43
+
44
+ # Determines if a complex coordinate lies within the fractal's set.
45
+ def in_set?(c)
46
+ @args[:c] = c
47
+ iterate(@max_iterations) do |i, z|
48
+ if z.abs > @bailout then
49
+ @last_iteration = i
50
+ return false
51
+ end
52
+ end
53
+ return true
54
+ end
55
+
56
+ # Loops through each x, y value pair yielding the pair and its RGB color
57
+ # value as an array [R, G, B].
58
+ def render
59
+ (0...@width).each do |x|
60
+ (0...@height).each do |y|
61
+ if !in_set?(where_is?(x, y)) then
62
+ yield x, y, @theme.call(@algorithm.call(self))
63
+ else
64
+ yield x, y, @set_color
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ # Extends the Renderers::Base class with the provided module.
71
+ #
72
+ # =Example:
73
+ # mandelbrot = Mandelbrot.new<br />
74
+ # mandelbrot.<b>renderer =</b> Renderers::RMagickRenderer
75
+ def renderer=(renderer)
76
+ self.extend renderer
77
+ end
78
+
79
+ # Includes the provided module. Use the renderer= method for setting the
80
+ # renderer at runtime.
81
+ def self.acts_as_renderer(renderer)
82
+ include renderer
83
+ end
84
+
85
+ # Determines the location of an x, y value pair on the complex coordinate
86
+ # plane.
87
+ def where_is?(x, y)
88
+ Complex(@c.real - (@width / 2 * scale) + (x * scale),
89
+ @c.image - (@height / 2 * scale) + (y * scale))
90
+ end
91
+
92
+ private
93
+ def scale
94
+ 0.01 / @magnification
95
+ end
96
+ end
97
+
98
+ # Renders fractals using the PNG library. PNGRenderer is the default
99
+ # renderer.
100
+ module PNGRenderer
101
+ begin; require 'png'; rescue LoadError; end
102
+
103
+ # Returns the fractal image as a BLOB.
104
+ def to_blob()
105
+ canvas = PNG::Canvas.new(@width, @height)
106
+ render do |x, y, color|
107
+ canvas[x, (y - @height).abs - 1] = PNG::Color.new(color[0], color[1], color[2], 255)
108
+ end
109
+
110
+ PNG.new(canvas).to_blob
111
+ end
112
+
113
+ # Writes the image to the specifiec file path.
114
+ def write(file_path='fractal.png')
115
+ File.open(file_path, 'wb') { |file| file.write(to_blob) }
116
+ end
117
+ end
118
+
119
+ # Renders fractals using the RMagick library.
120
+ module RMagickRenderer
121
+ begin; require 'RMagick'; rescue LoadError; end
122
+
123
+ # Returns the fractal image as a BLOB of the specified file format.
124
+ #
125
+ # =Example:
126
+ # mandelbrot = Mandelbrot.new<br />
127
+ # mandelbrot.renderer = Renderers::RMagickRenderer<br />
128
+ # blob = mandelbrot.<b>to_blob('jpg')</b>
129
+ def to_blob(file_format)
130
+ image = Magick::Image.new(@width, @height)
131
+ render do |x, y, color|
132
+ image.pixel_color(x, y, "rgb(#{color.join(',')})")
133
+ end
134
+
135
+ image.to_blob { self.format = file_format; self.quality = 100 }
136
+ end
137
+
138
+ # Writes the image to the specified file path.
139
+ def write(file_path='fractal.png')
140
+ File.open(file_path, 'wb') do |file|
141
+ file.write(to_blob(file_path.split('.').last))
142
+ end
143
+ end
144
+ end
145
+
146
+ # Renders fractals using native Java libraries.
147
+ module JRubyRenderer
148
+ begin; include Java; rescue NameError; end
149
+
150
+ # Writes the image to the specified file path.
151
+ def write(file_path='fractal.png')
152
+ import java.awt.image.BufferedImage
153
+ import javax.imageio.ImageIO
154
+
155
+ buffered_image = BufferedImage.new(@width, @height, BufferedImage::TYPE_INT_RGB)
156
+ render do |x, y, color|
157
+ buffered_image.setRGB(x, y, to_rgb(color))
158
+ end
159
+
160
+ ImageIO.write(buffered_image, file_path.split('.').last, java.io.File.new(file_path))
161
+ end
162
+
163
+ private
164
+ def to_rgb(color)
165
+ (color[0] << 16) + (color[1] << 8) + color[2]
166
+ end
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,48 @@
1
+ #--
2
+ # Copyright (c) 2009 Ryan Baxter
3
+ #++
4
+
5
+ module Fractals
6
+ # Each themes returns an array of RGB values [R, G, B] for the provided color
7
+ # index.
8
+ #
9
+ # <b>Fire</b>:: The default theme. Themes::Fire produces bright fiery
10
+ # fractals.
11
+ # <b>Water</b>:: Deep blues and greens.
12
+ # <b>Winter</b>:: Lots of blue. Looks best with set_color = [255, 255, 255].
13
+ # <b>None</b>:: Coordinates outside the fractal set will appear white in
14
+ # color.
15
+ # <br />
16
+ # =Example:
17
+ # mandelbrot = Mandelbrot.new<br />
18
+ # mandelbrot.theme = <b>Themes::Winter</b>
19
+ module Themes
20
+ #:stopdoc:
21
+ Fire = lambda do |index|
22
+ case
23
+ when index >= 510 then return [255, 255, index % 255]
24
+ when index >= 255 then return [255, index % 255, 0]
25
+ else return [index % 255, 0, 0]
26
+ end
27
+ end
28
+
29
+ Water = lambda do |index|
30
+ case
31
+ when index >= 510 then return [index % 255, 255 - (index % 255), 0]
32
+ when index >= 255 then return [0, index - 255, 255 - (index - 255)]
33
+ else return [0, 0, index]
34
+ end
35
+ end
36
+
37
+ Winter = lambda do |index|
38
+ case
39
+ when index >= 510 then return [0, 255 % index, 255]
40
+ when index >= 255 then return [0, index % 255, 255]
41
+ else return [0, 0, index % 255]
42
+ end
43
+ end
44
+
45
+ None = lambda { |index| return [255, 255, 255] }
46
+ #:startdoc:
47
+ end
48
+ end
@@ -0,0 +1,19 @@
1
+ require 'fractals'
2
+ require 'test/unit'
3
+
4
+ include Fractals
5
+
6
+ class BurningShipTest < Test::Unit::TestCase
7
+ def setup
8
+ burning_ship = BurningShip.new
9
+ burning_ship.write
10
+ end
11
+
12
+ def teardown
13
+ File.delete('fractal.png')
14
+ end
15
+
16
+ def test_file_size
17
+ assert_equal(13326, File.size?('fractal.png'))
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ require 'fractals'
2
+ require 'test/unit'
3
+
4
+ include Fractals
5
+
6
+ class JuliaTest < Test::Unit::TestCase
7
+ def setup
8
+ julia = Julia.new
9
+ julia.write
10
+ end
11
+
12
+ def teardown
13
+ File.delete('fractal.png')
14
+ end
15
+
16
+ def test_file_size
17
+ assert_equal(16005, File.size?('fractal.png'))
18
+ end
19
+ end
20
+
@@ -0,0 +1,19 @@
1
+ require 'fractals'
2
+ require 'test/unit'
3
+
4
+ include Fractals
5
+
6
+ class MandelbrotTest < Test::Unit::TestCase
7
+ def setup
8
+ mandelbrot = Mandelbrot.new
9
+ mandelbrot.write
10
+ end
11
+
12
+ def teardown
13
+ File.delete('fractal.png')
14
+ end
15
+
16
+ def test_file_size
17
+ assert_equal(12046, File.size?('fractal.png'))
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ require 'fractals'
2
+ require 'test/unit'
3
+
4
+ include Fractals
5
+
6
+ class NewtonTest < Test::Unit::TestCase
7
+ def setup
8
+ newton = Newton.new
9
+ newton.write
10
+ end
11
+
12
+ def teardown
13
+ File.delete('fractal.png')
14
+ end
15
+
16
+ def test_file_size
17
+ assert_equal(4753, File.size?('fractal.png'))
18
+ end
19
+ end
20
+
@@ -0,0 +1,21 @@
1
+ require 'fractals'
2
+ require 'test/unit'
3
+
4
+ include Fractals
5
+
6
+ class MandelbrotTest < Test::Unit::TestCase
7
+ def test_false_in_set?
8
+ mandelbrot = Mandelbrot.new
9
+ assert_equal(false, mandelbrot.in_set?(Complex(-5.0, -5.0)))
10
+ end
11
+
12
+ def test_true_in_set?
13
+ mandelbrot = Mandelbrot.new
14
+ assert_equal(true, mandelbrot.in_set?(Complex(0.0, 0.0)))
15
+ end
16
+
17
+ def test_where_is?
18
+ mandelbrot = Mandelbrot.new
19
+ assert_equal(Complex(-2.15, -1.5), mandelbrot.where_is?(0, 0))
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fractals
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Baxter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-26 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: rcbaxter@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - examples.rb
24
+ - gpl-2.0.txt
25
+ - README
26
+ files:
27
+ - examples.rb
28
+ - gpl-2.0.txt
29
+ - Rakefile
30
+ - README
31
+ - doc/fr_method_index.html
32
+ - doc/classes
33
+ - doc/classes/Fractals.html
34
+ - doc/classes/Fractals
35
+ - doc/classes/Fractals/Fractal.html
36
+ - doc/classes/Fractals/Newton.html
37
+ - doc/classes/Fractals/BurningShip.html
38
+ - doc/classes/Fractals/Julia.html
39
+ - doc/classes/Fractals/Renderers
40
+ - doc/classes/Fractals/Renderers/JRubyRenderer.html
41
+ - doc/classes/Fractals/Renderers/RMagickRenderer.html
42
+ - doc/classes/Fractals/Renderers/PNGRenderer.html
43
+ - doc/classes/Fractals/Renderers/Base.html
44
+ - doc/classes/Fractals/Renderers.html
45
+ - doc/classes/Fractals/Themes.html
46
+ - doc/classes/Fractals/Mandelbrot.html
47
+ - doc/classes/Fractals/Algorithms.html
48
+ - doc/files
49
+ - doc/files/examples_rb.html
50
+ - doc/files/README.html
51
+ - doc/files/gpl-2_0_txt.html
52
+ - doc/files/lib
53
+ - doc/files/lib/fractals_rb.html
54
+ - doc/files/lib/fractals
55
+ - doc/files/lib/fractals/themes_rb.html
56
+ - doc/files/lib/fractals/renderers_rb.html
57
+ - doc/files/lib/fractals/algorithms_rb.html
58
+ - doc/fr_file_index.html
59
+ - doc/index.html
60
+ - doc/created.rid
61
+ - doc/fr_class_index.html
62
+ - doc/rdoc-style.css
63
+ - lib/fractals.rb
64
+ - lib/fractals
65
+ - lib/fractals/algorithms.rb
66
+ - lib/fractals/themes.rb
67
+ - lib/fractals/renderers.rb
68
+ - test/burning_ship_test.rb
69
+ - test/newton_test.rb
70
+ - test/renderer_test.rb
71
+ - test/julia_test.rb
72
+ - test/mandelbrot_test.rb
73
+ has_rdoc: true
74
+ homepage: http://ryanbaxter.net/pages/ruby_fractal_library
75
+ post_install_message:
76
+ rdoc_options: []
77
+
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: "0"
85
+ version:
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
91
+ version:
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.3.1
96
+ signing_key:
97
+ specification_version: 2
98
+ summary: A library for creating fractals in the Ruby programming language.
99
+ test_files:
100
+ - test/burning_ship_test.rb
101
+ - test/newton_test.rb
102
+ - test/renderer_test.rb
103
+ - test/julia_test.rb
104
+ - test/mandelbrot_test.rb