Skip to content

Instantly share code, notes, and snippets.

@enebo
Created February 8, 2020 06:46
Show Gist options
  • Select an option

  • Save enebo/a17836017715da2c9423cd622de58019 to your computer and use it in GitHub Desktop.

Select an option

Save enebo/a17836017715da2c9423cd622de58019 to your computer and use it in GitHub Desktop.
java_import java.awt.image.BufferedImage
java_import javax.swing.JFrame
class Mandelbrot < JFrame
MAX_ITER, ZOOM = 1200, 350.0
def initialize
super("Mandelbrot Set")
setBounds(100, 100, 800, 600)
setDefaultCloseOperation(EXIT_ON_CLOSE)
@image = BufferedImage.new(width, height, BufferedImage::TYPE_INT_RGB);
now = Time.now
threads = []
sub_divide = 2 # 2
(0...height).each_slice(height/sub_divide) do |x|
(0...width).each_slice(width/sub_divide) do |y|
threads << Thread.new { render(x, y) }
end
end
p threads.size
threads.each { |t| t.join }
p Time.now - now
end
def paint(g)
g.drawImage(@image, 0, 0, self);
end
def render(h, w)
h.each do |y|
w.each do |x|
zx, zy = 0.0, 0.0
cX, cY = (x - 400) / ZOOM, (y - 300) / ZOOM
iter = MAX_ITER
while (zx * zx + zy * zy < 4 && iter > 0)
zy, zx = 2.0 * zx * zy + cY, zx * zx - zy * zy + cX
iter -= 1
end
@image.setRGB x, y, iter | (iter << 8)
end
end
end
end
Mandelbrot.new.setVisible(true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment