I am trying to make a julia set image and programming this in C . I have tried researching an algorithm to create an image of the Julia set but am getting confused by most of the examples online (as most examples seem to be copied but with little explanation of what happens step-by-step).
I have re-written my code several times to fit with algorithms I have found online but with little success. Currently, I am using an iteration function to define a colour of each pixel in the following:
// Loop through each pixel to work out new image set
int x, y;
for(x = 0; x < SIZE; x++){
for(y = 0; y < SIZE; y++){
int i = iterate(x, y, maxIterations, c);
image[x][y] = i % 256 + 255 + 255 * (i < maxIterations);
}
}
/* Iterate function */
int iterate(int x, int y, int maxI, double c[]){
double z, zx, zy, oldRe, oldIm; //real and imaginary parts of new and old
double xmin = -1.0, xmax = 1.0, ymin = -1.0, ymax = 1.0;
int k; // number of times iterated
//calculate the initial real and imaginary part of z
// z0 = (x + yi)^2 + c = (0 + 0i) + c = c
zx = 1.5*(x - SIZE/2)/(0.5*SIZE);
zy = 1.0*(y - SIZE/2)/(0.5*SIZE);
//start the iteration process
for(k = 1; k < maxI; k++){
//remember value of previous iteration
oldRe = zx;
oldIm = zy;
z = zx*zx - zy*zy + c[0];
//the actual iteration, the real and imaginary part are calculated
zx = oldRe * oldRe - oldIm * c[1] + c[0];
zy = 2 * oldRe * oldIm + c[1];
zy = 2.0*zx*zy + c[1];
zx = z;
//if the point is outside the circle with radius 2: stop
if((zx * zx + zy * zy) > 4) break;
}
/*
while(((zx * zx + zy * zy) > 4) && (k < maxI)) {
//remember value of previous iteration
z = zx*zx - zy*zy + c[0];
zy = 2*x*y-c[1];
zx = z;
//if the point is outside the circle with radius 2: stop
}
return k;
} */ The Commented out while loop is a second algorithm I found online but is also incorrect. I am not sure what I am missing from the equation. Any guesses?
(I am not looking for a solution, just some pseudocode that can convert the real, imaginary complex number to a position in my bounding box, in this case: -1,1).