I have some code that generates pixel values for a mandelbrot image. I haven't added the code to output to the file yet; right now I'm trying to figure out why it loops infinitely. I want it to generate a 512x512 pixel image, but as I mentioned, it just keeps on going. Any help would be appreciated. Forgive me for programming ignorance. Here's what I've got so far:
Code:
#include <iostream>
using namespace std;
const int maxiters = 255;
const int xpixels = 512;
const int ypixels = 512;
int getGray(int c) { return ((c)%8)*32; }
int getRed(int c) { return c; }
int getGreen(int c){ return 0; }
int getBlue(int c) { return 255-((c)%16)*32; }
int getMbrotCount(double a0, double b0){
int count=0;
double a=a0;
double b=b0;
double anext,bnext;
while(a*a+b*b<4.0 && count < maxiters ){
anext = a*a - b*b + a0;
bnext = 2.0*a*b +b0;
a=anext;
b=bnext;
count++;
}
return count;
}
int main(){
double x0=-2.0;
double y0=1.5;
double x1=1.0;
double y1=-1.5;
double x,y;
int c;
int i,j;
cout << "P3\n" << xpixels << " " << ypixels << "\n255\n";
for(i=0;i<ypixels;++i){
y=y0+(y1-y0)/ypixels*i;
for(j=0;j<xpixels;++j){
x=x0+(x1-x0)/xpixels*j;
c=getMbrotCount(x,y);
if(c==maxiters) cout << "0 0 0 ";
else{
cout << getRed(c) << " ";
cout << getGreen(c) << " ";
cout << getBlue(c) << " ";
}
}
cout << endl;
}
return 0;
}