File: fractal.cpp

package info (click to toggle)
wxwidgets3.0 3.0.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 120,464 kB
  • sloc: cpp: 896,633; makefile: 52,303; ansic: 21,971; sh: 5,713; python: 2,940; xml: 1,534; perl: 264; javascript: 33
file content (257 lines) | stat: -rw-r--r-- 6,532 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
///////////////////////////////////////////////////////////////////////////////
// Name:        fractal.cpp
// Purpose:     demo of wxConfig and related classes
// Author:      Andrew Davison
// Modified by:
// Created:     05.04.94
// Copyright:   (c) 1994 Andrew Davison
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////


/*
Date: Tue, 5 Apr 1994 12:01:18 +1000
From: Andrew Davison <andrewd@au.com.sfe>
To: wxwin-users@ed.aiai
Subject: Fractal mountains

Hi,

This is a quick port of a fractal mountain generator originally
done for MS-Windows. On a Sun the colours look a little washed
out and there is not as much snow or high mountains (maybe the
random number generators fault). The viewing plane is not
quite right as the original code used SetViewportOrg() which there
doesn't seem to be an equivalent of under wxWidgets, and my quick
hack doesn't fix.
*/

#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef  WX_PRECOMP
  #include "wx/wx.h"
#endif //precompiled headers

#include "wx/math.h"
#include "wx/stockitem.h"

#include <stdlib.h>
#include <time.h>

#define Random(x) (rand() % x)
#define Randomize() (srand((unsigned int)time(NULL)))

static int detail = 9; // CHANGE THIS... 7,8,9 etc

static bool running = false;
static wxMenuBar *menuBar = NULL;

// Define a new application type
class MyApp: public wxApp
{
public:
    bool OnInit();
};

IMPLEMENT_APP(MyApp)

// Define a new frame type
class MyFrame: public wxFrame
{
public:
    MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size);

    void OnCloseWindow(wxCloseEvent& event);
    void OnExit(wxCommandEvent& event);

    DECLARE_EVENT_TABLE()
};

// Define a new canvas which can receive some events
class MyCanvas: public wxWindow
{
public:
    MyCanvas(wxFrame *frame);
    void Draw(wxDC& dc);

private:
    void OnPaint(wxPaintEvent& event);
    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);
    wxPen SnowPen, MtnPen, GreenPen;
    wxBrush WaterBrush;
    int Sealevel;

DECLARE_EVENT_TABLE()
};

// `Main program' equivalent, creating windows and returning main app frame
bool MyApp::OnInit()
{
  // Create the main frame window
  MyFrame *frame = new MyFrame(NULL, wxT("Fractal Mountains for wxWidgets"), wxDefaultPosition, wxSize(640, 480));

  // Make a menubar
  wxMenu *file_menu = new wxMenu;
  file_menu->Append(wxID_EXIT, wxGetStockLabel(wxID_EXIT));
  menuBar = new wxMenuBar;
  menuBar->Append(file_menu, wxT("&File"));
  frame->SetMenuBar(menuBar);

  int width, height;
  frame->GetClientSize(&width, &height);

  (void) new MyCanvas(frame);

  // Show the frame
  frame->Show(true);

  return true;
}

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
  EVT_CLOSE(MyFrame::OnCloseWindow)
  EVT_MENU(wxID_EXIT, MyFrame::OnExit)
END_EVENT_TABLE()

// My frame constructor
MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):
  wxFrame(frame, wxID_ANY, title, pos, size, wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE )
{
}

// Intercept menu commands
void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
{
    this->Destroy();
}

void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
{
    static bool destroyed = false;
    if (destroyed)
        return;

    this->Destroy();

    destroyed = true;
}

BEGIN_EVENT_TABLE(MyCanvas, wxWindow)
  EVT_PAINT(MyCanvas::OnPaint)
END_EVENT_TABLE()

