マンデルブロ集合 | JS版 |
/* * Draw Mandelbrot Set in C (Win32API) * https://mind.kittttttan.info/c/mandelbrot * * How to compile with GCC: * $ gcc -mwindows -o mandelbrot mandelbrot.c */ #include <windows.h> #define LIMIT 256 #define ZOOM 128.0 #define WIDTH 384 // (int)ZOOM * 3 #define HEIGHT 414 // WIDTH + 30 int data[WIDTH][HEIGHT]; int lim(double x, double y); void calc(void); HWND Create(HINSTANCE hInst); LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nCmdShow) { HWND hwnd; MSG msg; hwnd = Create(hInstance); if (hwnd == NULL) { return -1; } calc(); while (GetMessage(&msg, NULL, 0, 0)) { DispatchMessage(&msg); } return msg.wParam; } int lim(double x, double y) { int i, c; double px, py, nx, ny; c = 0; px = py = 0.0; for (i = 0; i < LIMIT; i++) { nx = px * px - py * py + x; ny = 2.0 * px * py + y; if (nx * nx + ny * ny > 4.0) { c = i + 1; break; } else { px = nx; py = ny; } } if (!c) { c = LIMIT; } return c; } void calc(void) { int i, j; for (i = 0; i < WIDTH; ++i) { for (j = 0; j < HEIGHT; ++j) { data[i][j] = lim(i / ZOOM - 2, j / ZOOM - 1.5); } } } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { HDC hdc; PAINTSTRUCT ps; int i, j, c; switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); for (i = 0; i < WIDTH; ++i) { for (j = 0; j < HEIGHT; ++j) { if (data[i][j] < LIMIT) { c = data[i][j] << 4; if (c > 0xff) { c = 0xff; } } else { c = 0; } SetPixel(hdc, i, j, RGB(0, 0, c)); } } EndPaint(hwnd, &ps); return 0; } return DefWindowProc(hwnd, msg, wp, lp); } HWND Create(HINSTANCE hInstance) { WNDCLASS winc; winc.style = 0; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL, IDI_APPLICATION); winc.hCursor = LoadCursor(NULL, IDC_ARROW); winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = TEXT("Main"); if (!RegisterClass(&winc)) { return NULL; } return CreateWindow( TEXT("Main"), TEXT("Mandelbrot"), WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, NULL, NULL, hInstance, NULL); }