C语言画Mandelbrot和Julia集合(WinAPI版)
acmilan2017/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

<code class="language-c">#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></code>
来自:计算机科学 / 软件综合
0
已屏蔽 原因:{{ notice.reason }}已屏蔽
{{notice.noticeContent}}
~~空空如也

想参与大家的讨论?现在就 登录 或者 注册

所属专业
所属分类
上级专业
同级专业
acmilan
进士 学者 笔友
文章
461
回复
2934
学术分
4
2009/05/30注册,5年2个月前活动
暂无简介
主体类型:个人
所属领域:无
认证方式:邮箱
IP归属地:未同步
文件下载
加载中...
{{errorInfo}}
{{downloadWarning}}
你在 {{downloadTime}} 下载过当前文件。
文件名称:{{resource.defaultFile.name}}
下载次数:{{resource.hits}}
上传用户:{{uploader.username}}
所需积分:{{costScores}},{{holdScores}}下载当前附件免费{{description}}
积分不足,去充值
文件已丢失

当前账号的附件下载数量限制如下:
时段 个数
{{f.startingTime}}点 - {{f.endTime}}点 {{f.fileCount}}
视频暂不能访问,请登录试试
仅供内部学术交流或培训使用,请先保存到本地。本内容不代表科创观点,未经原作者同意,请勿转载。
音频暂不能访问,请登录试试
支持的图片格式:jpg, jpeg, png
插入公式
评论控制
加载中...
文号:{{pid}}
投诉或举报
加载中...
{{tip}}
请选择违规类型:
{{reason.type}}

空空如也

加载中...
详情
详情
推送到专栏从专栏移除
设为匿名取消匿名
查看作者
回复
只看作者
加入收藏取消收藏
收藏
取消收藏
折叠回复
置顶取消置顶
评学术分
鼓励
设为精选取消精选
管理提醒
编辑
通过审核
评论控制
退修或删除
历史版本
违规记录
投诉或举报
加入黑名单移除黑名单
查看IP
{{format('YYYY/MM/DD HH:mm:ss', toc)}}