Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Thursday 25 December 2014

Towards a zoom-able mandelbrot ruby-processing

Working toward a zoom-able mandelbrot (albeit somewhat speed limited in ruby-processing)
Parameter = Struct.new(:cx, :cy, :ch, :cw) # required for future

RATIO = 1.336
TMAX = 5_000

attr_reader :current, :default # required for future work

def setup
  size(534, 400, P2D)
  @default = Parameter.new(-0.53 - (1.4 * RATIO), -1.4, 2.8, 2.8 * RATIO)
  @current = default
  load_pixels
  no_fill
  stroke(255)
  create_mandelbrot(width, height)
  no_loop
end

def create_mandelbrot(w, h)
  y_zero = ->(y, param) { y * param.ch / height + param.cy }
  x_zero = ->(x, param) { x * param.cw / width + param.cx }
  h.times do |j|
    y0 = y_zero.call(j, current)
    w.times do |k|
      x0 = x_zero.call(k, current)
      x, y = 0, 0
      xsqr, ysqr = 0.0, 0.0
      t = 0
      while t < TMAX && (xsqr + ysqr < 4.0)
        y = x * y
        y += y + y0
        x = xsqr - ysqr + x0
        xsqr = x * x
        ysqr = y * y
        t += 1
      end
      if t < TMAX
        smth = t - Math.log(xsqr + ysqr)
        pixels[k + j * width] = Java::JavaAwt::Color.HSBtoRGB(smth / 500, 0.8, 0.9)
      else
        pixels[k + j * width] = 0
      end
    end
  end
  update_pixels
end

No comments:

Post a Comment

Followers

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2