程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 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 }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved