现在的位置: 首页 > 综合 > 正文

曼德罗伯特集代码

2013年12月05日 ⁄ 综合 ⁄ 共 5245字 ⁄ 字号 评论关闭
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <windows.h>
/*
#define rmin   -0.67058
#define rmax   -0.67008
#define imin   -0.45820
#define imax   -0.45770
*/
#define rmin   -2.0
#define rmax   2.0
#define imin   -2.0
#define imax   2.0

#define MaxIter 100

#define height  500
#define width   500

#define NumColor 16
/**/
unsigned long ColorTable[NumColor] = {RGB(255,255,255), //WHITE
                         RGB(0,0,0),       //BLACK			
                         RGB(255,0,0),     //RED				
                         RGB(255,255,0),   //YELLOW			
                         RGB(0,255,0),     //GREEN			
                         RGB(0,255,255),   //CYAN			
                         RGB(0,0,255),     //BLUE			
                         RGB(255,0,255),   //MAGENTA			
                         RGB(255,128,255), //AQUAMARINE		
                         RGB(0,128,0),     //FORESTGREEN		
                         RGB(200,128,0),   //ORANGE			
                         RGB(200,0,255),   //MAROON			
                         RGB(128,128,64),  //BROWN			
                         RGB(255,128,128), //PINK			
                         RGB(255,255,128), //CORAL			
                         RGB(128,128,128)};//GRAY		

unsigned long *ColorTable;						 
HDC g_hDC = NULL;    //设备上下文句柄
HGDIOBJ g_hOldBitmap;  // 选择要载入的对象的句柄。该对象句柄必需已经利用以下函数所创建的
HWND hWnd;
MSG msg;

typedef struct complextype{
	float real, imag;
} Compl;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void MessageLoopThread();
int Make_color_array();
unsigned long getColor(double fraction, double intensity);

HANDLE hEvent;

