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).
void mygenerate(double factor, double cen_x, double cen_y)
{
printf("Regenerating...\n");
if(factor>0)
{
cen_x-factor*dx/2,
cen_x+factor*dx/2,
cen_y-factor*dy/2,
cen_y+factor*dy/2
);
}
else
{
if(last_histo!=NULL) delete last_histo;
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);
}
const int max_iter=50;
for(
int bx=1;bx<=last_histo->
GetNbinsX();bx++)
for(
int by=1;by<=last_histo->
GetNbinsY();by++)
{
int iter=0;
z=z*z+point;
iter++;
if(iter>max_iter) break;
}
}
last_histo->
Draw(
"colz");
printf("Done.\n");
}
void myexec()
{
int event =
gPad->GetEvent();
int px =
gPad->GetEventX();
int py =
gPad->GetEventY();
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;
{
return;
}
const double Z=2.;
switch(px){
case 'z':
mygenerate(1./Z, last_x, last_y);
break;
case 'u':
mygenerate(Z , last_x, last_y);
break;
case 'r':
mygenerate(-1 , last_x, last_y);
break;
};
}
void mandelbrot()
{
new TCanvas(
"canvas",
"View Mandelbrot set");
mygenerate(-1,0,0);
gPad->AddExec(
"myexec",
"myexec()");
}
R__EXTERN TStyle * gStyle
virtual Double_t GetBinCenter(Int_t bin) const
Return center of bin.
virtual Int_t GetNbinsY() const
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
virtual Int_t GetNbinsX() const
virtual void SetContour(Int_t nlevels, const Double_t *levels=0)
Set the number and values of contour levels.
virtual void Draw(Option_t *option="")
Draw this histogram with options.
virtual void SetBins(Int_t nx, Double_t xmin, Double_t xmax)
Redefine x axis parameters.
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
2-D histogram with a float per channel (see TH1 documentation)}
virtual void Reset(Option_t *option="")
Reset this histogram: contents, errors, etc.
Int_t Fill(Double_t)
Invalid Fill method.
void SetPadGridX(Bool_t gridx)
void SetPadGridY(Bool_t gridy)
- Author
- Luigi Bardelli barde.nosp@m.lli@.nosp@m.fi.in.nosp@m.fn.i.nosp@m.t
Definition in file mandelbrot.C.