GDI Mandelbrot Set generator not working...

Pages: 12
Okay, I've got a very basic exploration code implemented. it allows you to pan around the fractal by dragging with the left mouse button, and zoom (in at least) with the scroll wheel (alternatively, one can use the arrow keys & page up/down, or a combination of the two).

Here's the modded drawing routine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

*Global Scope*
float zoom= 200.0f;

int translX= -185,
	translY= -224;

int rectSizeX= 0,
	rectSizeY= 0;

...

*Routine Scope*
int middleX= (cr.right - cr.left) + translX;
int middleY= (cr.bottom - cr.top) + translY;

const double saturation= 0.5;
const double value= 0.7;
const double rotation= 120.0; //adjusts hue

const int maxIterations= 100;

rectSizeX= cr.right - cr.left;
rectSizeY= cr.bottom - cr.top;

for(int vPos= cr.top; vPos < cr.bottom; vPos++)
{
	for(int hPos= cr.left; hPos < cr.right; hPos++)
	{
		COLORREF color= NULL;
					
		double x0= (hPos - middleX) / zoom,y0= (vPos - middleY) / zoom;
		double x= x0,y= y0;

		double hue= 0,f= 0;
		int p,q,t;
		
		int iteration= 0;

		while((x*x) + (y*y) <= 4 && iteration < maxIterations)
		{
			double xTemp= x*x - y*y + x0;
			y= 2*x*y + y0;
			
			x= xTemp;

			iteration++;
		}

		if(iteration == maxIterations)
			color= RGB(0,0,0);
		else
		{
			hue = iteration * 360.0 / maxIterations + rotation;
			while( hue >= 360.0 ) hue -= 360.0;

			// Convert HSV to RGB
			f = hue / 60 - (int)(hue / 60);
			p = (int) (value * (1 - saturation) * 255);
			q = (int) (value * (1 - f * saturation) * 255);
			t = (int) (value * (1 - (1 - f) * saturation) * 255);
			switch( (int)(hue / 60) % 6 )
			{
				case 0: color = RGB( value, t, p );  break;
				case 1: color = RGB( q, value, p );  break;
				case 2: color = RGB( p, value, t );  break;
				case 3: color = RGB( p, q, value );  break;
				case 4: color = RGB( t, p, value );  break;
				case 5: color = RGB( value, p, q );  break;
				default: break;
			}
		}

		SetPixel(hdc,hPos,vPos,color);
	}
}


And the WndProc changes (for movement/zoom):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
...

case WM_MOUSEMOVE:
	if(wparam == MK_LBUTTON)
	{
		translX= LOWORD(lparam) - rectSizeX;
		translY= HIWORD(lparam) - rectSizeY;
		
		InvalidateRect(hwnd,NULL,true);
	}
	break;
case WM_MOUSEWHEEL:
	zoom+= HIWORD(wparam) / 60;
	
	InvalidateRect(hwnd,NULL,true);
	break;
case WM_KEYDOWN:
	switch(wparam)
	{
		case VK_NEXT:
			zoom-= 2;
			InvalidateRect(hwnd,NULL,true);
			break;
		case VK_PRIOR:
			zoom+= 2;
			InvalidateRect(hwnd,NULL,true);
			break;
		case VK_UP:
			translY-= 5;
			InvalidateRect(hwnd,NULL,true);
			break;
		case VK_DOWN:
			translY+= 5;
			InvalidateRect(hwnd,NULL,true);
			break;
		case VK_LEFT:
			translX-= 5;
			InvalidateRect(hwnd,NULL,true);
			break;
		case VK_RIGHT:
			translX+= 5;
			InvalidateRect(hwnd,NULL,true);
			break;
		default: break;
	}
	break;
Last edited on
Err, well, I'm now trying to implement zooming on the spot with the mouse wheel, but I'm having trouble with the math.

Here's the WM_MOUSEWHEEL code:

1
2
3
4
5
6
7
zoom+= HIWORD(wparam) / 30;

translX-= LOWORD(lparam) - rectSizeX;
translY-= HIWORD(lparam) - rectSizeY;

InvalidateRect(hwnd,NULL,true);
break;


But all this does is zoom the view and move it by an increment according to the position of the mouse cursor.

I've tried other algorithms, including dividing by the zoom factor, subtracting the client area size and then dividing by the zoom factor, dividing by the zoom factor divided by half of itself, etc., and none of them seem to work right... I know, I'm really bad at this, and maybe shouldn't be trying to leap ahead so far just yet, but this would greatly improve my ability to zoom around the fractal (I wouldn't have to back-track every time I zoom in).
Last edited on
Topic archived. No new replies allowed.
Pages: 12