void main()
{
	hEvent = CreateEvent(NULL, TRUE, FALSE, TEXT("booglesandboogles"));
	if (hEvent == NULL)
		printf("CreateEvent failed, error %d\n", GetLastError());fflush(stdout);//fflush()清除缓冲区清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件 
	HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MessageLoopThread, NULL, 0, NULL);
		
	WaitForSingleObject(hEvent, INFINITE);
	CloseHandle(hEvent);

	SendMessage(hWnd, WM_PAINT, NULL, NULL);

	Make_color_array();

	int i,j,k;
	Compl z, c;
	float	lengthsq, temp;
	float DCI = (float)((imax-imin)/height);
	float DCR = (float)((rmax-rmin)/width);
	for(i=0; i < width; i++){
		for(j=0; j < height; j++){
			z.real = z.imag = 0.0;
			c.real = (float)rmin + ((float)i*DCR);
			c.imag = (float)imin + ((float)j*DCR);
			k = 0;
			do{  
				temp = z.real*z.real - z.imag*z.imag + c.real;
				z.imag = (float)2.0*z.real*z.imag + c.imag;
				z.real = temp;
				lengthsq = z.real*z.real+z.imag*z.imag;
				k++;
			} while (lengthsq < 4.0 && k < MaxIter);
			SetPixelV(g_hDC, i, j, ColorTable[k % NumColor]);
		}
		InvalidateRect(hWnd, 0, TRUE);//将画好的图像重新画在一个矩形里
		Sleep(10);
	}
	getchar();
	
	if (IsWindow(hWnd))
		PostMessage(hWnd, WM_DESTROY, NULL, NULL);
	CloseHandle(hThread);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	RECT r, rOuter;

	switch (message){
		case WM_ERASEBKGND:
			break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			//NoStretch
			BitBlt(hdc, 0, 0, width, height, g_hDC, 0, 0, SRCCOPY);
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:
			SelectObject(g_hDC, g_hOldBitmap);
			PostQuitMessage(0);
			break;
		case WM_LBUTTONDOWN:
			GetWindowRect(hWnd, &rOuter);
			GetClientRect(hWnd, &r);
			SetWindowPos(hWnd, HWND_TOP, 0, 0, 
			             rOuter.right - rOuter.left - r.right + width,
						 rOuter.bottom - rOuter.top - r.bottom + height, 
						 SWP_NOMOVE);
			break;
		case WM_WINDOWPOSCHANGED:
			PostMessage(hWnd, WM_PAINT, NULL, NULL);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

void MessageLoopThread()
{
	WNDCLASSEX wcex;
	TCHAR *szWindowClass = TEXT("MPI_MANDEL_WINDOW");
	
	wcex.cbSize = sizeof(WNDCLASSEX); 
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= NULL;
	wcex.hIcon			= NULL;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_BACKGROUND);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(NULL, IDI_APPLICATION);
	
	if (!RegisterClassEx(&wcex)){
		int error = GetLastError();
		printf("RegisterClassEx failed: %d\n", error);
	}
	
	hWnd = CreateWindowEx(
			WS_EX_TOOLWINDOW | WS_EX_APPWINDOW, 
			szWindowClass, TEXT("Mandel"), 
			WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU,
			CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, NULL, NULL);
	if (hWnd == NULL){
		int error = GetLastError();
		printf("CreateWindow failed: %d\n", error);
	}

	g_hDC = CreateCompatibleDC(NULL);
	HBITMAP hBitmap = CreateBitmap(width, height, GetDeviceCaps(g_hDC, PLANES), GetDeviceCaps(g_hDC, BITSPIXEL), NULL);
	g_hOldBitmap = SelectObject(g_hDC, hBitmap);
	SetStretchBltMode(g_hDC, COLORONCOLOR);

	ShowWindow(hWnd, SW_SHOW);
	SetWindowPos(hWnd, HWND_TOP, 0, 0, width, height, SWP_SHOWWINDOW | SWP_NOMOVE);
	RECT rOuter, r;
	GetWindowRect(hWnd, &rOuter);
	GetClientRect(hWnd, &r);
	SetWindowPos(hWnd, HWND_TOP, 0, 0, 
				 rOuter.right - rOuter.left - r.right + width,
				 rOuter.bottom - rOuter.top - r.bottom + height, 
				 SWP_NOMOVE);

	SetEvent(hEvent);

	while (GetMessage(&msg, NULL, 0, 0)){//hWnd
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}

unsigned long getColor(double fraction, double intensity)
{
	double red, green, blue;

	double dtemp;
	fraction = fabs(modf(fraction, &dtemp));

	if (intensity > 2.0)
		intensity = 2.0;
	if (intensity < 0.0)
		intensity = 0.0;

	dtemp = 1.0/6.0;

	if (fraction < 1.0/6.0)
	{
		red = 1.0;
		green = fraction / dtemp;
		blue = 0.0;
	}
	else
	if (fraction < 1.0/3.0)
	{
		red = 1.0 - ((fraction - dtemp) / dtemp);
		green = 1.0;
		blue = 0.0;
	}
	else
	if (fraction < 0.5)
	{
		red = 0.0;
		green = 1.0;
		blue = (fraction - (dtemp*2.0)) / dtemp;
	}
	else
	if (fraction < 2.0/3.0)
	{
		red = 0.0;
		green = 1.0 - ((fraction - (dtemp*3.0)) / dtemp);
		blue = 1.0;
	}
	else
	if (fraction < 5.0/6.0)
	{
		red = (fraction - (dtemp*4.0)) / dtemp;
		green = 0.0;
		blue = 1.0;
	}
	else
	{
		red = 1.0;
		green = 0.0;
		blue = 1.0 - ((fraction - (dtemp*5.0)) / dtemp);
	}

	if (intensity > 1)
	{
		intensity = intensity - 1.0;
		red = red + ((1.0 - red) * intensity);
		green = green + ((1.0 - green) * intensity);
		blue = blue + ((1.0 - blue) * intensity);
	}
	else
	{
		red = red * intensity;
		green = green * intensity;
		blue = blue * intensity;
	}

	int r,g,b;
	
	r = (int)(red * 255.0);
	g = (int)(green * 255.0);
	b = (int)(blue * 255.0);

	return RGB(r,g,b);
}

int Make_color_array()
{
	double fraction, intensity;

	//ColorTable = new unsigned long[NumColor];
	intensity = 1.0;
	for (int i=0; i<NumColor; i++)
	{
		fraction = double(i) / double(NumColor);
		ColorTable[i] = getColor(fraction, intensity);
	}
	return 0;
}

抱歉!评论已关闭.