# RB require 'rubygems' require 'complex' require 'png' module Fractals module Fractal attr_accessor :begin_range, :end_range def initialize(begin_range, end_range) @begin_range, @end_range = begin_range, end_range end def draw(height=250, width=250, m=1.0, save_as='fractal.png') canvas = PNG::Canvas.new(height, width) # Find the complex coordinate for each pixel. 0.upto(height - 1) { |y| i = (y * (@end_range.image - @begin_range.image) / height + @begin_range.image) * m 0.upto(width - 1) { |x| r = (x * (@end_range.real - @begin_range.real) / width + @begin_range.real) * m if self.in_set?(Complex(r, i)) then canvas[x, y] = PNG::Color::Black else canvas[x, y] = fetch_color(self.last_iteration, self.max_iterations) end } } png = PNG.new(canvas) png.save(save_as) end private def fetch_color(last_iteration, max_iterations) divisor = 765*last_iteration/max_iterations case divisor when 0..254 then return PNG::Color.new(divisor%255, 0, 0, 255) when 255..509 then return PNG::Color.new(255, divisor%255, 0, 255) when 510..765 then return PNG::Color.new(255, 255, divisor%255, 255) end end end class Julia include Fractal attr_accessor :seed, :bailout, :max_iterations attr_reader :last_iteration def initialize(seed=Complex(0.36, 0.1), bailout=2, max_iterations=100, begin_range=Complex(-2.25, -1.5), end_range=Complex(0.75, 1.5)) super(begin_range, end_range) @seed, @bailout, @max_iterations = seed, bailout, max_iterations end def in_set?(z) @max_iterations.times { |i| z = z**2 + @seed if z > @bailout then @last_iteration = i return false end } return true end end class Mandelbrot include Fractal attr_accessor :bailout, :max_iterations attr_reader :last_iteration def initialize(bailout=5, max_iterations=100, begin_range=Complex(-2.25, -1.5), end_range=Complex(0.75, 1.5)) super(begin_range, end_range) @bailout, @max_iterations = bailout, max_iterations end def in_set?(c) z = 0 @max_iterations.times { |i| z = z**2 + c if z > @bailout then @last_iteration = i return false end } return true end end end