mandelbrot set issues

closed account (2EURX9L8)
I thought I had this but I most certainly don't.

The code I have written so far does not display anything like what a mandelbrot looks like.

The assignment instructions are to write a program that creates a 512x512-pixel mandelbrot image in ppm format.

The program should accept three doubles as input from a file called "mandelinput.txt": Re0, Im0, and Re1. Im1 should be calculated as Im0 + (Re1-Re0) (i.e.,it should generate a square mandelbrot image).


The code I have is as follows, can anyone please tell me what the hell I am doing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <fstream>
#include <iostream>

using namespace std;

const int maxiters = 500;
const int xpixels = 512;
const int ypixels = 512;


double red(int c){ 
	return ((c)%16)*64;          
}
double blue(int c){
	return 0;
}
double green(int c){
	return c;
}

int getCount(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(){
	ofstream outFile;
	ifstream inFile;
	outFile.open("mandeloutput.ppm");

	
	
	double x0, y0, x1,y1=0;
	double x,y,i,j,c;
	
	inFile.open("mandelinput.txt");
		if(!inFile)
			cout<<"file open error.\n";
		else{
			while(!inFile.eof())
				inFile>>x0>>y0>>x1;}

	 y1=y0+(x1-x0);

	 outFile << "P3\n" << xpixels << " " << ypixels << "\n255\n";

	 for(j=0;j<ypixels; ++j)
	 {
		 x=x0+(x1-x0)/xpixels*j;
	 
		 for(i=0;i<xpixels; ++i)
		 {
		 y=y0+(y1-x0)/ypixels*i;
	 
		c=getCount(x,y);
			if(c==maxiters)
				outFile<<"0 0 0";
			else
				outFile<<red(c)<<" ";
				outFile<<blue(c)<<" ";
				outFile<<green(c)<<" ";
		}
	 outFile<<endl;
	 }
	return 0;
}
Could you post mandelinput.txt?
closed account (2EURX9L8)
-2.0
1.0
-1.2

Ok so I must be reading it wrong from the file, cause even though it still dosent look like a mandelbrot, its at least square when I assign values to x0,y0 and x1.
Last edited on
That region looks pretty much like that. It's not a particularly interesting region. Try
-3.0
-3.0
3.0


You did have a subtle bug in your code, though:
1
2
3
4
5
6
7
if(c==maxiters)
	outFile<<"0 0 0 "; // <- missing space screws up color
else{ // <- missing braces
	outFile<<saturate(red(c))<<" ";
	outFile<<saturate(blue(c))<<" ";
	outFile<<saturate(green(c))<<" ";
}
saturate() is simply this:
1
2
3
4
5
6
7
int saturate(double x){
	if (x<0)
		return 0;
	if (x>255)
		return 255;
	return (int)x;
}


PS: It's also worth noting that you're putting the real axis vertically. By convention, it goes horizontally, so you might want to change that.
Last edited on
Topic archived. No new replies allowed.