Computer Graphics: Fractals generation using Mandelbrot Set with C/C++ in Code::Blocks
“Clouds are not spheres, mountains are not cones, coastlines are not circles, and bark is not smooth, nor does lightning travel in a straight line.” – Benoit Mandelbrot
The Mandelbrot set is created by a general technique where a function of the form
zn+1 = f(zn)
is used to create a series of a complex variable. In the case of the Mandelbrot, the function is
f(zn) = zn2 + zo
This series is generated for every initial point zo on some partition of the complex plane separating the complex plane into two categories:
Output
destination source:https://www.programming-techniques.com/?p=98
The Mandelbrot set is created by a general technique where a function of the form
zn+1 = f(zn)
is used to create a series of a complex variable. In the case of the Mandelbrot, the function is
f(zn) = zn2 + zo
This series is generated for every initial point zo on some partition of the complex plane separating the complex plane into two categories:

Source Code :
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | #include <windows.h> #include <stdio.h> #include <math.h> #define ROUND(a) ((int) (a + 0.5)) /* set window handle */ static HWND sHwnd; COLORREF color; typedef struct { float x, y; } Complex; void SetWindowHandle(HWND hwnd){ sHwnd=hwnd; } void setPixel(int x,int y,COLORREF color){ if(sHwnd==NULL){ MessageBox(NULL,"sHwnd was not initialized !","Error",MB_OK|MB_ICONERROR); exit(0); } HDC hdc=GetDC(sHwnd); SetPixel(hdc,x,y,color); ReleaseDC(sHwnd,hdc); return; } Complex complexSquare(Complex c){ Complex cSq; cSq.x = c.x * c.x - c.y * c.y; cSq.y = 2 * c.x * c.y; return cSq; } int iterate (Complex zInit, int maxIter){ Complex z = zInit; int cnt = 0; while((z.x * z.x + z.y * z.y <= 4)&& (cnt < maxIter)){ z = complexSquare(z); z.x += zInit.x; z.y += zInit.y; cnt++; } return cnt; } void madelbrot(int nx, int ny, int maxIter, float realMin, float realMax, float imagMin, float imagMax){ float realInc = (realMax - realMin) / nx; float imagInc = (imagMax - imagMin) / ny; Complex z; int x, y; int cnt; for(x = 0, z.x = realMin; x < nx; x++, z.x += realInc) for(y = 0, z.y = imagMin; y < ny; y++, z.y += imagInc ){ cnt = iterate(z, maxIter); if(cnt == maxIter) color = RGB(255, 0, 0); else{ color = RGB(0, cnt, 0); } setPixel(x, y, color); } } /* Window Procedure WndProc */ LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){ switch(message){ case WM_PAINT: SetWindowHandle(hwnd); madelbrot(750, 500, 500, -2.4,0.8,-1.4 ,1.4); break; case WM_CLOSE: // FAIL THROUGH to call DefWindowProc break; case WM_DESTROY: PostQuitMessage(0); return 0; default: break; // FAIL to call DefWindowProc // } return DefWindowProc(hwnd,message,wParam,lParam); } int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow){ static TCHAR szAppName[] = TEXT("Fractals"); WNDCLASS wndclass; wndclass.style = CS_HREDRAW|CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; // Register the window // if(!RegisterClass(&wndclass)){ MessageBox(NULL,"Registering the class failled","Error",MB_OK|MB_ICONERROR); exit(0); } // Createw indow // HWND hwnd=CreateWindow(szAppName,"Mandelbrot Fractal - Programming Techniques", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); if(!hwnd){ MessageBox(NULL,"Window Creation Failed!","Error",MB_OK); exit(0); } // ShowWindow and UpdateWindow // ShowWindow(hwnd,iCmdShow); UpdateWindow(hwnd); // Message Loop // MSG msg; while(GetMessage(&msg,NULL,0,0)){ TranslateMessage(&msg); DispatchMessage(&msg); } /* return no error to the operating system */ return 0; } |
Output
1 | <a href="http://lh5.ggpht.com/-jhvgSQpawZg/T1zNGEe8vsI/AAAAAAAAASU/QU37ocmJvpw/s1600-h/Mandelbrot%252520Set%25255B4%25255D.jpg"><img style="display: inline; border-width: 0px;" title="Mandelbrot Set" src="http://lh4.ggpht.com/-GoSyQm15LN0/T1zNHrOVhjI/AAAAAAAAASc/vOXy0WDW0NU/Mandelbrot%252520Set_thumb%25255B2%25255D.jpg?imgmax=800" alt="Mandelbrot Set" width="454" height="333" border="0" /></a> |

Related Article
destination source:https://www.programming-techniques.com/?p=98