How to execute a code n times without copy the code many times

Hi all,
I hope you are all fine!.
I hope you can help me with a problem that I have in my program. Please let me explain you:
I made a program to project a rectangle onto a surface. The rectangle can be deformed as if it were a 4-bar mechanism. See the picture below:

P0 is the start point, and with the intersection between two spheres and the surface I can find J1. Now, to calculate J2 I need to know J1 before, and for to calculate J3 I need to know J2 before, and so on.

To do this, I copied and pasted the code N times, but it is not the smartest solution to muy program. I would be very grateful if someone could help me or suggest me some idea of how I can make that the rectangles spread on the surface, without copy the code many times.

NewShearing - Cilinder - copia.gh (56.5 KB)
Thank you for your time!.
Edwin Rosario

without looking into your file: looping can be done with plugin anemone

Iteration (looping) can be done with Anemone, not recursion.

1 Like

Hi!, Thank you for your suggestion. I have never used this plugin , but Im going to check it.

yes was a typo I corrected it before your reply

Do you think it would be easier with Anemone? Or do you recommend that I try to make a scrip?

I don’t think you need Anemone or Python for this. You certainly don’t need Python to compare two numbers. It’s difficult to separate your actual objective from all the code being used to accomplish it.

You can get all the surface frames this way (white group):

With those, it’s easy to move points and draw lines that are pulled to the surface. Standard GH will do.

In fact, is it really more complicated than this?


NewShearing_2020May20a.gh (19.6 KB)

1 Like

Or how about cutting holes through the surface?


NewShearing_2020May20b.gh (18.8 KB)

1 Like

Hi Joseph,
Thank you very much for your help!.
Your code is very short and I think that I can take some good ideas of your program.
I see that you use Move tool to make a copy of the output points of DivDist , and then you move those points on X direction, but what happen if a change the Shifting direction as in the figure below,


NewShearing - Cilinder - copia.gh (53.9 KB)
X0 is tangent to Shifting direction.
Z0 is normal to the surface
Y0 is the cross product between X0 and Z0.

in the next reference frame:

X1 is tangent to the line between P1 and J1
Z1 is normal to the surface.
Y1 is the cross product between X1 and Z1.

P0,P1,P2… are the start points and J1,J2,J3… are the point that o would like to calculate.

What I thought to calculate J1, J2, J3 … was the intersection of two spheres and the surface, with this procedure I calculate J1 and then J2 and so on.

Do you have any idea how to build rectangles when Shifting direction is changed?
Something important is that the shifting axis changes in each rectangle . (in each reference frame)

Thank you very much for your help and for your time.

Edwin Rosario.

Yeah, I know. I guess it goes something like this? I’m not paying full attention here, sorry if I’ve missed a detail. I think the point is made though that Anemone isn’t required?


NewShearing_2020May20c.gh (20.7 KB)

1 Like

Or maybe you’re thinking like this?


NewShearing_2020May20d.gh (21.8 KB)

Again, I’m not in “deep thought” mode about this, it’s relatively trivial no matter which way you want it.

Hi Joseph,

Thank you for your help. It seems that you have managed to do it and with a fairly short code, contrary to what I was doing. I will try to learn about you code.
Thank you for your time!
Edwin

btw: recursion in anemone is in fact possible I just found:

1 Like

I doubt it. Recursion is accomplished with a function that can call itself at each branch of a tree with results being accumulated in a stack. Anemone isn’t designed that way. Many things are possible with it, including nested loops, but true recursion? I think not.

All the technical mumbo jumbo aside, recursive behaviour is without a doubt possible. It just depends on your data structuring, and where and how you pass it Anemone. Whether that’s technically recursion or not is pretty irrelevant.

Let me for once entertain your smartassery. What if what’s defined between the Anemone loop components is the function body, and the loop start the function declaration/definition, where you pass in certain arguments (i.e. T, C, D0, D1, …, DX). The loop end component concludes the function and returns some values via relevant outputs. The body gets called over and over again. My guess would be that Anemone even calls itself.
Even in most programming or scripting languages that are privy to recursion, whether you copy data over and over, or simply edit the values of an object at the same memory address, is either up to you or not even in scope of things you can control, like in many managed languages.
To recurse or not to recurse? Is Grasshopper even true programming? Does it have any benefits going around correcting people about using the term “recursion” and contributing not much else to the discussion in question (which by the way I do here too)?