Skip to content

Commit

Permalink
mandelbrot set
Browse files Browse the repository at this point in the history
  • Loading branch information
erik committed Nov 24, 2010
1 parent 6e1f6d8 commit 3ef8588
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions sample/mandelbrot.rb
@@ -0,0 +1,57 @@
# awesome (but insanely slow) Mandelbrot set drawing
# feel free to mess around with various variables to
# find a better / faster solution

require 'rbdraw'
include Draw

# higher resolutions are *much* nicer, but 400, 300
# should complete in ~30 seconds
$w = 400
$h = 300

w = Window.new

w.height = $h
w.width = $w

w.show

g = w.graphics

MAX_ITER = 255

g.sync_on_draw false

# this strategy was shamelessly stolen from RosettaCode
# http://rosettacode.org/wiki/Mandelbrot_set#haXe

sx = 3.0 / ($w - 1)
sy = 2.0 / ($h - 1)

(0...$h).each {|y|
ci = y * sy - 1.0
(0...$w).each {|x|
k = 0
zr = 0
zi = 0
cr = x * sx - 2.0

while k <= MAX_ITER and zr * zr + zi * zi <= 4
temp = zr * zr - zi *zi + cr
zi = 2 * zr * zi + ci
zr = temp
k += 1
end

g.color k > MAX_ITER ? 0 : k * 0x010
g.point(x, y)

}
# Sync after every line, makes drawing less choppy
g.sync
}
g.sync

gets

0 comments on commit 3ef8588

Please sign in to comment.