Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
mandelbrot.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphics
3/// \notebook -js
4/// \preview Using TExec to handle keyboard events and TComplex to draw the Mandelbrot set.
5///
6/// Pressing the keys 'z' and 'u' will zoom and unzoom the picture
7/// near the mouse location, 'r' will reset to the default view.
8///
9/// Try it (in compiled mode!) with: `root mandelbrot.C+`
10///
11/// \macro_image (tcanvas_js)
12///
13/// ### Details
14///
15/// when a mouse event occurs the myexec() function is called (by
16/// using AddExec). Depending on the pressed key, the mygenerate()
17/// function is called, with the proper arguments. Note the
18/// last_x and last_y variables that are used in myexec() to store
19/// the last pointer coordinates (px is not a pointer position in
20/// kKeyPress events).
21///
22/// \macro_code
23///
24/// \author Luigi Bardelli <bardelli@fi.infn.it>
25
26#include <TStyle.h>
27#include <TROOT.h>
28#include <TH2.h>
29#include <TComplex.h>
30#include <TVirtualPad.h>
31#include <TCanvas.h>
32
33TH2F *last_histo = nullptr;
34
35void mygenerate(double factor, double cen_x, double cen_y)
36{
37 printf("Regenerating...\n");
38 // resize histo:
39 if (factor > 0) {
40 double dx = last_histo->GetXaxis()->GetXmax() - last_histo->GetXaxis()->GetXmin();
41 double dy = last_histo->GetYaxis()->GetXmax() - last_histo->GetYaxis()->GetXmin();
42 last_histo->SetBins(last_histo->GetNbinsX(), cen_x - factor * dx / 2, cen_x + factor * dx / 2,
43 last_histo->GetNbinsY(), cen_y - factor * dy / 2, cen_y + factor * dy / 2);
44 last_histo->Reset();
45 } else {
46 if (last_histo)
47 delete last_histo;
48 // allocate first view...
49 last_histo = new TH2F("h2", "Mandelbrot [move mouse and press z to zoom, u to unzoom, r to reset]", 200, -2, 2,
50 200, -2, 2);
51 last_histo->SetStats(false);
52 }
53 const int max_iter = 50;
54 for (int bx = 1; bx <= last_histo->GetNbinsX(); bx++)
55 for (int by = 1; by <= last_histo->GetNbinsY(); by++) {
56 double x = last_histo->GetXaxis()->GetBinCenter(bx);
57 double y = last_histo->GetYaxis()->GetBinCenter(by);
58 TComplex point(x, y);
59 TComplex z = point;
60 int iter = 0;
61 while (z.Rho() < 2) {
62 z = z * z + point;
63 last_histo->Fill(x, y);
64 iter++;
65 if (iter > max_iter)
66 break;
67 }
68 }
69 last_histo->SetContour(99);
70 last_histo->Draw("colz");
71 gPad->Modified();
72 gPad->Update();
73 printf("Done.\n");
74}
75
76void myexec()
77{
78 // get event information
79 int event = gPad->GetEvent();
80 int px = gPad->GetEventX();
81 int py = gPad->GetEventY();
82
83 // some magic to get the coordinates...
84 double xd = gPad->AbsPixeltoX(px);
85 double yd = gPad->AbsPixeltoY(py);
86 float x = gPad->PadtoX(xd);
87 float y = gPad->PadtoY(yd);
88
89 static float last_x;
90 static float last_y;
91
92 if (event != kKeyPress) {
93 last_x = x;
94 last_y = y;
95 return;
96 }
97
98 const double Z = 2.;
99 switch (px) {
100 case 'z': // ZOOM
101 mygenerate(1. / Z, last_x, last_y);
102 break;
103 case 'u': // UNZOOM
104 mygenerate(Z, last_x, last_y);
105 break;
106 case 'r': // RESET
107 mygenerate(-1, last_x, last_y);
108 break;
109 };
110}
111
112void mandelbrot()
113{
114 // cosmetics...
117 new TCanvas("canvas", "View Mandelbrot set");
118 // this generates and draws the first view...
119 mygenerate(-1, 0, 0);
120
121 // add exec
122 gPad->AddExec("myexec", "myexec()");
123}
@ kKeyPress
Definition Buttons.h:20
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
R__EXTERN TStyle * gStyle
Definition TStyle.h:436
#define gPad
virtual Double_t GetBinCenter(Int_t bin) const
Return center of bin.
Definition TAxis.cxx:473
Double_t GetXmax() const
Definition TAxis.h:142
Double_t GetXmin() const
Definition TAxis.h:141
The Canvas class.
Definition TCanvas.h:23
Double_t Rho() const
Definition TComplex.h:48
virtual Int_t GetNbinsY() const
Definition TH1.h:299
TAxis * GetXaxis()
Definition TH1.h:325
virtual Int_t GetNbinsX() const
Definition TH1.h:298
TAxis * GetYaxis()
Definition TH1.h:326
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3068
virtual void SetContour(Int_t nlevels, const Double_t *levels=nullptr)
Set the number and values of contour levels.
Definition TH1.cxx:8516
virtual void SetBins(Int_t nx, Double_t xmin, Double_t xmax)
Redefine x axis parameters.
Definition TH1.cxx:8800
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:9023
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:308
void Reset(Option_t *option="") override
Reset this histogram: contents, errors, etc.
Definition TH2.cxx:3969
Int_t Fill(Double_t) override
Invalid Fill method.
Definition TH2.cxx:393
void SetPadGridX(Bool_t gridx)
Definition TStyle.h:362
void SetPadGridY(Bool_t gridy)
Definition TStyle.h:363
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17