⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sierpinski.cpp

📁 《分形算法与程序设计VC版》一书<5_01>:内含基于逃逸时间算法的Sierpinski三角形源代码。双击Debug下的SIERPINSKI.exe文件
💻 CPP
字号:
 /*------------------------------------------------------------
This program is design for fractal
  ------------------------------------------------------------*/
#include <stdafx.h>
#include <windows.h>

#define ID_BUTTONDRAW	1
#define ID_BUTTONSLOW	2

HINSTANCE hInst ;
DWORD SLEEPTIME ;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

//'serpirski曲线递归过程声明
void serpirski(HDC hdc, float a, float b, float c, float d, int n, int m, int r);


int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName [] = TEXT ("sierpinski") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;
	 hInst=hInstance;

     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     
     hwnd = CreateWindow (szAppName,                  // window class name
                          TEXT ("sierpinski"),		  // window caption
                          WS_OVERLAPPEDWINDOW,        // window style
                          CW_USEDEFAULT,              // initial x position
                          CW_USEDEFAULT,              // initial y position
                          CW_USEDEFAULT,              // initial x size
                          CW_USEDEFAULT,              // initial y size
                          NULL,                       // parent window handle
                          NULL,                       // window menu handle
                          hInstance,                  // program instance handle
                          NULL) ;                     // creation parameters
     
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
     
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}


//serpirski曲线迭代过程定义

void serpirski(HDC hdc, float a, float b, float c, float d, int n, int m, int r)
{

	static int  p , q ;
    float x , y , x0 , y0 ;

	for (p = 0 ; p < m - 1 ; p++)
	 {
		x0 = a + (c - a) * p / m ;
		for (q = 0 ; q < m - 1 ; )
		{

			y0 = b + (d - b) * q / m ;
			x = x0 ; y = y0 ;
			for (int k = 1; k < n;k++)
			{
				if (y > 0.5)
				{
					x = 2 * x ; 
					y = 2 * y - 1 ;
				}
				else if (x >= 0.5)
				{
					x = 2 * x - 1 ;
					y = 2 * y ;
				}
				else
				{
					x = 2 * x ;
					y = 2 * y ;
				}
				if (x * x + y * y > r)
					goto np ;
			}

            SetPixel ( hdc,2*p + 50, 2*q + 50 ,RGB(0 , 0, 250));//修改值2,可得到不同的放大效果
		
        np:
            q++;

		}	
		Sleep (SLEEPTIME) ;	

	}

}

//以下定义窗口过程调用

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	 HWND hwndDraw;
     static HDC         hdc ;
     static int cxChar,cyChar;
     PAINTSTRUCT ps ;
	 WORD id;



     switch (message)
     {
     case WM_CREATE:
		  cxChar=LOWORD(GetDialogBaseUnits());
		  cyChar=HIWORD(GetDialogBaseUnits());

          return 0 ;

     case WM_PAINT:
			hdc = BeginPaint(hwnd,&ps) ;
			serpirski(hdc, 0, 0, 1, 1, 12, 200, 200) ; //调用绘图函数;
			EndPaint (hwnd, &ps) ;
			return 0 ;
          
	 

     case WM_DESTROY:
			PostQuitMessage (0) ;
			return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -