Logo ROOT   6.12/07
Reference Guide
Fibonacci.C
Go to the documentation of this file.
1 
2 /// \file
3 /// \ingroup tutorial_hist
4 /// \notebook -js
5 /// A TH2Poly build with Fibonacci numbers.
6 ///
7 /// In mathematics, the Fibonacci sequence is a suite of integer in which
8 /// every number is the sum of the two preceding one.
9 ///
10 /// The first 10 Fibonacci numbers are:
11 ///
12 /// 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...
13 ///
14 /// This tutorial computes Fibonacci numbers and uses them to build a TH2Poly
15 /// producing the "Fibonacci spiral" created by drawing circular arcs connecting
16 /// the opposite corners of squares in the Fibonacci tiling.
17 ///
18 /// \macro_image
19 /// \macro_code
20 ///
21 /// \author Olivier Couet
22 
23 void Arc(int n, double a, double r, double *px, double *py);
24 void AddFibonacciBin(TH2Poly *h2pf, double N);
25 
26 void Fibonacci(int N=7) {
27  // N = number of Fibonacci numbers > 1
28 
29  TCanvas *C = new TCanvas("C", "C", 800, 600);
30  C->SetFrameLineWidth(0);
31 
32  TH2Poly *h2pf = new TH2Poly(); // TH2Poly containing Fibonacci bins.
33  h2pf->SetTitle(Form("The first %d Fibonacci numbers",N));
34  h2pf->SetMarkerColor(kRed-2);
35  h2pf->SetStats(0);
36 
37  double f0 = 0.;
38  double f1 = 1.;
39  double ft;
40 
41  AddFibonacciBin(h2pf, f1);
42 
43  for (int i=0; i<=N; i++) {
44  ft = f1;
45  f1 = f0 + f1;
46  f0 = ft;
47  AddFibonacciBin(h2pf, f1);
48  }
49 
50  h2pf->Draw("A COL L TEXT");
51 }
52 
53 void Arc(int n, double a, double r, double *px, double *py) {
54  // Add points on a arc of circle from point 2 to n-2
55 
56  double da = TMath::Pi()/(2*(n-2)); // Angle delta
57 
58  for (int i = 2; i<=n-2; i++) {
59  a = a+da;
60  px[i] = r*TMath::Cos(a) + px[0];
61  py[i] = r*TMath::Sin(a) + py[0];
62  }
63 }
64 
65 void AddFibonacciBin(TH2Poly *h2pf, double N) {
66  // Add to h2pf the bin corresponding to the Fibonacci number N
67 
68  double X1 = 0.; //
69  double Y1 = 0.; // Current Fibonacci
70  double X2 = 1.; // square position.
71  double Y2 = 1.; //
72 
73  static int MoveId = 0;
74 
75  static double T = 1.; //Current Top limit of the bins
76  static double B = 0.; //Current Bottom limit of the bins
77  static double L = 0.; //Current Left limit of the bins
78  static double R = 1.; //Current Right limit of the bins
79 
80  const int NP = 50; // Number of point to build the current bin
81  double px[NP]; // Bin's X positions
82  double py[NP]; // Bin's Y positions
83 
84  double pi2 = TMath::Pi()/2;
85 
86  switch (MoveId) {
87  case 1:
88  R = R+N;
89  X2 = R;
90  Y2 = T;
91  X1 = X2-N;
92  Y1 = Y2-N;
93  px[0] = X1;
94  py[0] = Y2;
95  px[1] = X1;
96  py[1] = Y1;
97  px[NP-1] = X2;
98  py[NP-1] = Y2;
99  Arc(NP,3*pi2,(double)N,px,py);
100  break;
101 
102  case 2:
103  T = T+N;
104  X2 = R;
105  Y2 = T;
106  X1 = X2-N;
107  Y1 = Y2-N;
108  px[0] = X1;
109  py[0] = Y1;
110  px[1] = X2;
111  py[1] = Y1;
112  px[NP-1] = X1;
113  py[NP-1] = Y2;
114  Arc(NP,0.,(double)N,px,py);
115  break;
116 
117  case 3:
118  L = L-N;
119  X1 = L;
120  Y1 = B;
121  X2 = X1+N;
122  Y2 = Y1+N;
123  px[0] = X2;
124  py[0] = Y1;
125  px[1] = X2;
126  py[1] = Y2;
127  px[NP-1] = X1;
128  py[NP-1] = Y1;
129  Arc(NP,pi2,(double)N,px,py);
130  break;
131 
132  case 4:
133  B = B-N;
134  X1 = L;
135  Y1 = B;
136  X2 = X1+N;
137  Y2 = Y1+N;
138  px[0] = X2;
139  py[0] = Y2;
140  px[1] = X1;
141  py[1] = Y2;
142  px[NP-1] = X2;
143  py[NP-1] = Y1;
144  Arc(NP,2*pi2,(double)N,px,py);
145  break;
146  }
147 
148  if (MoveId==0) h2pf->AddBin(X1,Y1,X2,Y2); // First bin is a square
149  else h2pf->AddBin(NP, px ,py); // Other bins have an arc of circle
150 
151  h2pf->Fill((X1+X2)/2.5, (Y1+Y2)/2.5, N);
152 
153  MoveId++;
154  if (MoveId==5) MoveId=1;
155 }
156 
157 
static double B[]
Definition: Rtypes.h:59
double T(double x)
Definition: ChebyshevPol.h:34
#define N
virtual Int_t AddBin(TObject *poly)
Adds a new bin to the histogram.
Definition: TH2Poly.cxx:215
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:38
static constexpr double L
constexpr Double_t Pi()
Definition: TMath.h:40
static double C[]
ROOT::R::TRInterface & r
Definition: Object.C:4
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2969
auto * a
Definition: textangle.C:12
char * Form(const char *fmt,...)
Double_t Cos(Double_t)
Definition: TMath.h:550
The Canvas class.
Definition: TCanvas.h:31
static constexpr double pi2
Double_t Sin(Double_t)
Definition: TMath.h:547
TF1 * f1
Definition: legend1.C:11
void SetFrameLineWidth(Width_t width=1)
Definition: TAttPad.h:77
virtual void SetTitle(const char *title)
See GetStatOverflows for more information.
Definition: TH1.cxx:6154
constexpr Double_t R()
Definition: TMath.h:213
const Int_t n
Definition: legend1.C:16
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8247
virtual Int_t Fill(Double_t x, Double_t y)
Increment the bin containing (x,y) by 1.
Definition: TH2Poly.cxx:605
2D Histogram with Polygonal Bins
Definition: TH2Poly.h:66