// Define a constructor for my canvas
MyCanvas::MyCanvas(wxFrame *frame):
 wxWindow(frame, wxID_ANY)
{
    wxColour wxCol1(255,255,255);
    SnowPen = wxPen(wxCol1, 2, wxSOLID);

    wxColour wxCol2(128,0,0);
    MtnPen = wxPen(wxCol2, 1, wxSOLID);

    wxColour wxCol3(0,128,0);
    GreenPen = wxPen(wxCol3, 1, wxSOLID);

    wxColour wxCol4(0,0,128);
    WaterBrush = wxBrush(wxCol4, wxSOLID);
}

void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
{
    wxPaintDC dc(this);
    PrepareDC(dc);
    Draw(dc);
}

void MyCanvas::Draw(wxDC& dc)
{
    if (running) return;

    running = true;
    menuBar->EnableTop(0, false);

    Randomize();

    dc.SetBackground(*wxLIGHT_GREY_BRUSH);
    dc.Clear();

    int Left, Top, Right, Bottom;
    GetClientSize(&Right, &Bottom);

    Right *= 3; Right /= 4;
    Bottom *= 3; Bottom /= 4;
    Left = 0;
    Top = Bottom/8;

    wxPoint Water[4];
    Water[0].x = Left;            Water[0].y = Top;
    Water[1].x = Right;           Water[1].y = Top;
    Water[2].x = Right+Bottom/2;  Water[2].y = Bottom;
    Water[3].x = Bottom/2;        Water[3].y = Bottom;

    dc.SetBrush(WaterBrush);
    dc.DrawPolygon(4, Water);

    double H = 0.75;
    double Scale = Bottom;
    double Ratio = 1.0 / pow(2.0, H);
    double Std = Scale * Ratio;
    Sealevel = Random(18) - 8;

    Fractal(dc, Left, Top, Right, Bottom, 0, 0, 0, 0, detail, Std, Ratio);

    menuBar->EnableTop(0, true);
    running = false;
}

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)
{
    int Xmid = (X1 + X2) / 2;
    int Ymid = (Y1 + Y2) / 2;
    int Z23 = (Z2 + Z3) / 2;
    int Z41 = (Z4 + Z1) / 2;
    int Newz = (int)((Z1 + Z2 + Z3 + Z4) / 4 + (double)(Random(17) - 8) / 8.0 * Std);

    if (--Iteration)
    {
        int Z12 = (Z1 + Z2) / 2;
        int Z34 = (Z3 + Z4) / 2;
        double Stdmid = Std * Ratio;

        Fractal(dc, Xmid, Y1, X2, Ymid, Z12, Z2, Z23, Newz, Iteration, Stdmid, Ratio);
        Fractal(dc, X1, Y1, Xmid, Ymid, Z1, Z12, Newz, Z41, Iteration, Stdmid, Ratio);
        Fractal(dc, Xmid, Ymid, X2, Y2, Newz, Z23, Z3, Z34, Iteration, Stdmid, Ratio);
        Fractal(dc, X1, Ymid, Xmid, Y2, Z41, Newz, Z34, Z4, Iteration, Stdmid, Ratio);
    }
    else
    {
        if (Newz <= Sealevel)
        {
            wxPoint P[4];
            P[0].x = Y1 / 2 + X1;    P[0].y = Y1 + Z1;
            P[1].x = Y1 / 2 + X2;    P[1].y = Y1 + Z2;
            P[2].x = Y2 / 2 + X2;    P[2].y = Y2 + Z3;
            P[3].x = Y2 / 2 + X1;    P[3].y = Y2 + Z4;

            dc.SetPen(* wxBLACK_PEN);
            dc.SetBrush(* wxBLACK_BRUSH);

            dc.DrawPolygon(4, P);

            if (Z1 >= -(60+Random(25)))
                dc.SetPen(GreenPen);
            else if (Z1 >= -(100+Random(25)))
                dc.SetPen(MtnPen);
            else
                dc.SetPen(SnowPen);

            dc.DrawLine(Ymid/2+X2, Ymid+Z23, Ymid/2+X1, Ymid+Z41);
        }
    }
}