跳到正文

剪贴板

剪贴板 d1kcwdp9

原文
剪贴板信息

剪贴板信息

收录
更新

Julia sets.cpp

C++
/***********
	链接选项 <一定一定一定要加上>:
	-lgdi32 -fopenmp -mwindows
	各个链接选项的功能分别是:
	Win32API  多线程加速  不产生控制台窗口

	功能简介:
	主窗口中可以:
	1.按住右键,显示Mandelbrot set中该点对应的Julia sets
	所有窗口中都可以:
	2.按住左键,拖动显示范围
	3.滚动滚轮键对该点放大缩小

	警示恒言 (bug):
	1.内存泄漏
	解决时间:2week
	解决方法:DeleteObject(hDIB);//释放内存
	2.巨量卡顿
	解决时间:2week
	解决方法:
	删除#pragma GCC加速
	可能和多线程冲突了
 ***********/
#include <bits/stdc++.h>
#include <immintrin.h>
#include <emmintrin.h>
#include <windowsx.h>
#include <windows.h>
#include <gdiplus.h>
//#define double long double

/***********
	这个函数有一些bug,但是我找不到
	所以我干脆不用它了
	BUG:
	"paint_xy" 函数与绘制的图像错位了
 ***********/
void paint_xy(HDC hdc, double unit, double left, double top, int W, int H) { // 绘制x,y轴
	int t = 1;
	unit = 1 / unit;
	while (unit > 100)t *= 10, unit /= 10;
	HPEN hline;
	//竖
	for (int i = (ceil(left * t)  - left * t) * unit, j = ceil(left * t); i <= W; i += unit, ++j) {
		if (!(j % 100))
			hline = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
		else if (!(j % 10))
			hline = CreatePen(PS_SOLID, 1, RGB(140, 140, 140));
		else
			hline = CreatePen(PS_SOLID, 1, RGB(60, 60, 60));
		SelectObject(hdc, hline);
		MoveToEx(hdc, i, 0, NULL);
		LineTo(hdc, i, H);
	}
	//横
	for (int i = (0 - ceil(top * t) + top * t) * unit, j = ceil(top * t); i <= H; i += unit, --j) {
		if (!(j % 100))
			hline = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
		else if (!(j % 10))
			hline = CreatePen(PS_SOLID, 1, RGB(140, 140, 140));
		else
			hline = CreatePen(PS_SOLID, 1, RGB(60, 60, 60));
		SelectObject(hdc, hline);
		MoveToEx(hdc, 0, i, NULL);
		LineTo(hdc, W, i);
	}
	DeleteObject(hline);
}
HWND hwndNew, hwndFather; // 新窗口句柄
std::complex<double> const_z, const_c;
/*************************************************************************************************
	新窗口:
	绘置Julia sets
	互动:
 *************************************************************************************************/
