Random Points On Surface
Contents
Introduction
Expected Time: 20 min
A nice possible option in Grasshopper is to use a random generator to add randomness to your design. In this tutorial we will create a grid on a Rhino surface and then randomly pick a predefined number of grid points. I you have any trouble understanding this tutorial, it is advised to take a look back at Basic Principles Explained.
The Design
Before we start building the grasshopper model we need to create our Rhino Surface. This can be any kind of surface. In this tutorial we use a flat surface with two holes, created with the the function planar.
- Create a surface in Rhino, for example with
Creating the basic grid
The first step is to link the Rhino surface within our Grasshopper model. Right click on the surface parameter and set one surface. Next, click on the surface you just created.
- Create a surface parameter
- Link the Rhino surface to the surface parameter
To create the grid on the surface we will divide the surface.
- Add a divide surface component to the script
We want to be able to change the number of divisions in order to change the number of grid points on the surface.
- Connect two number sliders to the U and V input of the divide surface module
Cleaning up the list of points
When we create a panel and connect the output of our Divide Surface to it, we can see the values within the list of grid points.
- Connect a panel to the Points output of the divide surface component
What we notice immediately, are the list items with the value "NULL". These are points that could not be created because they are outside the surface. This is due to the way planar surfaces are created. A planar surface is basically a rectangular surface that has parts cut off to create the holes and the curved edges. When we divide the surface Grasshopper still tries to create points on the cut off pieces of the planar surface, but it will off course not succeed.
We want grasshopper to pick a number of grid points randomly, so we don't want grasshopper to try and pick one of the grid points that could not be created. Therefore we get rid of the "NULL"-values.
- Connect a Clean Tree component to the Points output of the divide surface component. Make sure you use the "tree" input of the Clean tree function.
When we look at the list we can still see that the values are grouped in smaller separate lists. To be able to pick the points more easily we will turn these smaller lists into one big list. Note the difference between the original list with smaller groups and the new flattened list.
- Flatten the created list of points
Introducing the random generator
To be able to use a random generator, first let's add a random component. The random component asks for a domain with a minimum value and a maximum value from which the function can choose. To find out between which values the random generator can choose, we need to retrieve the length of the list.
- Add a random function to the canvas
- Connect a construct domain to the domain input of the Random function
As computers start to count with zero, the start of the domain will be zero. In this case it might be the easiest to create a panel and type the value within the panel.
- Add a panel with value 0 to the Domain Start
The end of the domain will be equal to the amount of points in the list. However, there is one small issue. Computers start to count with index zero. That means the number of values is always 1 higher than the highest index in a list. Therefore we need to substract 1 from the list length to be able to get the correct domain.
- Connect a list length to the output of the Clean Tree. After that connect the output of the length to the Domain end.
Now add an expression to the input of the Domain End. This will probably be the first time you add an internal expression to your grasshopper script, so follow the steps carefully:
- Right click on the Domain End input. Select expression.
- Type x - 1 and click on OK.
The random generator still has two other inputs. The number of random values created (N) and the random seed (S). We will create number sliders for both inputs.
- Add a numberslider to the N and S input
The random seed might need a little more explanation. The random generator is a pseudo random generator. This means that for every seed you choose it creates random values, but it will always create the same random values for the same seed. If you do not like a random distribution, just try a different seed.
Finally, we have to set the output of the Random function to integer (round) values.
- Right click on the Random node and select Integer Numbers.
Now our random number generator is completed. The input is a number of values and a seed. The output is a selected amount of round numbers.
Now we can select random points of our flattened point list, by adding a list item.
- Connect the list input of a list item node to the flattened list.
- Connect the Random output to the list item index
We now have our final script! If you add some geometry to the points, a table or chair for example, you get a surface with randomly placed objects. Don't forget to set preview off for unnecessary nodes.
- Add a final point parameter for good practice
- Set preview off of unnecessary nodes
Alternative Approach
Grasshopper has several alternative methods to populate a geometry or domain with random points. For example:
, useful for populating rectangular shapes, , especially useful for populating boxes, and finally , for populating 3D geometry.For the assignment in this tutorial, it would have made way more sense to use the Populate Geometry node. However, considering that it is important to learn how the Random function works, we used the previous method. In the model you are building you may prefer to use the Populate Geometry function.