mandelbrot.C

Go to the documentation of this file.
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 // Using TExec to handle keyboard events and TComplex to draw the Mandelbrot set.
00011 // Author : Luigi Bardelli [ bardelli@fi.infn.it ]
00012 //
00013 // Pressing the keys 'z' and 'u' will zoom and unzoom the picture
00014 // near the mouse location, 'r' will reset to the default view.
00015 //
00016 // Try it (in compiled mode!) with:   root mandelbrot.C+
00017 //
00018 // Details:
00019 //    when a mouse event occurs the myexec() function is called (by
00020 //    using AddExec). Depending on the pressed key, the mygenerate()
00021 //    function is called, with the proper arguments. Note the
00022 //    last_x and last_y variables that are used in myexec() to store
00023 //    the last pointer coordinates (px is not a pointer position in
00024 //    kKeyPress events).
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   // resize histo:
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       // allocate first view...
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   // get event information
00081   int event = gPad->GetEvent();
00082   int px = gPad->GetEventX();
00083   int py = gPad->GetEventY();
00084 
00085   // some magic to get the coordinates...
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': // ZOOM
00104     mygenerate(1./Z, last_x, last_y);
00105     break;
00106   case 'u': // UNZOOM
00107     mygenerate(Z   , last_x, last_y);
00108     break;
00109   case 'r': // RESET
00110     mygenerate(-1   , last_x, last_y);
00111     break;
00112   };
00113 }
00114 
00115 void mandelbrot()
00116 {
00117   // cosmetics...
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   // this generates and draws the first view...
00125   mygenerate(-1,0,0);
00126 
00127   // add exec
00128   gPad->AddExec("myexec","myexec()");
00129 }

Generated on Tue Jul 5 15:44:20 2011 for ROOT_528-00b_version by  doxygen 1.5.1