LRESULT CALLBACK NewWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	static bool is_size = 1;
	static bool is_stop_move = 1;
	static double unit = 1.0 / 150;
	static double left = -2, top = 2;
	static int mouse_x_pos = 0;
	static int mouse_y_pos = 0;
	static int mouse_times = 0;
	switch (uMsg) {
		case WM_DESTROY: {//结束该窗口
			hwndNew = NULL;
			DestroyWindow(hwnd);
			return 0;
		}
		case WM_SIZE: {//改变大小
			is_size = 1;
			return 0;
		}
		case WM_ERASEBKGND: {//跳过背景擦除
			return 1;
		}
		case WM_MOUSEWHEEL: {//放大缩小
			//鼠标坐标
			POINT cursorPos;
			GetCursorPos(&cursorPos);
			ScreenToClient(hwnd, &cursorPos);
			int xPos = cursorPos.x;
			int yPos = cursorPos.y;

			double l = left + xPos * unit;
			double t = top - yPos * unit;
			if (GET_WHEEL_DELTA_WPARAM(wParam) > 0)
				unit *= 0.9;// 向上滚动,放大
			else
				unit /= 0.9;// 向下滚动,缩小
			left = l - xPos * unit;
			top = t + yPos * unit;

			//重绘
			PostMessage(hwndNew, WM_SIZE, 0, 0);
			PostMessage(hwndNew, WM_EXITSIZEMOVE, 0, 0);
			break;
		}
		case WM_EXITSIZEMOVE: {//停止改变大小or移动
			is_stop_move = 1;
			if (is_size)
				InvalidateRect(hwnd, NULL, TRUE);
			return 0;
		}
		case WM_LBUTTONDOWN: {//按下左键时记录
			// 记录鼠标按下时的位置
			mouse_x_pos = GET_X_LPARAM(lParam);
			mouse_y_pos = GET_Y_LPARAM(lParam);
			mouse_times = 1;
			return 0;
		}
		case WM_MOUSEMOVE: {//鼠标坐标
			// 获取设备上下文
			HDC hdc = GetDC(hwnd);

			// 获取相对坐标
			int xPos = GET_X_LPARAM(lParam);
			int yPos = GET_Y_LPARAM(lParam);

			// 获取窗口客户区域的大小
			RECT rect;
			GetClientRect(hwnd, &rect);
			int W = rect.right - rect.left;
			int H = rect.bottom - rect.top;
			W >>= 2;//单位高度
			H >>= 2;//单位长度

			// 输出鼠标坐标
			std::string text = "z=( " + std::to_string(xPos * 1.0 / W - 2) + " , "
			                   + std::to_string( 2 - yPos * 1.0 / H) + "i )    ";
			SetBkColor(hdc, RGB(4, 1, 16)); // 设置背景色为蓝黑色
			SetTextColor(hdc, RGB(255, 255, 255)); // 设置文本颜色为白色
			TextOutA(hdc, 0, 16, text.c_str(), text.length());

			if (mouse_times) {
				left += (mouse_x_pos - xPos) * unit;
				top -= (mouse_y_pos - yPos) * unit;
				mouse_x_pos = xPos;
				mouse_y_pos = yPos;

				//重绘
				PostMessage(hwndNew, WM_SIZE, 0, 0);
				PostMessage(hwndNew, WM_EXITSIZEMOVE, 0, 0);
				++mouse_times;
			}

			// 释放设备上下文
			ReleaseDC(hwnd, hdc);
			return 0;
		}
		case WM_LBUTTONUP: {//抬起左键
			int xPos = GET_X_LPARAM(lParam);
			int yPos = GET_Y_LPARAM(lParam);
			left += (mouse_x_pos - xPos) * unit;
			top -= (mouse_y_pos - yPos) * unit;
			PostMessage(hwndNew, WM_SIZE, 0, 0);
			PostMessage(hwndNew, WM_EXITSIZEMOVE, 0, 0);
			mouse_times = 0;
		}
		case WM_PAINT: {//绘置Julia sets
			if (is_size && is_stop_move)
				is_size = 0, is_stop_move = 0;
			else
				return 0;

			// 获取窗口大小
			RECT rect;
			GetClientRect(hwnd, &rect);
			int W, H;
			W = (rect.right - rect.left) >> 2 << 2;
			H = rect.bottom - rect.top;

			// 获取设备上下文
			PAINTSTRUCT ps;
			HDC hdc = BeginPaint(hwnd, &ps);

			{
				// 创建内存缓冲区
				BITMAPINFO bmpInfo;
				bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
				bmpInfo.bmiHeader.biWidth = W;
				bmpInfo.bmiHeader.biHeight = -H; // 负值表示顶部到底部扫描
				bmpInfo.bmiHeader.biPlanes = 1;
				bmpInfo.bmiHeader.biBitCount = 24; // 24位颜色
				bmpInfo.bmiHeader.biCompression = BI_RGB;
				bmpInfo.bmiHeader.biSizeImage = 0;
				bmpInfo.bmiHeader.biXPelsPerMeter = 0;
				bmpInfo.bmiHeader.biYPelsPerMeter = 0;
				bmpInfo.bmiHeader.biClrUsed = 0;
				bmpInfo.bmiHeader.biClrImportant = 0;

				// 分配内存缓冲区
				void* pBuffer;
				HDC hdcMem = CreateCompatibleDC(hdc);
				HBITMAP hDIB = CreateDIBSection(hdcMem, &bmpInfo, DIB_RGB_COLORS, &pBuffer, nullptr, 0);
				memset(pBuffer, 0xff, W * H * 3);

				// 保存数据到位图
				#pragma omp parallel for schedule(guided)
				for (int Y = 0; Y != H; ++Y) {
					BYTE* p = (BYTE*)pBuffer + Y * W * 3;
					for (int X = 0; X != W; ++X) {
						std::complex<double> z(left + X * unit, top - Y * unit);
						for (int i = 1; i != 255; ++i) {
							z = z * z + const_c;
							if (std::norm(z) >= 4) {
								// 计算像素点颜色并保存到缓冲区
								*(p++) = std::min(i << 4, 255); // 蓝色分量
								*(p++) = std::min(i << 0, 255); // 绿色分量
								*(p++) = std::min(i << 2, 255); // 红色分量
								goto NEXT;
							}
						}
						// 设置像素点为黑色
						*(p++) = 0; // 蓝色分量
						*(p++) = 0; // 绿色分量
						*(p++) = 0; // 红色分量
NEXT:
						;
					}
				}

// 将位图选入设备上下文
				HBITMAP hOldBitmap = (HBITMAP)SelectObject(hdcMem, hDIB);

// 将缓冲区的数据绘制到屏幕上
				BitBlt(hdc, 0, 0, W, H, hdcMem, 0, 0, SRCCOPY);

// 清理资源
				SelectObject(hdcMem, hOldBitmap);
				DeleteObject(hDIB);
				DeleteDC(hdcMem);
			}

			//xy轴
//			paint_xy(hdc, unit, left, top, W, H);

			// 将c绘制在新窗口内
			std::string text = "c=( " + std::to_string(const_c.real()) + " , "
			                   + std::to_string(const_c.imag()) + "i )   ";
			SetBkColor(hdc, RGB(4, 1, 16)); // 设置背景色为蓝黑色
			SetTextColor(hdc, RGB(255, 255, 255)); // 设置文本颜色为白色
			TextOutA(hdc, 0, 0, text.c_str(), text.length());

			EndPaint(hwnd, &ps);
			return 0;
		}
	}
	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
