Expertise

Grasshopper Python 106

Loops (II)

In the previous lesson we introduced some loops. Working with conditional statements and loops together with numbers and strings will be more than enough most of the times, remember you still have all the power of out-of-the-box grasshopper, so Point3d is part of the RhinoCommon library. Being a Reference-Type class, we should declare it as we did with lists. Let’s create a point from scratch with components: We can do the whole thing if we pass the coordinates to the constructing sentence:  A Spiral Mixing point lists and loops we can start building familiar curves. Distance Meter The following code takes a point cloud and returns the distance between each of them and a sample point together with the furthest and closest one A key aspect here is to initialize the minDist and maxDist variables with the right values. If you pick 0.0 as the initial value for minDist there is no lower value possible, and if you pick a random number, you might be assuming there will be a distance smaller than that number. That is why we pick a real value as the first state.

Nested Loops

Nested loops are loops happening inside other loops. This gives access to complex operations where we need to cross-evaluate certain variables or work with multiple dimensions. It is also worth nothing that nested loops are dangerous if used without care, a loop within a loop within a loop, each one having 100 steps will result in a million calls being processed by the computer, and depending on the case, this could hang your computer without mercy. Let’s see an example of nested loops to create a grid of points. Keep in mind that each loop needs its iterator.

Challenge

1. Write a component that creates points arranged in the shape of a sphere with Radius R and centered at the origin. 2. Write a component that creates points arranged in the shape of a thorus with radius R1 and R2 and centered at the origin. [EDIT] Challenge Solutions: Challenge 1. Points Sphere: Challenge 2. Points Torus:  A little note: Please note that in this two examples, the inner and outer loops can be swapped together and that script would “build” the points then following Parallels > Meridians. See the example with the sphere: Download the solutions here (GH 0.9.0014)

Final Challenge

Write a component that calculates the first n prime numbers, with n being a reasonable number, not too big.

Leave a Reply

Your email address will not be published. Required fields are marked *