 /**************************************************************************\
(                                                                            )
 ) Klassisches Fließkomma-Mandelbrötchen.                   2015-08-20/Yeti (
(                                                                            )
 ) Basierend auf dem Code für ein monochromes Bild im PPM(-P6)-Format von   (
(  http://rosettacode.org/wiki/Mandelbrot_set#C plus einige unspektakuläre   )
 ) Änderungen zur Anpassung an den Propeller und das PGM(-P3)-Format.       (
(                                                                            )
 \**************************************************************************/

#include <propeller.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

void main()
{
	const int iXmax = 400;
	const int iYmax = 300;
	const int IterationMax=31;

	const double CxMin=-2.1;
	const double CxMax=0.7;
	const double CyMin=-1.25;
	const double CyMax=1.25;

	double PixelWidth=(CxMax-CxMin)/iXmax;
	double PixelHeight=(CyMax-CyMin)/iYmax;
	double Cx,Cy;
	double Zx,Zy;
	double Zx2,Zy2;
	int Iteration;
	const double EscapeRadius=2;
	double ER2=EscapeRadius*EscapeRadius;
	printf("P2\r# no comment.\r%d %d\r%d\r",iXmax,iYmax,IterationMax+1);
	for(int iY=0;iY<iYmax;iY++) {
		Cy=CyMin+iY*PixelHeight;
		for(int iX=0;iX<iXmax;iX++) {
			Cx=CxMin+iX*PixelWidth;
			Zx=0.0;
			Zy=0.0;
			Zx2=Zx*Zx;
			Zy2=Zy*Zy;
			for(Iteration=0;Iteration<IterationMax && ((Zx2+Zy2)<ER2);Iteration++) {
				Zy=2*Zx*Zy+Cy;
				Zx=Zx2-Zy2+Cx;
				Zx2=Zx*Zx;
				Zy2=Zy*Zy;
			}
			if (Iteration==IterationMax)
				puts("0");
			else
				printf("%d\r",Iteration);
		}
	}
	cogstop(0);
}
