?? sierpinski.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 + -