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];
- // the current fibonacci numbers
- int current = 1;
- int previous = 0;
- // the current bounding box
- int left = 0;
- int right = 1;
- int top = 0;
- int bottom = 0;
- // the number of boxes you want to draw
- 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: // attach to bottom of current rectangle
- drawRectangle(left, right, bottom, bottom + current);
- bottom += current;
- break;
- case 1: // attach to right of current rectangle
- drawRectangle(right, right + current, top, bottom);
- right += current;
- break;
- case 2: // attach to top of current rectangle
- drawRectangle(left, right, top - current, top);
- top -= current;
- break;
- case 3: // attach to left of current rectangle
- drawRectangle(left - current, left, top, bottom);
- left -= current;
- break;
- }
- // update fibonacci number
- int temp = current;
- current += previous;
- previous = temp;
- }
- }
- }
- private void drawRectangle(int left, int right, int top, int bottom)
- {
- // set the scale as well as horizontal and vertical offsets.
- int scalevar = 23;
- int offsetx = 152;
- int offsety = 180;
- if (inputted_numer_of_fibonacci_boxes_to_draw == 7)
- {
- offsety = 362;
- }
- // draw top line of the fibonacci box.
- 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);
- // draw bottom line of the fibonacci box.
- 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);
- // draw right line of the fibonacci box.
- 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);
- // draw left line of the fibonacci box.
- 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);
- // set arc parameters for the first fibonacci box.
- 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;
- }
- // set arc parameters for the second fibonacci box.
- 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;
- }
- // set arc parameters for the third fibonacci box.
- 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;
- }
- // set arc parameters for the fourth fibonacci box.
- 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;
- }
- // set arc parameters for the fifth fibonacci box.
- 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;
- }
- // set arc parameters for the sixth fibonacci box.
- 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;
- }
- // set arc parameters for the seventh fibonacci box.
- 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;
- }
- // draw the above specified arc for each value of loop counter
- // variable, "fibonacci_counter".
- drawArc(x_start, y_start, x_end, y_end, curve_size_x, curve_size_y);
- }
- 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();
- // starting cordinates of arcs
- pthFigure1.StartPoint = new Point(x_start, y_start);
- ArcSegment arcSeg1 = new ArcSegment();
- // ending cordinates of arcs
- 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);
- }
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.

Santhakumar MunuswamyPosted Apr 25, 2015, 7:51 AM
Good work
Gowtham RajamanickamPosted Apr 18, 2015, 3:58 AM
good
Vithal WadjePosted Feb 4, 2015, 1:17 PM
nice keep it up
Michal HabalcikPosted Feb 2, 2015, 6:16 AM
never heard about Fibonacci Spiral before, thanks for sharing
Onlymorons onthiswebsitePosted Feb 1, 2015, 3:12 PM
Your code is extremely repetitive. Please read this http://www.ifsq.org/indicator-SPM-3.html