00001 #include <TStyle.h>
00002 #include <TROOT.h>
00003 #include <TH2.h>
00004 #include <TComplex.h>
00005 #include <TVirtualPad.h>
00006 #include <TCanvas.h>
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 TH2F *last_histo=NULL;
00028
00029 void mygenerate(double factor, double cen_x, double cen_y)
00030 {
00031 printf("Regenerating...\n");
00032
00033 if(factor>0)
00034 {
00035 double dx=last_histo->GetXaxis()->GetXmax()-last_histo->GetXaxis()->GetXmin();
00036 double dy=last_histo->GetYaxis()->GetXmax()-last_histo->GetYaxis()->GetXmin();
00037 last_histo->SetBins(
00038 last_histo->GetNbinsX(),
00039 cen_x-factor*dx/2,
00040 cen_x+factor*dx/2,
00041 last_histo->GetNbinsY(),
00042 cen_y-factor*dy/2,
00043 cen_y+factor*dy/2
00044 );
00045 last_histo->Reset();
00046 }
00047 else
00048 {
00049 if(last_histo!=NULL) delete last_histo;
00050
00051 last_histo= new TH2F("h2",
00052 "Mandelbrot [move mouse and press z to zoom, u to unzoom, r to reset]",
00053 200,-2,2,200,-2,2);
00054 last_histo->SetStats(0);
00055 }
00056 const int max_iter=50;
00057 for(int bx=1;bx<=last_histo->GetNbinsX();bx++)
00058 for(int by=1;by<=last_histo->GetNbinsY();by++)
00059 {
00060 double x=last_histo->GetXaxis()->GetBinCenter(bx);
00061 double y=last_histo->GetYaxis()->GetBinCenter(by);
00062 TComplex point( x,y);
00063 TComplex z=point;
00064 int iter=0;
00065 while (z.Rho()<2){
00066 z=z*z+point;
00067 last_histo->Fill(x,y);
00068 iter++;
00069 if(iter>max_iter) break;
00070 }
00071 }
00072 last_histo->Draw("colz");
00073 gPad->Modified();
00074 gPad->Update();
00075 printf("Done.\n");
00076 }
00077
00078 void myexec()
00079 {
00080
00081 int event = gPad->GetEvent();
00082 int px = gPad->GetEventX();
00083 int py = gPad->GetEventY();
00084
00085
00086 double xd = gPad->AbsPixeltoX(px);
00087 double yd = gPad->AbsPixeltoY(py);
00088 float x = gPad->PadtoX(xd);
00089 float y = gPad->PadtoY(yd);
00090
00091 static float last_x;
00092 static float last_y;
00093
00094 if(event!=kKeyPress)
00095 {
00096 last_x=x;
00097 last_y=y;
00098 return;
00099 }
00100
00101 const double Z=2.;
00102 switch(px){
00103 case 'z':
00104 mygenerate(1./Z, last_x, last_y);
00105 break;
00106 case 'u':
00107 mygenerate(Z , last_x, last_y);
00108 break;
00109 case 'r':
00110 mygenerate(-1 , last_x, last_y);
00111 break;
00112 };
00113 }
00114
00115 void mandelbrot()
00116 {
00117
00118 gROOT->SetStyle("Plain");
00119 gStyle->SetPalette(1,0);
00120 gStyle->SetPadGridX(kTRUE);
00121 gStyle->SetPadGridY(kTRUE);
00122 new TCanvas("canvas","View Mandelbrot set");
00123 gPad->SetCrosshair();
00124
00125 mygenerate(-1,0,0);
00126
00127
00128 gPad->AddExec("myexec","myexec()");
00129 }