0

I want to draw Mandelbrot to PPM file in C. My code is working but my drawing is always black. I have this code from wikia. The key to my success is to think "alfa" (i think so). I have no idea what alfa should be. Here's my code:

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

#define W 800
#define H 800

//rgb struct
struct RGB {
    int r;
    int g;
    int b;
};
struct RGB picture[W][H];

void draw() {
int i, j, iteration, max_iteration, alfa, color;
float x, y, x0, y0, xtemp;

for(i=0;i<W;++i)
{
    for(j=0;j<H;++j)
    {
         x0 = 1; //scaled x (e.g interval(-2.5, 1))
         y0 = -1; //scaled y (e.g interval(-1, 1))
         x = 0.0;
         y = 0.0;
         iteration = 0;
         max_iteration = 1000;

         while (x*x + y*y < 2*2 && iteration < max_iteration)
         {
             xtemp = x*x - y*y + x0;
             y = 2*x*y + y0;
             x = xtemp;
             iteration = iteration+ 1;
             alfa = x*y; //???
         }

         color = alfa * (iteration / max_iteration);

         picture[i][j].r = color;
         picture[i][j].g = color;
         picture[i][j].b = color;
    }
}
}

int main() {

//variables
int i,j;

draw();

FILE *fp;
fp = fopen("picture.ppm", "w");
fprintf(fp,"P3\n#test\n%d %d\n256\n", W, H);

for (i=0; i < W; ++i)
    {
        for (j=0; j < H; ++j)
        {
        fprintf(fp,"%d %d %d ", picture[i][j].r, picture[i][j].g , picture[i][j].b);
        }
    fprintf(fp, "\n");
    }

fclose(fp);

return 0;
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
upgrade11
  • 57
  • 8
  • 1
    `iteration` and `max_interation` are integers, so `(iteration / max_iteration)` is an integer division, which will always yield 0 when `iteration` is less than `max_iteration`. Remove the parentheses, so that the multiplication comes first. – M Oehm Jan 24 '16 at 19:12
  • Your itarative code probably needs to do something to the position `i` and `j`. You use them only when you assign the colours. Should you initialise `x0` and `y0` by means of `i` and `j`, e.g. by mapping them to the desires interval? – M Oehm Jan 24 '16 at 19:18
  • Finally, I think you should map the Mandelbrodt value to a colour in the range of 0 to 255. – M Oehm Jan 24 '16 at 19:27
  • If I understand correctly: 1-you pick up some random code on the internet, 2-it does not work as expected, 3-you are asking us to debug it. Please edit question and explain at least what you have tried already. – kebs Jan 24 '16 at 19:35
  • Thanks guys alfa = 255 :D – upgrade11 Jan 24 '16 at 21:16

1 Answers1

1

In this line

color = alfa * (iteration / max_iteration);

the divison iteration / max_iteration is int division whose result is always 0 (possibly 1 if ever iteration == max_iteration).

Try working in float, or rearrange like this

color = alfa * iteration / max_iteration;

removing the parentheses. But you'll have to watch out that the int range isn't broken.

Having said that, it seems you are not sure yourself what alfa is. I suggest 255 so you get a grey-scale image.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56