// C sucks

/*
	gcc -std=c99 burning.c && \
	./a.out 1600 1600 512 8000 -1.78 -0.035 > big.pgm && \
	convert big.pgm big.tiff && \
	open -a preview big.tiff
*/

#include <stdio.h>
#include <stdlib.h>
#include <complex.h>

unsigned fractal(double x, double y, unsigned depth) {
	double complex z = x + I * y;
	double complex o = 0 + I*0;
	for (unsigned i = 0; i < depth; i++) {
		if (cabs(o) <= 2.0) {
			o = cabs(creal(o)) + I* cabs(cimag(o));
			o = cpow(o, 2.0) + z;
		} else { return i; } }
	return 0; }

int main(int argc, char* argv[]) {
	if (argc < 7) {
		perror("width, height, depth, zoom factor, x, y; produces a PGM"); }
	
	unsigned W = atoi(argv[1]);
	unsigned H = atoi(argv[2]);
	unsigned depth = atoi(argv[3]);
	double zoom = atof(argv[4]);
	double xd = atof(argv[5]);
	double yd = atof(argv[6]);
	
	printf("P2\n%i %i\n%i\n", W, H, depth);
	double x, y;
	for (unsigned i = 0; i < H; i++) {
		y = (double)i - H/2;
		y = y/zoom + yd;
	
		for (unsigned j = 0; j < W; j++) {
			x = (double)j - W/2;
			x = x/zoom + xd;
	
			printf("%u ", fractal(x, y, depth) ); }
		printf("\n"); } }

// C sucks