/*************************************************************************************************
	主窗口:
	绘置Mandelbrot set
	互动:
	左键实现创建Julia sets
 *************************************************************************************************/
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	static bool is_size = 1;
	static bool is_stop_move = 1;
	static double unit = 1.0 / 200;
	static double left = -2, top = 1.5;
	static int mouse_x_pos = 0;
	static int mouse_y_pos = 0;
	static int mouse_times = 0;
	switch (uMsg) {
		case WM_DESTROY: {//结束
			PostQuitMessage(0);
			return 0;
		}
		case WM_SIZE: {//改变大小
			is_size = 1;
			return 0;
		}
		case WM_ERASEBKGND: {//跳过背景擦除
			return 1;
		}
		case WM_MOUSEWHEEL: {//放大缩小
			//鼠标坐标
			POINT cursorPos;
			GetCursorPos(&cursorPos);
			ScreenToClient(hwnd, &cursorPos);
			int xPos = cursorPos.x;
			int yPos = cursorPos.y;

			double l = left + xPos * unit;
			double t = top - yPos * unit;
			if (GET_WHEEL_DELTA_WPARAM(wParam) > 0)
				unit *= 0.9;// 向上滚动,放大
			else
				unit /= 0.9;// 向下滚动,缩小
			left = l - xPos * unit;
			top = t + yPos * unit;

			//重绘
			PostMessage(hwndFather, WM_SIZE, 0, 0);
			PostMessage(hwndFather, WM_EXITSIZEMOVE, 0, 0);
			break;
		}
		case WM_EXITSIZEMOVE: {//停止改变大小or移动
			is_stop_move = 1;
			if (is_size) {
				InvalidateRect(hwnd, NULL, TRUE);
			}
			return 0;
		}
		case WM_RBUTTONDOWN: {//右键刷新Julia sets
			/*更改const_c的值*/{
				// 获取相对坐标
				int xPos = GET_X_LPARAM(lParam);
				int yPos = GET_Y_LPARAM(lParam);

				// 更改c的值
				std::complex<double> c(left + xPos * unit, top - yPos * unit);
				const_c = c;
			}

			if (hwndNew != NULL) {//重绘信息
				SendMessage(hwndNew, WM_SIZE, 0, 0);
				SendMessage(hwndNew, WM_EXITSIZEMOVE, 0, 0);
				return 0;
			}
			//				SendMessage(hwndNew, WM_DESTROY, 0, 0);

			/*创建窗口*/{
				// 获取系统边框大小
				RECT windowRect;
				GetWindowRect(hwnd, &windowRect);

				// 获取当前实例句柄
				HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE);

				// 创建新窗口类
				WNDCLASS wc = {};
				wc.lpfnWndProc = NewWindowProc;
				wc.hInstance = hInstance;
				wc.lpszClassName = "NewWindowClass";
				RegisterClass(&wc);

				// 创建新窗口
				hwndNew = CreateWindowEx(
				              0,
				              "NewWindowClass",
				              "Julia sets",
				              WS_OVERLAPPEDWINDOW,
				              windowRect.right,
				              windowRect.top,
				              windowRect.right - windowRect.left,
				              windowRect.bottom - windowRect.top,
				              hwnd,
				              NULL,
				              hInstance,
				              NULL
				          );

			}

			// 显示新窗口
			ShowWindow(hwndNew, SW_SHOW);
			return 0;
		}
		case WM_LBUTTONDOWN: {//按下左键时记录
			// 记录鼠标按下时的位置
			mouse_x_pos = GET_X_LPARAM(lParam);
			mouse_y_pos = GET_Y_LPARAM(lParam);
			mouse_times = 1;
			return 0;
		}
		case WM_MOUSEMOVE: {//鼠标坐标
			// 获取设备上下文
			HDC hdc = GetDC(hwnd);

			// 获取相对坐标
			int xPos = GET_X_LPARAM(lParam);
			int yPos = GET_Y_LPARAM(lParam);

			// 输出鼠标坐标
			std::string text = "c=( " + std::to_string(left + xPos * unit) + " , "
			                   + std::to_string(top - yPos * unit) + "i )    ";
			SetBkColor(hdc, RGB(4, 1, 16)); // 设置背景色为蓝黑色
			SetTextColor(hdc, RGB(255, 255, 255)); // 设置文本颜色为白色
			TextOutA(hdc, 0, 0, text.c_str(), text.length());

			if (mouse_times) {
				left += (mouse_x_pos - xPos) * unit;
				top -= (mouse_y_pos - yPos) * unit;
				mouse_x_pos = xPos;
				mouse_y_pos = yPos;

				//重绘
				PostMessage(hwndFather, WM_SIZE, 0, 0);
				PostMessage(hwndFather, WM_EXITSIZEMOVE, 0, 0);
				++mouse_times;
			}

			if (GetAsyncKeyState(VK_RBUTTON) & 0x8000) {
				// 更改c的值
				const_c = {left + xPos * unit, top - yPos * unit};

				if (hwndNew != NULL) {//重绘信息
					SendMessage(hwndNew, WM_SIZE, 0, 0);
					SendMessage(hwndNew, WM_EXITSIZEMOVE, 0, 0);
					return 0;
				}
			}

			// 释放设备上下文
			ReleaseDC(hwnd, hdc);
			return 0;
		}
		case WM_LBUTTONUP: {//抬起左键
			int xPos = GET_X_LPARAM(lParam);
			int yPos = GET_Y_LPARAM(lParam);
			left += (mouse_x_pos - xPos) * unit;
			top -= (mouse_y_pos - yPos) * unit;
			PostMessage(hwndFather, WM_SIZE, 0, 0);
			PostMessage(hwndFather, WM_EXITSIZEMOVE, 0, 0);
			mouse_times = 0;
		}
		case WM_PAINT: {//绘置Mandelbrot set
			if (is_size && is_stop_move)
				is_size = 0, is_stop_move = 0;
			else
				return 0;

			// 获取窗口大小
			RECT rect;
			GetClientRect(hwnd, &rect);
			int W, H;
			W = (rect.right - rect.left) >> 2 << 2;
			H = rect.bottom - rect.top;

			// 获取设备上下文
			PAINTSTRUCT ps;
			HDC hdc = BeginPaint(hwnd, &ps);

			/*Mandelbrot set*/{
				// 创建内存缓冲区
				BITMAPINFO bmpInfo;
				bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
				bmpInfo.bmiHeader.biWidth = W;
				bmpInfo.bmiHeader.biHeight = -H; // 负值表示顶部到底部扫描
				bmpInfo.bmiHeader.biPlanes = 1;
				bmpInfo.bmiHeader.biBitCount = 24; // 24位颜色
				bmpInfo.bmiHeader.biCompression = BI_RGB;
				bmpInfo.bmiHeader.biSizeImage = 0;
				bmpInfo.bmiHeader.biXPelsPerMeter = 0;
				bmpInfo.bmiHeader.biYPelsPerMeter = 0;
				bmpInfo.bmiHeader.biClrUsed = 0;
				bmpInfo.bmiHeader.biClrImportant = 0;

				// 分配内存缓冲区
				void* pBuffer;
				HDC hdcMem = CreateCompatibleDC(hdc);
				HBITMAP hDIB = CreateDIBSection(hdcMem, &bmpInfo, DIB_RGB_COLORS, &pBuffer, nullptr, 0);
				memset(pBuffer, 0xff, W * H * 3);

				// 保存数据到位图
				#pragma omp parallel for schedule(guided)
				for (int Y = 0; Y != H; ++Y) {
					BYTE* p = (BYTE*)pBuffer + Y * W * 3;
					for (int X = 0; X != W; ++X) {
						std::complex<double> z;
						std::complex<double> c(left + X * unit, top - Y * unit);
						for (int i = 1; i != 255; ++i) {
							z = z * z + c;
							if (std::norm(z) >= 4) {
								// 计算像素点颜色并保存到缓冲区
								*(p++) = std::min(i << 4, 255); // 蓝色分量
								*(p++) = std::min(i << 0, 255); // 绿色分量
								*(p++) = std::min(i << 2, 255); // 红色分量
								goto NEXT;
							}
						}
						// 设置像素点为黑色
						*(p++) = 0; // 蓝色分量
						*(p++) = 0; // 绿色分量
						*(p++) = 0; // 红色分量
NEXT:
						;
					}
				}

// 将位图选入设备上下文
				HBITMAP hOldBitmap = (HBITMAP)SelectObject(hdcMem, hDIB);

// 将缓冲区的数据绘制到屏幕上
				BitBlt(hdc, 0, 0, W, H, hdcMem, 0, 0, SRCCOPY);

// 清理资源
				SelectObject(hdcMem, hOldBitmap);
				DeleteObject(hDIB);
				DeleteDC(hdcMem);

			}

			//xy轴
			//			paint_xy(hdc, unit, left, top, W, H);

			EndPaint(hwnd, &ps);
			return 0;
		}
	}
	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	// 注册窗口类
	WNDCLASS wc = {};
	wc.lpfnWndProc = WindowProc;
	wc.hInstance = hInstance;
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wc.lpszClassName = "MyWindowClass";
	RegisterClass(&wc);

	// 创建窗口
	hwndFather = CreateWindowEx(
	                 0,
	                 "MyWindowClass",
	                 "Mandelbrot set",
	                 WS_OVERLAPPEDWINDOW,
	                 CW_USEDEFAULT,
	                 CW_USEDEFAULT,
	                 620,
	                 640,
	                 NULL,
	                 NULL,
	                 hInstance,
	                 NULL);
	if (hwndFather == NULL) {
		return 1;
	}

	// 显示窗口
	ShowWindow(hwndFather, nCmdShow);
	UpdateWindow(hwndFather);

	// 处理消息
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	// 销毁窗口
	DestroyWindow(hwndFather);
	UnregisterClass("MyWindowClass", hInstance);

	return 0;
}