C# Fibonacci Sequence Problem

I am trying to create a Fibonacci sequence curve in Grasshopper in C#. My plan is to create the numerical sequence, then change these numbers to squares and at the end make arcs out of these squares to get the resulting Fibonacci sequence curve. However, when I was working on it, I didn’t think how I would place the rectangles in order for them to go in this pattern.

What I have now:

What I want to get:

Here is the GH file:
Fibonacci.gh (4.8 KB)

1 Like

My first though is different approach, that doesn’t even require calculation of sizes of these squares:

  1. Take existing squares
  2. Get Bounding Box for them together (BBox will be a rectangle then)
  3. Take longer side of this Bounding Box
  4. Create square with this longer side of BBox
  5. Place it next to longer side of BBox
  6. Go to point 1. and continue

To place it to the correct side of the BBox we should track which side is taken, as there is pattern:
Bottom, Left, Top, Right, Bottom, Left, Top, Right, Bottom, Left, Top, Right and so on and on.

Just the idea, I guess there is plenty of better existing solutions :wink:

1 Like

See attached.

GoldenSpiral_EntryLevel_V1.gh (128.5 KB)

BTW: 12 years old ??? If so feel free to ask ANY C# question (either in public or via PM).

2 Likes

Try this:


Fibonacci re.gh (4.1 KB)

The idea is something like this:
each rectangle (square for you btw) have corners; first at bottom left, second at bottom right, etc etc.

We can locate the center and orientation of the next square by looking at the first 2 corners of the last square in the list, pick the second corner and move it towards the first corner by the distance of “i” in the fibonacci series. Then put there a plane correctly aligned…

private void RunScript(int number, ref object R, ref object A)
  {

    List<int> FibonacciNumbers = FibonacciSeriesNumber(number);
    List<Rectangle3d> rects = new List<Rectangle3d>();

    for(int i = 0; i < number; i++){
      if(i == 0){rects.Add(new Rectangle3d(Rhino.Geometry.Plane.WorldXY, FibonacciNumbers[i], FibonacciNumbers[i]));}
      else{
        Rhino.Geometry.Point3d BR = rects[i - 1].Corner(1);
        Rhino.Geometry.Point3d BL = rects[i - 1].Corner(0);
        Rhino.Geometry.Vector3d V = BR - BL;
        V.Unitize();
        V *= FibonacciNumbers[i];
        BR -= V;
        Rhino.Geometry.Plane plane = new Plane(BR, Vector3d.CrossProduct(V, Vector3d.ZAxis), V);
        rects.Add(new Rectangle3d(plane, FibonacciNumbers[i], FibonacciNumbers[i]));
      }
    }

    List<Arc> arcs = new List<Arc>();

    foreach(Rectangle3d rect in rects){
      arcs.Add(new Arc(rect.Corner(1), (rect.Corner(2) - rect.Corner(1)), rect.Corner(3)));
    }

    R = rects;
    A = arcs;
  }

  // <Custom additional code> 

  List<int> FibonacciSeriesNumber(int number){
    int firstNumber = 1;
    int secondNumber = 1;
    List<int> Fibonacci = new List<int>();

    if(number > 0)Fibonacci.Add(firstNumber);
    if(number > 1)Fibonacci.Add(secondNumber);

    for(int i = 2; i < number; i++){
      int indexLast = Fibonacci.Count - 1;
      int indexPenultimate = Fibonacci.Count - 2;
      Fibonacci.Add(Fibonacci[indexLast] + Fibonacci[indexPenultimate]);
    }

    return Fibonacci;
  }
1 Like

@w.radaczynski @maje90 @PeterFotiadis
Thank you all for the rapid responses you have given me, and the amazing solutions. My problem is solved, thanks to you and this amazing community. I will play with all of the different solutions, and if I will have any questions regarding them I will contact you directly!

I can’t express enough, how much I am in awe of the support you have provided me! :grinning: :pray:
I am only 13 and it is phenomenal, how much you have helped me! Especially in the Covid times, a forum like this is one of the few places where you can gain any programming experience or interaction.

It is definitely an exceptional community.

I do need to get back to learning, so you are an inspiration! Btw, quite an impressive portfolio you have on your site for such a young age, congratulations!! Your dad rocks!

Enjoy your young years and don’t forget to have fun too!

2 Likes