Ruby Fractal Library 1.1.0
Posted by Ryan Baxter Fri, 24 Oct 2008 18:49:00 GMT
Between spending time with the baby and working on a new project (more to come), I’ve found time to add a few features to the Ruby Fractal Library. An Algorithms module now contains lambda expressions implementing both the Escape Time and Normalized Iteration Count algorithms. Users can also create their own lambda expressions and assign them to the Fractal class’s algorithm property.
In the example below, I’ve show the difference between images rendered using the Escape Time and Normalized Iteration Count algorithms. As you can see, the Normalized Iteration Count algorithm generates images without the color banding associated with the Escape Time algorithm.
| Escape Time | Normalized Iteration Count |
|
|
A Themes module now serves as a home for all of the library’s predefined color palettes. There are only two, but they’re easy to make. Since themes are also expressed as lambdas they too can be created by users and applied to the Fractal class. Below is my attempt at creating a snowflake using the Julia set and a user-defined theme.
snowflakes = Julia.new(Complex(-0.3007, 0.6601), 5, 100)
snowflakes.width = 350
snowflakes.height = 350
snowflakes.m = 2
snowflakes.set_color = PNG::Color::White
snowflakes.algorithm = Algorithms::NormalizedIterationCount
snowflakes.theme = lambda { |index|
r, g, b = 0, 0, 0
if index >= 510
r = 0
g = 255 % index
b = 255
elsif index >= 255
r = 0
g = index % 255
b = 255
else
b = index % 255
end
return r, g, b
}
snowflakes.draw('snowflakes.png')Snowflakes was inspired by a colleague who wondered why I kept creating paisley. The Fire theme will do that. :)
When I get more time I’d like to implement some of the Escape Angle and Curvature Estimation algorithms as outlined by Garcia, Fernandez, Barrallo, and Martin, but for now I’d gladly accept any user-contributed algorithms or themes.
A fractal can now be instantiated with a single point rather than a range. This is the biggest breaking change over version 1.0.0. I believe that this makes the library easier to use and more similar to other fractal generating programs. The Fractal type also contains a where_is? method. This should help when trying to determine the complex coordinate of an x, y value pair.
In testing the library, I attempted to generate a few of the fractals found in the Mandelbrot set Wikipedia entry. The images found at Wikipedia were rendered using Ultra Fractal 3 and are beautiful. Knowing Ruby, I didn’t expect to generate images with the same quality, but I was pleasantly surprised. Here is “Satellite” followed by the Misiurewicz point. Both were rendered with the Ruby Fractal Library.
| Satellite | Misiurewicz point |
|
|
Satellite can be found where c = -0.743643135, 0.131825963i at around 200k magnification. I had to set max_iterations = 1500 to get this level of detail. It still took a few minutes to render using the latest YARV interpreter on an Intel Core 2 Duo 2.6Ghz. Overall I’ve noticed that YARV finishes rendering the Mandelbrot set in approximately half the time of the old Matz interpreter. Not a bad gain. It’ll never be as quick as C, but I still look forward to Ruby 2.0!
The Ruby Fractal Library can be found under the “Projects” section of this website. Feel free to send me any feedback. I’d love to see some new color themes or algorithms.
- Posted in Code Snippets
- Meta no trackbacks, 2 comments, permalink, rss, atom
Trackbacks
Use the following link to trackback from your own site:
http://crunchlife.com/articles/trackback/83


If any Windows users need a C compiler, I suggest using Cygwin with GCC. GCC is great and free!
If more people are interested in contributing I'll set up some sort of project management software.