import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;

public class Mandelbrot {

	public static void main(String[] args){
		int[] colors = new int[2048];
		for(int i = 0; i < colors.length; i++){
			colors[i] = Color.HSBtoRGB(i/256f, 1, i/(i+8f));
		}

		double re = Double.parseDouble(args[2]);
		double imag = Double.parseDouble(args[3]);
		double r = Double.parseDouble(args[4]);
		int width = Integer.parseInt(args[0]);
		int height = Integer.parseInt(args[1]);
		int max = 2048;
		BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		for(int row = 0; row < height; row++){
			for(int col = 0; col < width; col++){
				double c_re = re + (col - width/2.0)*4.0/width*r;
				double c_im = imag + (row - height/2.0)*4.0/width*r;
				double x = 0, y = 0;
				int iteration = 0;
				while(x*x+y*y <= 4 && iteration < max){
					double x_new = x*x - y*y + c_re;
					y = 2*x*y + c_im;
					x = x_new;
					iteration++;
				}
				if(iteration < max){
					img.setRGB(col, row, colors[iteration % colors.length]);
				}else{
					img.setRGB(col, row, 0);
				}
			}
		}
		try {
			ImageIO.write(img, "png", new File("./m2.png"));
		}catch(Exception e){
			System.err.println("Error: ");
			e.printStackTrace();
			System.exit(1);
		}
	}

}
