# @param [float] val # @param [range(start1 .. end1)] r_in # @param [range(start2 .. end2)] r_out # @return [float] mapped value # def map1d(val, r_in, r_out) r_out.first + (r_out.last - r_out.first) * ((val - r_in.first).to_f / (r_in.last - r_in.first)) end # usage map1d(5, (0 .. 10), (10 .. 110)) exclude end is false puts map1d(5, (0 .. 10), (10 .. 110)) # output 60.0Here is the method in action
# Mandelbrot Set example # by Jordan Scales (http://jordanscales.com) # 27 Dec 2012 # Modified to use map1d (instead of map), and somewhat # optimized (update_pixels instead of set, and hypot for abs) # default size 900x600 # no need to loop def setup size 900, 600 load_pixels no_loop end # main drawing method def draw (0 ... 900).each do |x| (0 ... 600).each do |y| c = Complex.new(map1d(x, (0 .. 900), (-3 .. 1.5)), map1d(y, (0 .. 600), (-1.5 .. 1.5))) # mandel will return 0 to 20 (20 is strong) # map this to 0, 255 (and flip it) pixels[x + y * 900] = color(255 - map1d(mandel(c, 20), (0 .. 20), (0 .. 255)).to_i) end end update_pixels end # calculates the "accuracy" of a given point in the mandelbrot set # : how many iterations the number survives without becoming chaotic def mandel(z, max = 10) score = 0 c = z.clone while score < max # z = z^2 + c z.square z.add c break if z.abs > 2 score += 1 end score end # rolled my own Complex class # stripped of all functionality, except for what I needed (abs, square, add, to_s) # # Using this class, runs in ~12.5s on my MacBook Air # compared to ~22s using ruby's Complex struct class Complex attr_accessor :real, :imag def initialize(real, imag) @real = real @imag = imag end # square a complex number - overwriting it def square r = real * real - imag * imag i = 2 * real * imag @real = r @imag = i end # add a given complex number def add(c) @real += c.real @imag += c.imag end # compute the magnitude def abs hypot(real, imag) end def to_s "#{real} + #{imag}i" end end
PS: I have not tested whether Jordans findings re: ruby complex implementation still hold true (jruby truffle claims wicked fast mandelbrot calculations). Benchmarking is tricky with jvm warmup up time, I have replaced sqrt(real * real + imag * imag) (abs method) with hypot(real, imag), which should be more efficient...
No comments:
Post a Comment