fractal.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: fractal.cpp
  3. // Purpose: demo of wxConfig and related classes
  4. // Author: Andrew Davison
  5. // Modified by:
  6. // Created: 05.04.94
  7. // Copyright: (c) 1994 Andrew Davison
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. /*
  11. Date: Tue, 5 Apr 1994 12:01:18 +1000
  12. From: Andrew Davison <andrewd@au.com.sfe>
  13. To: wxwin-users@ed.aiai
  14. Subject: Fractal mountains
  15. Hi,
  16. This is a quick port of a fractal mountain generator originally
  17. done for MS-Windows. On a Sun the colours look a little washed
  18. out and there is not as much snow or high mountains (maybe the
  19. random number generators fault). The viewing plane is not
  20. quite right as the original code used SetViewportOrg() which there
  21. doesn't seem to be an equivalent of under wxWidgets, and my quick
  22. hack doesn't fix.
  23. */
  24. #include "wx/wxprec.h"
  25. #ifdef __BORLANDC__
  26. #pragma hdrstop
  27. #endif
  28. #ifndef WX_PRECOMP
  29. #include "wx/wx.h"
  30. #endif //precompiled headers
  31. #include "wx/math.h"
  32. #include "wx/stockitem.h"
  33. #include <stdlib.h>
  34. #include <time.h>
  35. #define Random(x) (rand() % x)
  36. #define Randomize() (srand((unsigned int)time(NULL)))
  37. static int detail = 9; // CHANGE THIS... 7,8,9 etc
  38. static bool running = false;
  39. static wxMenuBar *menuBar = NULL;
  40. // Define a new application type
  41. class MyApp: public wxApp
  42. {
  43. public:
  44. bool OnInit();
  45. };
  46. IMPLEMENT_APP(MyApp)
  47. // Define a new frame type
  48. class MyFrame: public wxFrame
  49. {
  50. public:
  51. MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size);
  52. void OnCloseWindow(wxCloseEvent& event);
  53. void OnExit(wxCommandEvent& event);
  54. DECLARE_EVENT_TABLE()
  55. };
  56. // Define a new canvas which can receive some events
  57. class MyCanvas: public wxWindow
  58. {
  59. public:
  60. MyCanvas(wxFrame *frame);
  61. void Draw(wxDC& dc);
  62. private:
  63. void OnPaint(wxPaintEvent& event);
  64. void Fractal(wxDC& dc, int X1, int Y1, int X2, int Y2, int Z1, int Z2, int Z3, int Z4, int Iteration, double Std, double Ratio);
  65. wxPen SnowPen, MtnPen, GreenPen;
  66. wxBrush WaterBrush;
  67. int Sealevel;
  68. DECLARE_EVENT_TABLE()
  69. };
  70. // `Main program' equivalent, creating windows and returning main app frame
  71. bool MyApp::OnInit()
  72. {
  73. // Create the main frame window
  74. MyFrame *frame = new MyFrame(NULL, wxT("Fractal Mountains for wxWidgets"), wxDefaultPosition, wxSize(640, 480));
  75. // Make a menubar
  76. wxMenu *file_menu = new wxMenu;
  77. file_menu->Append(wxID_EXIT, wxGetStockLabel(wxID_EXIT));
  78. menuBar = new wxMenuBar;
  79. menuBar->Append(file_menu, wxT("&File"));
  80. frame->SetMenuBar(menuBar);
  81. int width, height;
  82. frame->GetClientSize(&width, &height);
  83. (void) new MyCanvas(frame);
  84. // Show the frame
  85. frame->Show(true);
  86. return true;
  87. }
  88. BEGIN_EVENT_TABLE(MyFrame, wxFrame)
  89. EVT_CLOSE(MyFrame::OnCloseWindow)
  90. EVT_MENU(wxID_EXIT, MyFrame::OnExit)
  91. END_EVENT_TABLE()
  92. // My frame constructor
  93. MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):
  94. wxFrame(frame, wxID_ANY, title, pos, size, wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE )
  95. {
  96. }
  97. // Intercept menu commands
  98. void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
  99. {
  100. this->Destroy();
  101. }
  102. void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
  103. {
  104. static bool destroyed = false;
  105. if (destroyed)
  106. return;
  107. this->Destroy();
  108. destroyed = true;
  109. }
  110. BEGIN_EVENT_TABLE(MyCanvas, wxWindow)
  111. EVT_PAINT(MyCanvas::OnPaint)
  112. END_EVENT_TABLE()
  113. // Define a constructor for my canvas
  114. MyCanvas::MyCanvas(wxFrame *frame):
  115. wxWindow(frame, wxID_ANY)
  116. {
  117. wxColour wxCol1(255,255,255);
  118. SnowPen = wxPen(wxCol1, 2, wxSOLID);
  119. wxColour wxCol2(128,0,0);
  120. MtnPen = wxPen(wxCol2, 1, wxSOLID);
  121. wxColour wxCol3(0,128,0);
  122. GreenPen = wxPen(wxCol3, 1, wxSOLID);
  123. wxColour wxCol4(0,0,128);
  124. WaterBrush = wxBrush(wxCol4, wxSOLID);
  125. }
  126. void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
  127. {
  128. wxPaintDC dc(this);
  129. PrepareDC(dc);
  130. Draw(dc);
  131. }
  132. void MyCanvas::Draw(wxDC& dc)
  133. {
  134. if (running) return;
  135. running = true;
  136. menuBar->EnableTop(0, false);
  137. Randomize();
  138. dc.SetBackground(*wxLIGHT_GREY_BRUSH);
  139. dc.Clear();
  140. int Left, Top, Right, Bottom;
  141. GetClientSize(&Right, &Bottom);
  142. Right *= 3; Right /= 4;
  143. Bottom *= 3; Bottom /= 4;
  144. Left = 0;
  145. Top = Bottom/8;
  146. wxPoint Water[4];
  147. Water[0].x = Left; Water[0].y = Top;
  148. Water[1].x = Right; Water[1].y = Top;
  149. Water[2].x = Right+Bottom/2; Water[2].y = Bottom;
  150. Water[3].x = Bottom/2; Water[3].y = Bottom;
  151. dc.SetBrush(WaterBrush);
  152. dc.DrawPolygon(4, Water);
  153. double H = 0.75;
  154. double Scale = Bottom;
  155. double Ratio = 1.0 / pow(2.0, H);
  156. double Std = Scale * Ratio;
  157. Sealevel = Random(18) - 8;
  158. Fractal(dc, Left, Top, Right, Bottom, 0, 0, 0, 0, detail, Std, Ratio);
  159. menuBar->EnableTop(0, true);
  160. running = false;
  161. }
  162. void MyCanvas::Fractal(wxDC& dc, int X1, int Y1, int X2, int Y2, int Z1, int Z2, int Z3, int Z4, int Iteration, double Std, double Ratio)
  163. {
  164. int Xmid = (X1 + X2) / 2;
  165. int Ymid = (Y1 + Y2) / 2;
  166. int Z23 = (Z2 + Z3) / 2;
  167. int Z41 = (Z4 + Z1) / 2;
  168. int Newz = (int)((Z1 + Z2 + Z3 + Z4) / 4 + (double)(Random(17) - 8) / 8.0 * Std);
  169. if (--Iteration)
  170. {
  171. int Z12 = (Z1 + Z2) / 2;
  172. int Z34 = (Z3 + Z4) / 2;
  173. double Stdmid = Std * Ratio;
  174. Fractal(dc, Xmid, Y1, X2, Ymid, Z12, Z2, Z23, Newz, Iteration, Stdmid, Ratio);
  175. Fractal(dc, X1, Y1, Xmid, Ymid, Z1, Z12, Newz, Z41, Iteration, Stdmid, Ratio);
  176. Fractal(dc, Xmid, Ymid, X2, Y2, Newz, Z23, Z3, Z34, Iteration, Stdmid, Ratio);
  177. Fractal(dc, X1, Ymid, Xmid, Y2, Z41, Newz, Z34, Z4, Iteration, Stdmid, Ratio);
  178. }
  179. else
  180. {
  181. if (Newz <= Sealevel)
  182. {
  183. wxPoint P[4];
  184. P[0].x = Y1 / 2 + X1; P[0].y = Y1 + Z1;
  185. P[1].x = Y1 / 2 + X2; P[1].y = Y1 + Z2;
  186. P[2].x = Y2 / 2 + X2; P[2].y = Y2 + Z3;
  187. P[3].x = Y2 / 2 + X1; P[3].y = Y2 + Z4;
  188. dc.SetPen(* wxBLACK_PEN);
  189. dc.SetBrush(* wxBLACK_BRUSH);
  190. dc.DrawPolygon(4, P);
  191. if (Z1 >= -(60+Random(25)))
  192. dc.SetPen(GreenPen);
  193. else if (Z1 >= -(100+Random(25)))
  194. dc.SetPen(MtnPen);
  195. else
  196. dc.SetPen(SnowPen);
  197. dc.DrawLine(Ymid/2+X2, Ymid+Z23, Ymid/2+X1, Ymid+Z41);
  198. }
  199. }
  200. }