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