#import "Controller.h"

@implementation Controller

- (void) awakeFromNib
{
	[self resetParameters:nil];
	[self redraw:nil];
}

- (IBAction) resetParameters:(id)sender
{
	[lambdaField setDoubleValue:-2.7];
	[alphaField setDoubleValue:5.0];
	[betaField setDoubleValue:1.5];
	[gammaField setDoubleValue:1.0];
	[omegaField setDoubleValue:0.0];
	[nField setIntValue:6];
	[scaleField setDoubleValue:1.0];
	[iteratesField setIntValue:60000];
}

- (IBAction) redraw:(id)sender
{
//	NSLog(@"redraw");
	
	NSArray *colourArray = [[NSArray alloc] initWithObjects:[NSColor blueColor], 
													[NSColor redColor], 
													[NSColor greenColor], 
													[NSColor orangeColor], 
													[NSColor purpleColor], 
													[NSColor yellowColor], 
													[NSColor cyanColor], 
													[NSColor magentaColor], 
													nil];
	NSRect bounds = [imageView bounds];
	NSSize size = bounds.size;
	int pixelsWide = (int)size.width;
	int pixelsHigh = (int)size.height;
	
	NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
               pixelsWide:pixelsWide
               pixelsHigh:pixelsHigh
               bitsPerSample:8
               samplesPerPixel:3
               hasAlpha:NO
               isPlanar:NO
               colorSpaceName:NSDeviceRGBColorSpace
               bytesPerRow:pixelsWide * 3
               bitsPerPixel:24];
	
/*	int i;
	for(i=0; i<pixelsWide; i++) {
		int j;
		for(j=0; j<pixelsHigh; j++) {
			int u = (int)random() & 0x03;
			[imageRep setColor:[colourArray objectAtIndex:u] atX:i y:j];
		}
	} */
	
	double lambda = [lambdaField doubleValue];
	double alpha = [alphaField doubleValue];
	double beta = [betaField doubleValue];
	double gamma = [gammaField doubleValue];
	double omega = [omegaField doubleValue];
	int n = [nField intValue];
	double scale = [scaleField doubleValue];
	int maxIterates = [iteratesField intValue];

	
  	int *imageBuffer = (int *)malloc(pixelsWide*pixelsHigh*sizeof(int));
  	
  	int i, j;
  	for(i=0; i<pixelsWide; i++) {
  		for(j=0; j<pixelsHigh; j++) {
  			imageBuffer[i*pixelsWide + j] = 0;
  		}
  	}
  	
  	double scaleX = pixelsWide/(2.0*scale);
//  	double scaleY = pixelsHigh/(2.0*scale);
  	
  	double x = 0.01;
  	double y = 0.004;
  	double xnew;
  	double ynew;
  	
  	int iterate;
  	for(iterate=0; iterate<maxIterates; iterate++) {
  		double zzbar = x*x + y*y;
  		double zreal = x;
  		double zimag = y;
  		int i;
  		for(i=0; i<n; i++) {
  			double za = zreal*x - zimag*y;
  			double zb = zimag*x + zreal*y;
  			zreal = za;
  			zimag = zb;
  		}
  		double zn = x*zreal - y*zimag;
  		double p = lambda + alpha*zzbar + beta*zn;
  		xnew = p*x + gamma*zreal - omega*y;
  		ynew = p*y - gamma*zimag + omega*x;
  		x = xnew;
  		y = ynew;

  		int px = (int)(scaleX*(x + scale));
  		int py = (int)(pixelsHigh - scaleX*(y + scale));
		if ((px>=0) && (px<pixelsWide) && (py>=0) && (py<pixelsHigh)) {
			imageBuffer[px*pixelsWide + py]++;
		}
  	  	
  	}
  	
  	for(i=0; i<pixelsWide; i++) {
  		for(j=0; j<pixelsHigh; j++) {
  			int v = imageBuffer[i*pixelsWide + j];
  			v = (v<7)?v:7;
//  			gdImageSetPixel(im, i, j, colourList[v]);
			[imageRep setColor:[colourArray objectAtIndex:v] atX:i y:j];
  		}
  	}
	free(imageBuffer);

	NSImage *image = [[NSImage alloc] init];
	[image addRepresentation:imageRep];	
	[imageRep release];
	[image setSize:size];
	[imageView setImage:image];
	[image release];
	[colourArray release];
}

@end
