# Can you convert this simple code to geometry nodes?

``````draw = function() {

rect(0,0,width,height);
var scaled = 128;
var offset = width/2;

for( var i=0; i<44; i++){
var x = Math.sin(i)*scaled + offset;
var y = Math.cos(i)*scaled + offset;
rect(x, y, 1, 1);
}
};
``````

xp28 khan processing to geometry nodes sin cos position.blend (146.6 KB)

Is there any particular reason for using â€śrepeatâ€ť though?
A simple solution would be: create 44 Points (Points node) > for each Index (i) perform the required math > Set Position > Instance whatever you want on points.
Since each iteration does not rely on the previous one, there should be no need for a cycleâ€¦

3 Likes

Yesâ€¦ adding to what @stray said, always try find a SIMD solution first - itâ€™ll be way more performant in the end.

e.g.:

3 Likes

Out of interest I tried to use the Repeat Zone for this:

â€¦It was actually completely unintuitive how the whole thing workedâ€¦ You have to start with an empty Geometry then join on each step a new object with the translated version of everything from the previous step while keeping track of the index. The final position is in some irrational configurationâ€¦

Weird stuff. The SIMD version is actually easier to reason about. I think the Repeat Zone makes more sense for the types of issues where in the past you would cut & paste a bunch of copies of the same node group.

4 Likes

Is it moving the center of the circle and adding a new cube at zero? I wonder what it looks like on various iterationsâ€¦

1 Like

Yup, thatâ€™s exactly what it is doing.

Integer radians in the â€śloopâ€ť just looks like itâ€™s building the circle in 6ths that slightly miss each other since pi = 3.14 thatâ€™s kinda what you can expect.

This is it after 7 iterations.

1 Like

Sometimes itâ€™s a really bad idea to â€śconvertâ€ť some paradigma into anotherâ€¦
â€¦itâ€™s better to think about the initial idea: drawing a circleâ€¦ and then use whatever possibilities are within the systemâ€¦
â€¦so here the width and height arenâ€™t even usedâ€¦ soâ€¦ how aboutâ€¦

2 Likes

Iâ€™m guessing this is the proper way of telling the nodes inside the loop what the current loop iteration is.

Iâ€™m trying to convert the Khan Academy intro to programming via drawing geometric shapes lessons to Blender python and thought it would be interesting to do it in geometry nodes now that it has something like a for-loop. https://www.khanacademy.org/profile/thinsoldier/projects