Now we’re going to create a function to decide if some complex number c is in the set.
We’re going to define a function int is_in_set(std::complex<double> c)
this function will decide if the complex number c is in the set and return to us 0
if it is. Otherwise it will return some number depending on how many iterations it took to decide that it was not.
intis_in_set(std::complex<double>c){// create a complex number z
//
std::complex<double>z(0,0);// check if z ever exceeds our set limit of 10
// and return how many iterations it took to determin that
//
for(inti=0;i<2500;i++){z=std::pow(z,2)+c;if(std::norm(z)>10){returni;}}// if z never exceeded our bounds return 0
//
return0;}
This function is all you need to decide if some point c is in the set or not. We take
a c initialize z to (0,0) then iterate 2500 times to see if the squared magnitude
of z ever exceeds 10 (an arbitrary number I made up, feel free to change it to whatever you want).
Next we’re going to loop over x and y and run the is_in_set function for
every (x,y) to see if the (x,y) combination lands in the set.
1
2
3
4
5
6
7
8
9
10
11
12
intiters=0;for(doublex=-2.0;x<2.0;x+=0.001){for(doubley=-2.0;y<2.0;y+=0.001){iters=is_in_set(std::complex<double>(x,y);// If iters == 0 paint this point black.
// Otherwise paint the point
// a color depending on the number of iters
}}
doing this will give you something similar to this.