C# Recursion (Populating boxes)

Greetings!

Ive been trying to populate boxes randomly in C# with the help of recursion but it shows error. I’ve recently learned recursion in c# so kindly ignore my silly mistakes.

Any help would be appreciated.

Please find attached the reference what i an looking to code, and the GH file which has C# code.

refImage:


Grasshopper script : box fractal.gh (3.2 KB)

best
Alca

Hi, anyone can help?

hello
your script doesn’t seem related to the image. Image taken from a video that is not related to some recursion


It is very simple to reproduce the video as it is an 3d array of box followed by a random reduce.

In your script there are some data not declared before it is used so it is hard to know what you want.

Have you some sketch, pseudo algorithm … ?

moved to grasshopper category…

Hi @laurent_delrieu.

Sorry for not being clear, yes I’ve few more references which can clear what I was planning.
Please find links and image attached below:

  1. Fractal Cube03-recursive growth using Anemone - Grasshopper
    Here its done in Anemone, with some rules.

  2. This is exactly what I’m looking for and was trying on my script. this is done in grasshopper with anemone for recursion.

I hope i’m clear this time. and thank you so much for looking at my query.

Hi @DavidRutten

Thanks i’ll post grasshopper and c# related with grasshopper category tag.

Hello, you are a more clear, but still the logic has some differences. Example has quite no rules each cube is divided in 27 cubes and 8 or 9 cubes are removed. If you seek a C# not too fare I have done that


For the second, you are not very far in you script to get result. I will try to implement a logic but there are many possibilities… and it is like you tried to copy something without understanding what you did. Strange strange …

Hi,
yea you are correct, there was a recursion code for curves, which i was referring to.

private void RunScript(int n, ref object A)
{
if (n > 4)
{
    Component.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "n was lowered to 4");
    n = 4;
}
var boxes = new List<Box>();
boxes.Add(new Box(Plane.WorldXY, new Interval(0, 10), new Interval(0, 10), new Interval(0, 10)));
for (var i = 0; i < n - 1; i++)
{
    var temp = new List<Box>();
    foreach (var b in boxes)
        temp.AddRange(Subdivide(b));
    boxes = temp;
}
if (n > 0)
{
    var output = new List<GH_Box>();
    foreach (var b in boxes)
        output.AddRange(LastSubdivide(b));
    A = output;
}
else A = boxes;
}
// <Custom additional code> 
const double oneThird = 1.0 / 3.0;
const double twoThird = 2.0 / 3.0;
IEnumerable<Box> Subdivide(Box b)
{
var d = new Interval(0, (b.X.T1 - b.X.T0) * oneThird);
return new List<Box>()
{
new Box(new Plane(b.PointAt(oneThird, 0, 0), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(0, oneThird, 0), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(oneThird, oneThird, 0), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(twoThird, oneThird, 0), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(oneThird, twoThird, 0), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(0, 0, oneThird), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(oneThird, 0, oneThird), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(twoThird, 0, oneThird), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(0, oneThird, oneThird), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(oneThird, oneThird, oneThird), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(twoThird, oneThird, oneThird), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(0, twoThird, oneThird), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(oneThird, twoThird, oneThird), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(twoThird, twoThird, oneThird), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(oneThird, 0, twoThird), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(0, oneThird, twoThird), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(oneThird, oneThird, twoThird), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(twoThird, oneThird, twoThird), Vector3d.ZAxis), d, d, d),
new Box(new Plane(b.PointAt(oneThird, twoThird, twoThird), Vector3d.ZAxis), d, d, d),
};
}
IEnumerable<GH_Box> LastSubdivide(Box b)
{
var d = new Interval(0, (b.X.T1 - b.X.T0) * oneThird);
return new List<GH_Box>()
{
new GH_Box(new Box(new Plane(b.PointAt(oneThird, 0, 0), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(0, oneThird, 0), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(oneThird, oneThird, 0), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(twoThird, oneThird, 0), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(oneThird, twoThird, 0), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(0, 0, oneThird), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(oneThird, 0, oneThird), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(twoThird, 0, oneThird), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(0, oneThird, oneThird), Vector3d.ZAxis), d, d, d)),
//new GH_Box(new Box(new Plane(b.PointAt(oneThird, oneThird, oneThird), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(twoThird, oneThird, oneThird), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(0, twoThird, oneThird), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(oneThird, twoThird, oneThird), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(twoThird, twoThird, oneThird), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(oneThird, 0, twoThird), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(0, oneThird, twoThird), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(oneThird, oneThird, twoThird), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(twoThird, oneThird, twoThird), Vector3d.ZAxis), d, d, d)),
new GH_Box(new Box(new Plane(b.PointAt(oneThird, twoThird, twoThird), Vector3d.ZAxis), d, d, d)),
};
}
// </Custom additional code> 

BoxFractal.gh (2.7 KB)

7 Likes

Here a script working but the result is not nice.


box fractal LD.gh (4.4 KB)

Not as nice as the other from @Mahdiyar, transformed to mesh and some subdivisions applied to smooth it a bit


1 Like

For that type of result captured (equal sized boxes with some user controlled random voids) using recursion is kinda trying to tune a Harley Davidson (avoid at any cost).

Boxes_In3dGrid_V1.gh (117.9 KB)