Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist104_TH2Poly_fibonacci.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook -js
4/// A TH2Poly build with Fibonacci numbers.
5///
6/// In mathematics, the Fibonacci sequence is a suite of integer in which
7/// every number is the sum of the two preceding one.
8///
9/// The first 10 Fibonacci numbers are:
10///
11/// 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...
12///
13/// This tutorial computes Fibonacci numbers and uses them to build a TH2Poly
14/// producing the "Fibonacci spiral" created by drawing circular arcs connecting
15/// the opposite corners of squares in the Fibonacci tiling.
16///
17/// \macro_image
18/// \macro_code
19///
20/// \date September 2016
21/// \author Olivier Couet
22
23void Arc(int n, double a, double r, double *px, double *py);
24void AddFibonacciBin(TH2Poly *h2pf, double N);
25
26void hist104_TH2Poly_fibonacci(int N = 7)
27{
28 // N = number of Fibonacci numbers > 1
29
30 TCanvas *C = new TCanvas("C", "C", 800, 600);
31 C->SetFrameLineWidth(0);
32
33 TH2Poly *h2pf = new TH2Poly(); // TH2Poly containing Fibonacci bins.
34 h2pf->SetTitle(Form("The first %d Fibonacci numbers", N));
35 h2pf->SetMarkerColor(kRed - 2);
36 h2pf->SetStats(0);
37
38 double f0 = 0.;
39 double f1 = 1.;
40 double ft;
41
43
44 for (int i = 0; i <= N; i++) {
45 ft = f1;
46 f1 = f0 + f1;
47 f0 = ft;
49 }
50
51 h2pf->Draw("A COL L TEXT");
52}
53
54void Arc(int n, double a, double r, double *px, double *py)
55{
56 // Add points on a arc of circle from point 2 to n-2
57
58 double da = TMath::Pi() / (2 * (n - 2)); // Angle delta
59
60 for (int i = 2; i <= n - 2; i++) {
61 a = a + da;
62 px[i] = r * TMath::Cos(a) + px[0];
63 py[i] = r * TMath::Sin(a) + py[0];
64 }
65}
66
67void AddFibonacciBin(TH2Poly *h2pf, double N)
68{
69 // Add to h2pf the bin corresponding to the Fibonacci number N
70
71 double X1 = 0.; //
72 double Y1 = 0.; // Current Fibonacci
73 double X2 = 1.; // square position.
74 double Y2 = 1.; //
75
76 static int MoveId = 0;
77
78 static double T = 1.; // Current Top limit of the bins
79 static double B = 0.; // Current Bottom limit of the bins
80 static double L = 0.; // Current Left limit of the bins
81 static double R = 1.; // Current Right limit of the bins
82
83 const int NP = 50; // Number of point to build the current bin
84 double px[NP]; // Bin's X positions
85 double py[NP]; // Bin's Y positions
86
87 double pi2 = TMath::Pi() / 2;
88
89 switch (MoveId) {
90 case 1:
91 R = R + N;
92 X2 = R;
93 Y2 = T;
94 X1 = X2 - N;
95 Y1 = Y2 - N;
96 px[0] = X1;
97 py[0] = Y2;
98 px[1] = X1;
99 py[1] = Y1;
100 px[NP - 1] = X2;
101 py[NP - 1] = Y2;
102 Arc(NP, 3 * pi2, (double)N, px, py);
103 break;
104
105 case 2:
106 T = T + N;
107 X2 = R;
108 Y2 = T;
109 X1 = X2 - N;
110 Y1 = Y2 - N;
111 px[0] = X1;
112 py[0] = Y1;
113 px[1] = X2;
114 py[1] = Y1;
115 px[NP - 1] = X1;
116 py[NP - 1] = Y2;
117 Arc(NP, 0., (double)N, px, py);
118 break;
119
120 case 3:
121 L = L - N;
122 X1 = L;
123 Y1 = B;
124 X2 = X1 + N;
125 Y2 = Y1 + N;
126 px[0] = X2;
127 py[0] = Y1;
128 px[1] = X2;
129 py[1] = Y2;
130 px[NP - 1] = X1;
131 py[NP - 1] = Y1;
132 Arc(NP, pi2, (double)N, px, py);
133 break;
134
135 case 4:
136 B = B - N;
137 X1 = L;
138 Y1 = B;
139 X2 = X1 + N;
140 Y2 = Y1 + N;
141 px[0] = X2;
142 py[0] = Y2;
143 px[1] = X1;
144 py[1] = Y2;
145 px[NP - 1] = X2;
146 py[NP - 1] = Y1;
147 Arc(NP, 2 * pi2, (double)N, px, py);
148 break;
149 }
150
151 if (MoveId == 0)
152 h2pf->AddBin(X1, Y1, X2, Y2); // First bin is a square
153 else
154 h2pf->AddBin(NP, px, py); // Other bins have an arc of circle
155
156 h2pf->Fill((X1 + X2) / 2.5, (Y1 + Y2) / 2.5, N);
157
158 MoveId++;
159 if (MoveId == 5)
160 MoveId = 1;
161}
#define a(i)
Definition RSha256.hxx:99
#define R(a, b, c, d, e, f, g, h, i)
Definition RSha256.hxx:110
@ kRed
Definition Rtypes.h:66
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define N
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
The Canvas class.
Definition TCanvas.h:23
2D Histogram with Polygonal Bins
Definition TH2Poly.h:66
const Int_t n
Definition legend1.C:16
TF1 * f1
Definition legend1.C:11
RooArgList L(Args_t &&... args)
Definition RooArgList.h:156
constexpr Double_t C()
Velocity of light in .
Definition TMath.h:114
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Definition TMath.h:598
constexpr Double_t Pi()
Definition TMath.h:37
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:592