I occasionally get together with my cousin, who is a physics and technology buff. He has been after me for a long time to create software to generate a “Fibonacci Spiral”. I kept putting him off because of all the other business things I need to take care of. Finally, after I found some daylight with my crazy work schedule, I decided to make the programming for his “Fibonacci Spiral”. However, I wanted to make it into a Windows Phone app since I needed fresh app ideas for movies to position on my application development website.
Next, the C# code for scaling and drawing the adjacent Fibonacci boxes is shown below. Actually, this was slightly modified from some code I found online:
- void drawthespiralaroundtheorigin()
- {
- int a;
- int[] convert_to_number = new int[2];
-
- int current = 1;
- int previous = 0;
-
- int left = 0;
- int right = 1;
- int top = 0;
- int bottom = 0;
-
-
- App appx = Application.Current as App;
- string numberofboxes = appx.numberofboxes_public;
-
- for (a = 0; a < 2; a++)
- {
- convert_to_number[a] = 0;
- if (numberofboxes.Substring(a, 1) == "0") convert_to_number[a] = 0;
- if (numberofboxes.Substring(a, 1) == "1") convert_to_number[a] = 1;
- if (numberofboxes.Substring(a, 1) == "2") convert_to_number[a] = 2;
- if (numberofboxes.Substring(a, 1) == "3") convert_to_number[a] = 3;
- if (numberofboxes.Substring(a, 1) == "4") convert_to_number[a] = 4;
- if (numberofboxes.Substring(a, 1) == "5") convert_to_number[a] = 5;
- if (numberofboxes.Substring(a, 1) == "6") convert_to_number[a] = 6;
- if (numberofboxes.Substring(a, 1) == "7") convert_to_number[a] = 7;
- if (numberofboxes.Substring(a, 1) == "8") convert_to_number[a] = 8;
- if (numberofboxes.Substring(a, 1) == "9") convert_to_number[a] = 9;
- }
- inputted_numer_of_fibonacci_boxes_to_draw = (convert_to_number[0] * 10) + (convert_to_number[1] * 1);
-
-
- if (inputted_numer_of_fibonacci_boxes_to_draw >= 3 && inputted_numer_of_fibonacci_boxes_to_draw <= 7)
- {
-
-
- for (fibonacci_counter = 0; fibonacci_counter < inputted_numer_of_fibonacci_boxes_to_draw; fibonacci_counter++)
- {
-
- switch (fibonacci_counter % 4)
- {
- case 0:
- drawRectangle(left, right, bottom, bottom + current);
- bottom += current;
- break;
- case 1:
- drawRectangle(right, right + current, top, bottom);
- right += current;
- break;
- case 2:
- drawRectangle(left, right, top - current, top);
- top -= current;
- break;
- case 3:
- drawRectangle(left - current, left, top, bottom);
- left -= current;
- break;
- }
-
-
- int temp = current;
- current += previous;
- previous = temp;
-
- }
-
-
- }
-
-
- }
You will notice from above that I’m calling a function, “drawRectangle”, to make the
“Fibonacci boxes”. The code I found online to do this used a directive within this function to create the rectangles or
“Fibonacci boxes”. This code was used for a desktop application. I could not find a rectangle drawing function to work the way I wanted it to for Windows Phone, so I improvised and made my own code to create the
“Fibonacci boxes” with a series of connected lines as detailed below. After I create the
“Fibonacci boxes”, I then set the parameters for drawing the arcs within each one of the
“Fibonacci boxes” that will eventually create the smooth
"Fibonacci spiral".
- private void drawRectangle(int left, int right, int top, int bottom)
- {
-
- int scalevar = 23;
- int offsetx = 152;
- int offsety = 180;
- if (inputted_numer_of_fibonacci_boxes_to_draw == 7)
- {
- offsety = 362;
- }
-
-
- Line myLine_north = new Line();
- myLine_north.Stroke = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
- myLine_north.X1 = (scalevar * left) + offsetx;
- myLine_north.Y1 = (scalevar * top) + offsety;
- myLine_north.X2 = (scalevar * right) + offsetx;
- myLine_north.Y2 = (scalevar * top) + offsety;
- myLine_north.StrokeThickness = 1;
- ContentPanel.Children.Add(myLine_north);
-
-
- Line myLine_south = new Line();
- myLine_south.Stroke = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
- myLine_south.X1 = (scalevar * left) + offsetx;
- myLine_south.Y1 = (scalevar * bottom) + offsety;
- myLine_south.X2 = (scalevar * right) + offsetx;
- myLine_south.Y2 = (scalevar * bottom) + offsety;
- myLine_south.StrokeThickness = 1;
- ContentPanel.Children.Add(myLine_south);
-
-
- Line myLine_east = new Line();
- myLine_east.Stroke = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
- myLine_east.X1 = (scalevar * right) + offsetx;
- myLine_east.Y1 = (scalevar * top) + offsety;
- myLine_east.X2 = (scalevar * right) + offsetx;
- myLine_east.Y2 = (scalevar * bottom) + offsety;
- myLine_east.StrokeThickness = 1;
- ContentPanel.Children.Add(myLine_east);
-
-
- Line myLine_west = new Line();
- myLine_west.Stroke = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
- myLine_west.X1 = (scalevar * left) + offsetx;
- myLine_west.Y1 = (scalevar * top) + offsety;
- myLine_west.X2 = (scalevar * left) + offsetx;
- myLine_west.Y2 = (scalevar * bottom) + offsety;
- myLine_west.StrokeThickness = 1;
- ContentPanel.Children.Add(myLine_west);
-
-
- if (fibonacci_counter == 0)
- {
- x_start = (scalevar * left) + offsetx;
- y_start = (scalevar * top) + offsety;
- x_end = (scalevar * right) + offsetx;
- y_end = (scalevar * bottom) + offsety;
- curve_size_x = 50;
- curve_size_y = 25;
- }
-
- if (fibonacci_counter == 1)
- {
- x_start = (scalevar * left) + offsetx;
- y_start = (scalevar * bottom) + offsety;
- x_end = (scalevar * right) + offsetx;
- y_end = (scalevar * top) + offsety;
- curve_size_x = 50;
- curve_size_y = 25;
- }
-
- if (fibonacci_counter == 2)
- {
- x_start = (scalevar * right) + offsetx;
- y_start = (scalevar * bottom) + offsety;
- x_end = (scalevar * left) + offsetx;
- y_end = (scalevar * top) + offsety;
- curve_size_x = 45;
- curve_size_y = 45;
- }
-
- if (fibonacci_counter == 3)
- {
- x_start = (scalevar * right) + offsetx;
- y_start = (scalevar * top) + offsety;
- x_end = (scalevar * left) + offsetx;
- y_end = (scalevar * bottom) + offsety;
- curve_size_x = 85;
- curve_size_y = 85;
- }
-
- if (fibonacci_counter == 4)
- {
- x_start = (scalevar * left) + offsetx;
- y_start = (scalevar * top) + offsety;
- x_end = (scalevar * right) + offsetx;
- y_end = (scalevar * bottom) + offsety;
- curve_size_x = 110;
- curve_size_y = 110;
- }
-
- if (fibonacci_counter == 5)
- {
- x_start = (scalevar * left) + offsetx;
- y_start = (scalevar * bottom) + offsety;
- x_end = (scalevar * right) + offsetx;
- y_end = (scalevar * top) + offsety;
- curve_size_x = 220;
- curve_size_y = 220;
- }
-
- if (fibonacci_counter == 6)
- {
- x_start = (scalevar * right) + offsetx;
- y_start = (scalevar * bottom) + offsety;
- x_end = (scalevar * left) + offsetx;
- y_end = (scalevar * top) + offsety;
- curve_size_x = 300;
- curve_size_y = 300;
- }
-
-
-
- drawArc(x_start, y_start, x_end, y_end, curve_size_x, curve_size_y);
-
- }
Next, I submit the code used to make the “Fibonacci Spiral” within the confines of each “Fibonacci box”. The spiral is actually a series of carefully connected arcs drawn within each “Fibonacci box”. If done meticulously, it will create the appearance of one smooth, continuous spiral.
- private void drawArc(int x_start, int y_start, int x_end, int y_end, int curve_size_x, int curve_size_y)
- {
-
- PathFigure pthFigure1 = new PathFigure();
-
- pthFigure1.StartPoint = new Point(x_start, y_start);
- ArcSegment arcSeg1 = new ArcSegment();
-
- arcSeg1.Point = new Point(x_end, y_end);
- arcSeg1.Size = new Size(curve_size_x, curve_size_y);
- arcSeg1.IsLargeArc = false;
- arcSeg1.SweepDirection = SweepDirection.Counterclockwise;
- arcSeg1.RotationAngle = 0;
- PathSegmentCollection myPathSegmentCollection1 = new PathSegmentCollection();
- myPathSegmentCollection1.Add(arcSeg1);
- pthFigure1.Segments = myPathSegmentCollection1;
- PathFigureCollection pthFigureCollection1 = new PathFigureCollection();
- pthFigureCollection1.Add(pthFigure1);
- PathGeometry pthGeometry1 = new PathGeometry();
- pthGeometry1.Figures = pthFigureCollection1;
- System.Windows.Shapes.Path arcPath1 = new System.Windows.Shapes.Path();
- arcPath1.Data = pthGeometry1;
- arcPath1.Stroke = new SolidColorBrush(Color.FromArgb(255, 255, 255, 0));
- ContentPanel.Children.Add(arcPath1);
-
- }
Finally, here is an image of the input screen for the number of “Fibonacci boxes” for the app to create. In this case, they can range from 3 to as many as 7. I declined to go beyond 7, not because of any programming limitation, but rather for the spatial considerations of the Windows Phone emulator display. There are also spirals for 5, 6 and 7 “Fibonacci boxes”.
This is the best way I could think of for creating a “Fibonacci Spiral”. If you have a better way, then I'd like to hear about it.
Input screen for number of Fibonacci boxes.
Spiral for 5 Fibonacci boxes.
Spiral for 6 Fibonacci boxes.
Spiral for 7 Fibonacci boxes.