public class FPMandelbrot {
	/**
	 * Determines whether a point (x_0,y_0) is part of the Mandelbrot Set.
	 * We assume membership if the magnitude of the point's vector remains 
	 * in the range [0.0,2.0] after a specified maximum number of iterations.
	 * @param x_0 The real component (in 4.28 fixed point)
	 * @param y_0 The imaginary component (in 4.28 fixed point)
	 * @param max_iter The maximum number of iterations to perform
	 * @returns The number of iterations before the point is disqualified (or max_iter if it is not)
	 **/
    public static int mandlebrot_point(int x_0, int y_0, int max_iter) {
		int x = 0, y = 0;
		int iter = 0;
		
		while(true) {
			int x_sq = (int) ( ( (long)x * (long)x ) >> 28 );
			int y_sq = (int) ( ( (long)y * (long)y ) >> 28 );
			int xy =   (int) ( ( (long)x * (long)y ) >> 28 );
			
			int two_xy = xy + xy;
			int mag_sq = x_sq + y_sq;
			
			if (mag_sq > 4 << 28)
				break;
			if (iter >= max_iter)
				break;
				
			x = x_sq - y_sq + x_0;
			y = two_xy + y_0;
			
			iter++;
		}
		return iter;
	}
	
	public static void main(String[] args) {
		int x_0 = (int)(Double.parseDouble(args[0]) * (1 << 28));
		int y_0 = (int)(Double.parseDouble(args[1]) * (1 << 28));
		
		System.out.println("x_0: " + x_0 + " y_0: " + y_0 + " iter: " + mandlebrot_point(x_0, y_0,255));
	}
}
