C# CIrcle Fractal

Hi wizards , I am new to C# in grasshopper. I have just started to experiment functions and recursions.
@Michael_Pryor helped me achieve this using just forloops. i m now trying to achieve same using function. Below is the file. let me know what i missed. TIA.

Circle Fractals.gh (45.3 KB)

Hi Shridhar,
I propose you that. I didn’t reuse your code.
I kept Circle without conversion to Curve, so the division is using PointAt. Datatree is also used to keep track of differents levels of recursion.

private void RunScript(List listOfRadius, int number, ref object A)
{
int numberOfSteps = listOfRadius.Count;
//In order to store the different levels
DataTree circles = new DataTree();
//The first circle
if (numberOfSteps > 0)
{
circles.Add(new Circle(Point3d.Origin, listOfRadius[0]), new GH_Path(0));
}
//For all the recursion steps
for (int i = 1; i < numberOfSteps; i++)
{
foreach(Circle circleIn in circles.Branch(i - 1))
{
circles.AddRange(Recursion(circleIn, listOfRadius[i], number), new GH_Path(i));
}
}
A = circles;
}

//
//Not exactly a recursion function
List Recursion(Circle circleIn, double radius, int number)
{
List allCircles = new List();
for (int i = 0; i < number; i++)
{
allCircles.Add(new Circle(circleIn.PointAt((double) i * Math.PI * 2.0 / (double) number), radius));
}
return allCircles;
}
// </Custom additional code>
}
Circle Fractals_LD.gh (50.3 KB)

3 Likes

Thank u Laurent, can I avoid gh data tree ?

I have managed to do similar stuff using function. But missing Flexibility.Can u suggest something :sweat_smile: ???

private void RunScript(int x, ref object A)
{

AllC = new List<Circle>();

DC(0, 0, x);

A = AllC;

}

//

ListAllC = new List();

void DC(double x, double y, int r){

Circle circle = new Circle(new Point3d(x, y, 0), r);

AllC.Add(circle);
if(r > 2){
  DC(x + r, y, r / 2);
  DC(x - r, y, r / 2);
  DC(x, y + r, r / 2);
  DC(x, y - r, r / 2);


}

}

Awesome stuff !