Skip to content

Instantly share code, notes, and snippets.

@enebo
Last active February 8, 2020 06:48
Show Gist options
  • Save enebo/e6da2bb24ae886b7a5c7513d724d607f to your computer and use it in GitHub Desktop.
Save enebo/e6da2bb24ae886b7a5c7513d724d607f to your computer and use it in GitHub Desktop.
Mildly modified from some Mandelbrot site.
Mandelbrot
Convert to Ruby syntax.
Stretch goals if time:
parallelize computation
Use Complex
play with richer RGB
change ZOOM
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class Mandelbrot extends JFrame {
private final int MAX_ITER = 570;
private final double ZOOM = 150;
private BufferedImage image;
public Mandelbrot() {
super("Mandelbrot Set");
setBounds(100, 100, 800, 600);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < getHeight(); y++) {
for (int x = 0; x < getWidth(); x++) {
double zx = 0;
double zy = 0;
double cX = (x - 400) / ZOOM;
double cY = (y - 300) / ZOOM;
int iter = MAX_ITER;
while (zx * zx + zy * zy < 4 && iter > 0) {
double tmp = zx * zx - zy * zy + cX;
zy = 2.0 * zx * zy + cY;
zx = tmp;
iter--;
}
image.setRGB(x, y, iter | (iter << 8));
}
}
}
@Override
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}
public static void main(String[] args) {
new Mandelbrot().setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment