当前位置:编程学习 > C/C++ >>

c++简单的分形

\

 

 

 

 

 

 

1 //Copyright (c) LeafCore
  2 #include <windows.h>
  3 #include <math.h>
  4 
  5 LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
  6 void draw(HDC, double, double, double, double);
  7
  8 char szClassName[ ] = "LeafCore";
  9
 10 int WINAPI WinMain(HINSTANCE hThisInstance,
 11                    HINSTANCE hPrevInstance,
 12                    LPSTR lpszArgument,
 13                    int nFunsterStil)
 14
 15 {
 16     HWND hwnd;
 17     MSG messages;
 18     WNDCLASSEX wincl;
 19
 20     wincl.hInstance = hThisInstance;
 21     wincl.lpszClassName = szClassName;
 22     wincl.lpfnWndProc = WindowProcedure;
 23     wincl.style = CS_DBLCLKS;
 24     wincl.cbSize = sizeof (WNDCLASSEX);
 25
 26     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
 27     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
 28     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
 29     wincl.lpszMenuName = NULL;
 30     wincl.cbClsExtra = 0;
 31     wincl.cbWndExtra = 0;
 32     wincl.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
 33
 34     if (!RegisterClassEx(&wincl))
 35         return 0;
 36
 37     hwnd=CreateWindowEx(
 38               0,
 39               szClassName,
 40               "LeafCore",
 41               WS_OVERLAPPEDWINDOW,
 42               CW_USEDEFAULT,
 43               CW_USEDEFAULT,
 44               1024,
 45               768,
 46               HWND_DESKTOP,
 47               NULL,
 48               hThisInstance,
 49               NULL
 50          );
 51
 52     ShowWindow(hwnd, nFunsterStil);
 53
 54     while(GetMessage(&messages, NULL, 0, 0)) {
 55         TranslateMessage(&messages);
 56         DispatchMessage(&messages);
 57     }
 58
 59     return messages.wParam;
 60 }
 61
 62 LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 63 {
 64     PAINTSTRUCT ps;
 65     HDC hdc;
 66     switch (message) {
 67     case WM_PAINT:
 68         hdc=BeginPaint(hwnd, &ps);
 69         HPEN green_pen=CreatePen(PS_SOLID, 1, RGB(0, 127, 0));
 70         HPEN old_pen=(HPEN) SelectObject(hdc, green_pen);
 71
 72         draw(hdc, 200, 400, 600, 400);
 73
 74         SelectObject(hdc, old_pen);
 75         DeleteObject(green_pen);
 76         EndPaint(hwnd, &ps);
 77         break;
 78     case WM_DESTROY:
 79         PostQuitMessage (0);
 80         break;
 81     default:
 82         return DefWindowProc (hwnd, message, wParam, lParam);
 83     }
 84
 85     return 0;
 86 }
 87
 88 void draw(HDC hdc, double start_x, double start_y, double end_x, double end_y)
 89 {
 90     if (fabs(start_x-end_x)<2 && fabs(start_y-end_y)<2) {
 91         //递归出口
 92         return;
 93     }
 94
 95     //左三分点
 96     int left_point_x=(int)((2*start_x+end_x)/3);
 97     int left_point_y=(int)((2*start_y+end_y)/3);
 98
 99     //中间点
100     int middle_point_x=(int)((start_x+end_x)/2-0.2887*(start_y-end_y));
101     int middle_point_y=(int)((start_y+end_y)/2+0.2887*(start_x-end_x));
102
103     //右三分点
104     int right_point_x=(int)((start_x+2*end_x)/3);
105     int right_point_y=(int)((start_y+2*end_y)/3);
106
107     //画出起点至左三分点
108     MoveToEx(hdc, (int)start_x, (int)start_y, 0);
109     LineTo(hdc, left_point_x, left_point_y);
110
111     //画出左三分点至中间点
112     draw(hdc, left_point_x, left_point_y, middle_point_x, middle_point_y);
113
114     //画出中间点至右三分点
115     draw(hdc, middle_point_x, middle_point_y, right_point_x, right_point_y);
116
117     //画出右三分点至终点
118     MoveToEx(hdc, right_point_x, right_point_y, 0);
119     LineTo(hdc, (int)end_x, (int)end_y);
120
121     //休眠30毫秒
122     Sleep(30);
123 }

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,