Created
February 8, 2020 06:46
-
-
Save enebo/a71c1e7a328dc53865bfd475db90cc11 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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