Mandelbrot Generation Part 1: XNA Problems

Yeah, so I decided to try and make a Mandelbrot fractal generator, and while waiting for the C# download to finish I figured I might as well try and write it up just because.
So yeah. I don't have a degree in math or programming, I just like fractals, math and programming.
Okay, after reading the Wikipedia article I found this bit of pesudo-code:

For each pixel on the screen do:
  x0 = scaled x co-ordinate of pixel (must be scaled to lie somewhere in the interval (-2.5 to 1)
  y0 = scaled y co-ordinate of pixel (must be scaled to lie somewhere in the interval (-1, 1)
  x = 0
  y = 0
  iteration = 0
  max_iteration = 1000
  while ( x*x + y*y <= (2*2)  AND  iteration < max_iteration )
    xtemp = x*x - y*y + x0
    y = 2*x*y + y0
    x = xtemp
    iteration = iteration + 1
  if ( iteration == max_iteration )
    color = black
    color = iteration

This will be very useful, although we'll see if I actually end up doing it pixel-by pixel, as that seems kind of ineficient, we'll see.
I'll be writing this in C# and--for the drawing--XNA, because it's what I know. Speaking of that, the install has finished, I'm writing this on my laptop while on vacation, let's fire up a console project and see what I can whip up. I'd rather not have to worry about the graphics part with a full XNA project, and just be able to see how much I fail at math and programming. :p
Okay, I lied. I'm gonna start with the XNA of the project, I found some example C++ code that I can use:

double MinRe = -2.0;
double MaxRe = 1.0;
double MinIm = -1.2;
double MaxIm = MinIm+(MaxRe-MinRe)*ImageHeight/ImageWidth;
double Re_factor = (MaxRe-MinRe)/(ImageWidth-1);
double Im_factor = (MaxIm-MinIm)/(ImageHeight-1);
unsigned MaxIterations = 30;
for(unsigned y=0; y<ImageHeight; ++y)
    double c_im = MaxIm - y*Im_factor;
    for(unsigned x=0; x<ImageWidth; ++x)
        double c_re = MinRe + x*Re_factor;
        double Z_re = c_re, Z_im = c_im;
        bool isInside = true;
        for(unsigned n=0; n<MaxIterations; ++n)
            double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
            if(Z_re2 + Z_im2 > 4)
                isInside = false;
            Z_im = 2*Z_re*Z_im + c_im;
            Z_re = Z_re2 - Z_im2 + c_re;
        if(isInside) { putpixel(x, y); }
Of course, that wouldn't work if you just dropped it into a C++ compiler, as it needs some kind of graphics library, which is what I'm using XNA for.
Of course, just when I think I'm ready, I find out I accidentally installed Visual Basic instead of Visual C#. Dammit, back to the website to find the right download. This laptop is very slow. It makes me sad.
Okay, after way too long, Visual C# is installed.
Visual C# installed and now runs correctly, as does XNA. I also need to find a different word processor that's not Word. I'll work on that later.
For now I need to figure out how to set specific pixels to colors, as I'm going to try the above code method of generating the Mandelbrot, then see what I can do from there.
So, a little bit of googling shows that what I should probably use is the Texture2D's SetPixel function, so let's give that a shot.
Aand now it tells me that I don't have a good enough graphics card to run XNA. I'll admit, my laptop doesn't have shit in the way of a graphics card, but it can run League of Legends at lowest settings at 20 FPS, I think it can handle a 2D XNA game..
Okay, on the advice of a friend, I've decided to try SFML (Simple and Fast Multimedia LIbrary.) Hopefully this'll work fairly well. It looks pretty simple and sprite-based, which is fine so long as I can set pixel color, and I'd be surprised if I couldn't.
Okay, awesome. SFML is actually simpler to set up than XNA, and it's got what I need and more likely more. It's not as pretty as XNA, but that's completely fine.
At this point this is what I have:

It might not look like much, but it's a fairly large accomplishment. Keen-eyed readers will notice that there is one pixel of red in that 10x10 white square in the upper left, that's my test to set pixels to specific colors. It's actually simpler than in XNA, this is the code that setse art:
Image image = new Image(10, 10, Color.White);
image.SetPixel(0, 0, Color.Red);
Sprite sprite = new Sprite(image);
Just a test, of course, but still very simple, then it's rendered by doing 
And that's it. Couldn't be simpler. We'll see what problems I run into later.


Post a Comment