Skip to content

Instantly share code, notes, and snippets.

@neuro-sys
Created November 27, 2012 22:42
Show Gist options
  • Select an option

  • Save neuro-sys/4157677 to your computer and use it in GitHub Desktop.

Select an option

Save neuro-sys/4157677 to your computer and use it in GitHub Desktop.
mandel.c
#include <windows.h>
#define W 640
#define H 400
const double xCentre = -0.75;
const double yCentre = +0.0;
const int nx = W;
const int ny = H;
const double dxy = 0.005;
#define UCHAR_MAX 255
void draw_fractal(HDC hdc)
{
double cx, cy;
double zx, zy, new_zx;
unsigned char n;
int i, j;
for (j = 0; j < ny; j++)
{
cy = yCentre + (j - ny / 2) * dxy;
for (i = 0; i < nx; i++)
{
cx = xCentre + (i - nx / 2) * dxy;
zx = 0.0;
zy = 0.0;
n = 0;
while ((zx * zx + zy * zy < 4.0) && (n != UCHAR_MAX))
{
new_zx = zx * zx - zy * zy + cx;
zy = 2.0 * zx * zy + cy;
zx = new_zx;
n++;
}
UINT col;
n -= 255;
if (n < UCHAR_MAX/2-1)
col = RGB(n*6, n*2, n*8);
else
col = RGB(255, n/10, n/2);
SetPixel(hdc, i, j, col);
}
}
}
HWND g_hWnd;
LRESULT CALLBACK WinProc(HWND hWnd,UINT msg, WPARAM wParam,LPARAM lParam)
{
HDC hDc;
PAINTSTRUCT pt;
switch (msg) {
case WM_PAINT:
hDc = GetDC(hWnd);
BeginPaint(hWnd,&pt);
draw_fractal(hDc);
EndPaint(hWnd, &pt);
ReleaseDC(hWnd, hDc);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASS wc;
MSG msg;
ZeroMemory(&wc, sizeof(wc));
wc.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDC_ICON);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject(COLOR_WINDOW);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GDI_TEST";
if (!RegisterClass(&wc))
return 0;
g_hWnd = CreateWindow("GDI_TEST", "Foo", WS_OVERLAPPED | WS_SYSMENU,
0, 0, W, H, NULL, NULL, hInstance, NULL);
if (!g_hWnd)
return 0;
ShowWindow(g_hWnd, SW_SHOW);
while (GetMessage(&msg, g_hWnd, 0, 0) > 0)
{
if (msg.message == WM_QUIT)
return msg.wParam;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
int foo()
{
WinMain(NULL, NULL, NULL, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment