{ "cells": [ { "cell_type": "markdown", "id": "8f0fd3b2", "metadata": {}, "source": [ "# hist104_TH2Poly_fibonacci\n", "\n", "In mathematics, the Fibonacci sequence is a suite of integer in which\n", "every number is the sum of the two preceding one.\n", "\n", "The first 10 Fibonacci numbers are:\n", "\n", "1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...\n", "\n", "This tutorial computes Fibonacci numbers and uses them to build a TH2Poly\n", "producing the \"Fibonacci spiral\" created by drawing circular arcs connecting\n", "the opposite corners of squares in the Fibonacci tiling.\n", "\n", "\n", "\n", "\n", "**Author:** Olivier Couet \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:13 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "bc9f7c34", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:18.088032Z", "iopub.status.busy": "2026-05-19T20:13:18.087922Z", "iopub.status.idle": "2026-05-19T20:13:18.414075Z", "shell.execute_reply": "2026-05-19T20:13:18.413621Z" } }, "outputs": [], "source": [ "void Arc(int n, double a, double r, double *px, double *py);\n", "void AddFibonacciBin(TH2Poly *h2pf, double N);" ] }, { "cell_type": "markdown", "id": "84eef463", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 2, "id": "c71b9da9", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:18.416141Z", "iopub.status.busy": "2026-05-19T20:13:18.416010Z", "iopub.status.idle": "2026-05-19T20:13:18.420481Z", "shell.execute_reply": "2026-05-19T20:13:18.419932Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void Arc(int n, double a, double r, double *px, double *py)\n", "{\n", " // Add points on a arc of circle from point 2 to n-2\n", "\n", " double da = TMath::Pi() / (2 * (n - 2)); // Angle delta\n", "\n", " for (int i = 2; i <= n - 2; i++) {\n", " a = a + da;\n", " px[i] = r * TMath::Cos(a) + px[0];\n", " py[i] = r * TMath::Sin(a) + py[0];\n", " }\n", "}" ] }, { "cell_type": "markdown", "id": "011eb459", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 3, "id": "f2724a22", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:18.421855Z", "iopub.status.busy": "2026-05-19T20:13:18.421732Z", "iopub.status.idle": "2026-05-19T20:13:18.429235Z", "shell.execute_reply": "2026-05-19T20:13:18.428704Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void AddFibonacciBin(TH2Poly *h2pf, double N)\n", "{\n", " // Add to h2pf the bin corresponding to the Fibonacci number N\n", "\n", " double X1 = 0.; //\n", " double Y1 = 0.; // Current Fibonacci\n", " double X2 = 1.; // square position.\n", " double Y2 = 1.; //\n", "\n", " static int MoveId = 0;\n", "\n", " static double T = 1.; // Current Top limit of the bins\n", " static double B = 0.; // Current Bottom limit of the bins\n", " static double L = 0.; // Current Left limit of the bins\n", " static double R = 1.; // Current Right limit of the bins\n", "\n", " const int NP = 50; // Number of point to build the current bin\n", " double px[NP]; // Bin's X positions\n", " double py[NP]; // Bin's Y positions\n", "\n", " double pi2 = TMath::Pi() / 2;\n", "\n", " switch (MoveId) {\n", " case 1:\n", " R = R + N;\n", " X2 = R;\n", " Y2 = T;\n", " X1 = X2 - N;\n", " Y1 = Y2 - N;\n", " px[0] = X1;\n", " py[0] = Y2;\n", " px[1] = X1;\n", " py[1] = Y1;\n", " px[NP - 1] = X2;\n", " py[NP - 1] = Y2;\n", " Arc(NP, 3 * pi2, (double)N, px, py);\n", " break;\n", "\n", " case 2:\n", " T = T + N;\n", " X2 = R;\n", " Y2 = T;\n", " X1 = X2 - N;\n", " Y1 = Y2 - N;\n", " px[0] = X1;\n", " py[0] = Y1;\n", " px[1] = X2;\n", " py[1] = Y1;\n", " px[NP - 1] = X1;\n", " py[NP - 1] = Y2;\n", " Arc(NP, 0., (double)N, px, py);\n", " break;\n", "\n", " case 3:\n", " L = L - N;\n", " X1 = L;\n", " Y1 = B;\n", " X2 = X1 + N;\n", " Y2 = Y1 + N;\n", " px[0] = X2;\n", " py[0] = Y1;\n", " px[1] = X2;\n", " py[1] = Y2;\n", " px[NP - 1] = X1;\n", " py[NP - 1] = Y1;\n", " Arc(NP, pi2, (double)N, px, py);\n", " break;\n", "\n", " case 4:\n", " B = B - N;\n", " X1 = L;\n", " Y1 = B;\n", " X2 = X1 + N;\n", " Y2 = Y1 + N;\n", " px[0] = X2;\n", " py[0] = Y2;\n", " px[1] = X1;\n", " py[1] = Y2;\n", " px[NP - 1] = X2;\n", " py[NP - 1] = Y1;\n", " Arc(NP, 2 * pi2, (double)N, px, py);\n", " break;\n", " }\n", "\n", " if (MoveId == 0)\n", " h2pf->AddBin(X1, Y1, X2, Y2); // First bin is a square\n", " else\n", " h2pf->AddBin(NP, px, py); // Other bins have an arc of circle\n", "\n", " h2pf->Fill((X1 + X2) / 2.5, (Y1 + Y2) / 2.5, N);\n", "\n", " MoveId++;\n", " if (MoveId == 5)\n", " MoveId = 1;\n", "}" ] }, { "cell_type": "markdown", "id": "b7cd1142", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": 4, "id": "495b5593", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:18.430464Z", "iopub.status.busy": "2026-05-19T20:13:18.430337Z", "iopub.status.idle": "2026-05-19T20:13:18.634887Z", "shell.execute_reply": "2026-05-19T20:13:18.634052Z" } }, "outputs": [], "source": [ "int N = 7;" ] }, { "cell_type": "markdown", "id": "bf26796e", "metadata": {}, "source": [ "N = number of Fibonacci numbers > 1" ] }, { "cell_type": "code", "execution_count": 5, "id": "79e23bde", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:18.636657Z", "iopub.status.busy": "2026-05-19T20:13:18.636523Z", "iopub.status.idle": "2026-05-19T20:13:18.840253Z", "shell.execute_reply": "2026-05-19T20:13:18.839437Z" } }, "outputs": [], "source": [ "TCanvas *C = new TCanvas(\"C\", \"C\", 800, 600);\n", "C->SetFrameLineWidth(0);\n", "\n", "TH2Poly *h2pf = new TH2Poly(); // TH2Poly containing Fibonacci bins.\n", "h2pf->SetTitle(Form(\"The first %d Fibonacci numbers\", N));\n", "h2pf->SetMarkerColor(kRed - 2);\n", "h2pf->SetStats(0);\n", "\n", "double f0 = 0.;\n", "double f1 = 1.;\n", "double ft;\n", "\n", "AddFibonacciBin(h2pf, f1);\n", "\n", "for (int i = 0; i <= N; i++) {\n", " ft = f1;\n", " f1 = f0 + f1;\n", " f0 = ft;\n", " AddFibonacciBin(h2pf, f1);\n", "}\n", "\n", "h2pf->Draw(\"A COL L TEXT\");" ] }, { "cell_type": "markdown", "id": "f07fec9b", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 6, "id": "a1d860c6", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:18.841969Z", "iopub.status.busy": "2026-05-19T20:13:18.841848Z", "iopub.status.idle": "2026-05-19T20:13:19.063283Z", "shell.execute_reply": "2026-05-19T20:13:19.062531Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "