Sha256: 3d44612cb8bfaccc933337ad3696b8f11914450876616482b03024783af6dafd

Contents?: true

Size: 1.03 KB

Versions: 6

Compression:

Stored size: 1.03 KB

Contents

require 'complex'

class Mandelbrot
  def initialize(iterate, threshold)
    @iterate = iterate
    @threshold = threshold
  end

  def map(z, c)
    z * z + c
  end

  def iterate_map(z, c, &block)
    z_old = z
    @iterate.times do |i|
      z_new = map(z_old, c)
      z_old = z_new
      yield(z_old) if block_given?
    end
    z_old
  end

  def diverge?(c)
    iterate_map(Complex(0.0, 0.0), c) do |z|
      if z.abs > @threshold
        return true
      end
    end
    false
  end
end

class CalcMandelbrot
  def initialize(mandelbrot)
    @mandelbrot = mandelbrot
  end

  def calc(io, xrange, yrange, step_size)
    xrange.step(step_size) do |x|
      yrange.step(step_size) do |y|
        c = Complex(x, y)
        unless @mandelbrot.diverge?(c)
          io.puts "#{c.real}\t#{c.imag}"
        end
      end
    end
  end

  def calc_save(basename, *args)
    file = DRbQS::Temporary.file
    open(file, 'w') do |f|
      calc(f, *args)
    end
    DRbQS::Transfer.enqueue(file, compress: true, rename: basename) # Return basename.
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
drbqs-0.1.1 examples/mandelbrot/mandelbrot.rb
drbqs-0.1.0 examples/mandelbrot/mandelbrot.rb
drbqs-0.0.19 example/mandelbrot/mandelbrot.rb
drbqs-0.0.18 example/mandelbrot/mandelbrot.rb
drbqs-0.0.17 example/mandelbrot/mandelbrot.rb
drbqs-0.0.16 example/mandelbrot/mandelbrot.rb