# Fibonacci.inc

## POVRay include file –Fibonacci.inc

Fibonacci.inc
Portal ArticlesgroupKOS

### POVRay Code

```// Persistence of Vision Ray Tracer Scene Description File
// File: fibonacci.inc
// Vers: 3.7 beta
// Desc: Include file for Fibonacci numbers using Binet's formula.
//       Function: FibReal(n) gives the Fibonacci numbers between the positive and negative integers.
//       Macro: BinetCurve (parameters...) draws a section of Binet's Formula curve per parameters.
// Date: May 22, 2010

/* Useage
------------------------------------------------------------------------------------------------------------------------
#include "fibonacci.inc"

//     binetCurve   ( plotRadii, startReal, endReal, deltaX, piHarmonic, vertScale, pigmnt,   dots ) [object modifiers] }
object{binetCurve   ( .05,      -5.0,       5.0,     0.01,   180,        1,         Red*1.5,  no   ) rotate<0, 0, 0>    }

#declare F = fibReal(-5.5)
-------------------------------------------------------------------------------------------------------------------------
Note: Phi and phi variables, the binetCurve macro, and the fibReal function are
available to subsequent code following the include statement for this file.
*/

// Store computed Phi and phi calculated algebraically to the POVRay precision limit
#declare Phi = (pow(5,.5)+1)/2;                 // half of the sum of the square root 5 + 1
#declare phi = (pow(5,.5)-1)/2; // = Phi - 1;   // half of the difference square root 5 - 1
// http://mathworld.wolfram.com/FibonacciNumber.html

// Declare a function for Binet's Formula
#declare FibonacciReal = function(n) { (pow(+Phi/1, n) - pow(1/+Phi, n) * cos( n * pi )) / pow(5,.5) };

#macro BinetCurve( plotRadii, startReal, endReal, deltaX, piHarmonic, vertScale, pigmnt, dots  )
union{
// Store first plot point
#declare n      =  startReal ; // iterator index
#declare Fn     =  fibReal(n); // (pow((+Phi/1), n) - pow(1/+Phi, n) * cos( n * pi )) / pow(5,.5);
#declare theta  = n*piHarmonic;
#declare plot1  = vaxis_rotate(< n, Fn*vertScale, 0>, < -1,0,0>, theta);

// Draw a sphere on the plot

// Increment n
#declare n = n + deltaX;
#declare theta = n*piHarmonic;

#while ( n <= endReal )

// Calculate even-numbered plot point
//#declare Fn = (pow((+Phi/1), n) - pow(1/+Phi, n) * cos( n * pi )) / pow(5,.5);
#declare Fn = (pow((+Phi/1), n) - pow(1/+Phi, n) * cos( n * pi )) / pow(5,.5);

// Rotate the 2nd plot point by theta = n*piHarmonic
#declare plot2 = vaxis_rotate(< n, Fn*vertScale, 0>, < 1,0,0>, theta);

// Draw spheres at odd and even iterations along the plot curve
sphere{ plot2, plotRadii pigment{pigmnt} } //can be ommitted for most scenes

#if (!dots) //sphere of cylinder plot ?
// Draw a cylinder along the plot curve between odd and even points
cylinder{  plot1, plot2 plotRadii pigment{pigmnt }}
#end // sphere (dots) or cylinder plot

// Remember the last plotted point
#declare plot1 = plot2;

// Increment n and theta
#declare n = n + deltaX;
#declare theta = n*piHarmonic;
#end // --------------- end of while loop
} // end of union
#end // macro binetCurve()

#macro fiboDots( DotRadii, xStart, xEnd, xDelta, spin, pigmnt )
#declare xIter = xStart;
union{
#while (xEnd >= xIter)
sphere{ <xIter, fibReal(xIter), 0 >, DotRadii pigment{pigmnt} }
#declare xIter = xIter + xDelta;
#end
rotate<spin,0,0>
} // end union
#end // macro fiboDots()
```