Logo ROOT  
Reference Guide
mandelbrot.C File Reference

Detailed Description

View in nbviewer Open in SWAN Using TExec to handle keyboard events and TComplex to draw the Mandelbrot set.

Pressing the keys 'z' and 'u' will zoom and unzoom the picture near the mouse location, 'r' will reset to the default view.

Try it (in compiled mode!) with: root mandelbrot.C+

Details

when a mouse event occurs the myexec() function is called (by using AddExec). Depending on the pressed key, the mygenerate() function is called, with the proper arguments. Note the last_x and last_y variables that are used in myexec() to store the last pointer coordinates (px is not a pointer position in kKeyPress events).

#include <TStyle.h>
#include <TROOT.h>
#include <TH2.h>
#include <TComplex.h>
#include <TVirtualPad.h>
#include <TCanvas.h>
TH2F *last_histo=NULL;
void mygenerate(double factor, double cen_x, double cen_y)
{
printf("Regenerating...\n");
// resize histo:
if(factor>0)
{
double dx=last_histo->GetXaxis()->GetXmax()-last_histo->GetXaxis()->GetXmin();
double dy=last_histo->GetYaxis()->GetXmax()-last_histo->GetYaxis()->GetXmin();
last_histo->SetBins(
last_histo->GetNbinsX(),
cen_x-factor*dx/2,
cen_x+factor*dx/2,
last_histo->GetNbinsY(),
cen_y-factor*dy/2,
cen_y+factor*dy/2
);
last_histo->Reset();
}
else
{
if(last_histo!=NULL) delete last_histo;
// allocate first view...
last_histo= new TH2F("h2",
"Mandelbrot [move mouse and press z to zoom, u to unzoom, r to reset]",
200,-2,2,200,-2,2);
last_histo->SetStats(0);
}
const int max_iter=50;
for(int bx=1;bx<=last_histo->GetNbinsX();bx++)
for(int by=1;by<=last_histo->GetNbinsY();by++)
{
double x=last_histo->GetXaxis()->GetBinCenter(bx);
double y=last_histo->GetYaxis()->GetBinCenter(by);
TComplex point( x,y);
TComplex z=point;
int iter=0;
while (z.Rho()<2){
z=z*z+point;
last_histo->Fill(x,y);
iter++;
if(iter>max_iter) break;
}
}
last_histo->SetContour(99);
last_histo->Draw("colz");
gPad->Modified();
gPad->Update();
printf("Done.\n");
}
void myexec()
{
// get event information
int event = gPad->GetEvent();
int px = gPad->GetEventX();
int py = gPad->GetEventY();
// some magic to get the coordinates...
double xd = gPad->AbsPixeltoX(px);
double yd = gPad->AbsPixeltoY(py);
float x = gPad->PadtoX(xd);
float y = gPad->PadtoY(yd);
static float last_x;
static float last_y;
if(event!=kKeyPress)
{
last_x=x;
last_y=y;
return;
}
const double Z=2.;
switch(px){
case 'z': // ZOOM
mygenerate(1./Z, last_x, last_y);
break;
case 'u': // UNZOOM
mygenerate(Z , last_x, last_y);
break;
case 'r': // RESET
mygenerate(-1 , last_x, last_y);
break;
};
}
void mandelbrot()
{
// cosmetics...
new TCanvas("canvas","View Mandelbrot set");
// this generates and draws the first view...
mygenerate(-1,0,0);
// add exec
gPad->AddExec("myexec","myexec()");
}
Author
Luigi Bardelli barde.nosp@m.lli@.nosp@m.fi.in.nosp@m.fn.i.nosp@m.t

Definition in file mandelbrot.C.

kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TComplex::Rho
Double_t Rho() const
Definition: TComplex.h:52
TH1::SetContour
virtual void SetContour(Int_t nlevels, const Double_t *levels=0)
Set the number and values of contour levels.
Definition: TH1.cxx:7946
TComplex
Definition: TComplex.h:27
TH2F
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:251
TH1::SetBins
virtual void SetBins(Int_t nx, Double_t xmin, Double_t xmax)
Redefine x axis parameters.
Definition: TH1.cxx:8222
TStyle.h
TAxis::GetBinCenter
virtual Double_t GetBinCenter(Int_t bin) const
Return center of bin.
Definition: TAxis.cxx:478
x
Double_t x[n]
Definition: legend1.C:17
TCanvas.h
TStyle::SetPadGridY
void SetPadGridY(Bool_t gridy)
Definition: TStyle.h:346
TROOT.h
TAxis::GetXmin
Double_t GetXmin() const
Definition: TAxis.h:133
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
TH1::GetYaxis
TAxis * GetYaxis()
Definition: TH1.h:318
TVirtualPad.h
y
Double_t y[n]
Definition: legend1.C:17
TStyle::SetPadGridX
void SetPadGridX(Bool_t gridx)
Definition: TStyle.h:345
TH1::SetStats
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8445
TH2.h
TH2::Fill
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:294
TCanvas
Definition: TCanvas.h:23
TComplex.h
gPad
#define gPad
Definition: TVirtualPad.h:287
TH1::GetNbinsY
virtual Int_t GetNbinsY() const
Definition: TH1.h:294
kKeyPress
@ kKeyPress
Definition: Buttons.h:26
TAxis::GetXmax
Double_t GetXmax() const
Definition: TAxis.h:134
TH1::GetXaxis
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:317
TH2F::Reset
virtual void Reset(Option_t *option="")
Reset this histogram: contents, errors, etc.
Definition: TH2.cxx:3551
TH1::GetNbinsX
virtual Int_t GetNbinsX() const
Definition: TH1.h:293
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2997