Mandelbrot screen display

I am having much trouble generating a screen display of the basic Mandelbrot set. What I get is superficially like a full Mandelbrot set but greatly different in detail. I have been planning to use this bit of code in an overview course in Windows programming for seniors (55+) next fall. Can someone look over the code and point out where I have gone wrong? I think the basic logic is right. I have tried declaring all the variables as global and casting all the integers as doubles to be sure I am not using data out of scope or loosing precision by mixing doubles and integers in some of the calculations. Still the same muddled display.

I compiled this code with Visual C++ Express 2010.

This is especially frustrating since I have a Windows program compiled from code I wrote over 20 yrs ago and it does exactly what I need. Of course, I have no idea where the source code is. That was many computers, many houses, and one marriage ago.

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static double maxFractalX=0.5,minFractalX=-2.0,maxFractalY=1.25,minFractalY=-1.25;
static int maxI=32,maxX,maxY;

switch (message)
{
case WM_SIZE:
maxX=LOWORD(lParam);
maxY=HIWORD(lParam);
break;

case WM_PAINT:
int i,x,y;
double FractalX, FractalY;
double newFractalX,newFractalY;
double oldFractalX; //new

hdc = BeginPaint(hWnd, &ps);

for(y=0;y<maxY;y++)
{
FractalY=minFractalY+((maxFractalY-minFractalY)*y/(maxY-1));
for (x=0;x<maxX;x++)
{
FractalX=minFractalX+((maxFractalX-minFractalX)*x/(maxX-1));
newFractalX=0;
newFractalY=0;

for(i=0;i<maxI;i++)
{
oldFractalX=newFractalX;
newFractalX=(newFractalX*newFractalX)-(newFractalY*newFractalY)+FractalX;
// newFractalY=(2*newFractalX*newFractalY)+FractalY; error
newFractalY=(2*oldFractalX*newFractalY)+FractalY; //new code
if(((newFractalX*newFractalX)+(newFractalY*newFractalY))>=4) break;
}
if(i==maxI)
SetPixel(hdc,x,y,0);


}
}

EndPaint(hWnd, &ps);
break;
Last edited on
I have discovered the problem with my coding. My coding was mixing the values for newFraxctalX from two different iterations. My correction is shown above. This code was designed to be easy to read rather that to be the most efficient for calculations. (I am planning to use this a course for seniors in the fall.)
Hey, It's working pretty nicely. I'm a bit new at Microsoft programming, so this is just a suggestion as far as examples you could show the class;

first start off with this program and show them that yes, it can be re-sized but it's kind of slow when resizing the window (Drawback of drawing everything to screen pixel-by-pixel).

Then show them a second program that builds in full-screen mode, takes a bitmap image of the mandelbrot, then displays the bmp for each new resizing (should run much faster when resizing). - This could be an example of graphics code that runs close to the machine (BMP) vs. code that has to run through the windproc/message loop, and how to use both in tandem. (maybe include a counting variable so you can show them the number of times the program goes through the loop before the whole picture is displayed)


(I also ran across something new for myself, if you were to run the code as you have it above without a "default:" case then the picture is drawn to the screen without a window, maybe don't show that in class, but It was news to me - had to use my compiler's stop button to end the paint. It was kind of neat seeing it displayed without a window though.)
Last edited on
Topic archived. No new replies allowed.