文号
835023
阅读
4328
回复
0
收藏
1
C语言画Mandelbrot和Julia集合(WinAPI版)
2017/06/18
IP:四川

Mandelbrot集合的定义是:设Z0=0+0i,C=复平面上的点,根据Zn=Z(n-1)^2+C递推出后面的Zn,若|Zn|<2,则属于该集合。

Julia集合的定义是:设Z0=复平面上的点,C=a+bi,根据Zn=Z(n-1)^2+C递推出后面的Zn,若|Zn|<2,则属于该集合。

mandelbrot.png

julia.png

#include <windows.h>
#include <tchar.h>
#include <math.h>

HINSTANCE hInst;
HWND hMainWnd;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void PaintMandelbrot(HWND, HDC, double, double, double, double, int, int);
void PaintJulia(HWND, HDC, double, double, double, double, int, int, double, double);
void CExpoInt(double *, double *, int);

typedef COLORREF(WINAPI*Pshlwapi_ColorHLSToRGB)(WORD, WORD, WORD);
HMODULE hshlwapi;
Pshlwapi_ColorHLSToRGB shlwapi_ColorHLSToRGB;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	WNDCLASSEX wcex;
	RECT rc;
	MSG msg;

	hshlwapi = LoadLibrary(_T("shlwapi.dll"));
	if (!hshlwapi)
		return 0;
	shlwapi_ColorHLSToRGB = (Pshlwapi_ColorHLSToRGB)GetProcAddress(hshlwapi, "ColorHLSToRGB");
	if (!shlwapi_ColorHLSToRGB)
		return 0;

	hInst = hInstance;
	
	wcex.cbSize = sizeof wcex;
	wcex.style = CS_VREDRAW|CS_HREDRAW;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = _T("MainWndClass");
	wcex.hIconSm = wcex.hIcon;
	if (!RegisterClassEx(&wcex))
		return 0;

	rc.left = rc.top = 0;
	rc.right = rc.bottom = 480;
	AdjustWindowRectEx(&rc, WS_OVERLAPPEDWINDOW, FALSE, 0);
	hMainWnd = CreateWindowEx(
		0, wcex.lpszClassName, _T("Mandelbrot and Julia"), WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top,
		NULL, NULL, hInstance, NULL);
	if (!hMainWnd)
		return 0;

	ShowWindow(hMainWnd, nShowCmd);
	UpdateWindow(hMainWnd);

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	if (msg == WM_LBUTTONDOWN)
	{
		HDC hdc = GetDC(hWnd);
		PaintMandelbrot(hWnd, hdc, -2.2, -2.2, 2.2, 2.2, 16, 2);
		ReleaseDC(hWnd, hdc);
		return 0;
	}
	if (msg == WM_RBUTTONDOWN)
	{
		HDC hdc = GetDC(hWnd);
		PaintJulia(hWnd, hdc, -2.2, -2.2, 2.2, 2.2, 16, 2, 0.4, 0.3);
		ReleaseDC(hWnd, hdc);
		return 0;
	}
	if (msg == WM_DESTROY)
	{
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}

void CExpoInt(double *preal, double *pimag, int expo)
{
	double real, imag, treal, timag;
	int i;
	real = *preal;
	imag = *pimag;
	for (i = 1; i < expo; i++)
	{
		treal = *preal;
		timag = *pimag;
		*preal = treal * real - timag * imag;
		*pimag = timag * real + treal * imag;
	}
}

void PaintMandelbrot(HWND hWnd, HDC hdc, double fromx, double fromy, double tox, double toy, int iter, int expo)
{
	RECT rc;
	int width, height, i, j, k, value;
	double Creal, Cimag;
	double Zreal, Zimag;

	GetClientRect(hWnd, &rc);
	width = rc.right - rc.left;
	height = rc.bottom - rc.top;

	for (i = 0; i < height; i++)
	{
		Creal = 0;
		Cimag = fromy + (toy - fromy) * i / (double)height;
		for (j = 0; j < width; j++)
		{
			Creal = fromx + (tox - fromx) * j / (double)width;
			Zreal = 0;
			Zimag = 0;
			for (k = 0; k < iter; k++)
			{
				if ((Zreal * Zreal + Zimag * Zimag) > 4.0)
					break;
				CExpoInt(&Zreal, &Zimag, expo);
				Zreal += Creal;
				Zimag += Cimag;
			}
			value = k * 160 / iter;
			SetPixel(hdc, j, i, shlwapi_ColorHLSToRGB((WORD)value, 120, 240));
		}
	}
	return;
}

void PaintJulia(HWND hWnd, HDC hdc, double fromx, double fromy, double tox, double toy, int iter, int expo, double real, double imag)
{
	RECT rc;
	int width, height, i, j, k, value;
	double Creal, Cimag;
	double Zreal, Zimag;

	GetClientRect(hWnd, &rc);
	width = rc.right - rc.left;
	height = rc.bottom - rc.top;

	for (i = 0; i < height; i++)
	{
		Creal = 0;
		Cimag = fromy + (toy - fromy) * i / (double)height;
		for (j = 0; j < width; j++)
		{
			Creal = fromx + (tox - fromx) * j / (double)width;
			Zreal = Creal;
			Zimag = Cimag;
			for (k = 0; k < iter; k++)
			{
				if ((Zreal * Zreal + Zimag * Zimag) > 4.0)
					break;
				CExpoInt(&Zreal, &Zimag, expo);
				Zreal += real;
				Zimag += imag;
			}
			value = k * 160 / iter;
			SetPixel(hdc, j, i, shlwapi_ColorHLSToRGB((WORD)value, 120, 240));
		}
	}
	return;
}
</math.h></tchar.h></windows.h>
笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的