## Example 3A

In the last example, we used a **Range** component to gradually increase the rotation of a set of rectangular section curves as they moved along a shape, thus creating a twist. In that case, we increased the rotation linearly (i.e. with the same step each time). If we were to plot that onto a graph we would get some kind of straight line graph of the form *y = mx*.

We don’t always want to increase things linearly, however. Say we want to define some other kind of relationship – how are we to do it?

One easy way of doing this graphically is with a **Graph Mapper** component (Params/Input). Starting from Example 3, drop one onto the canvas and connect the **R** output of our **Range** component into the input nodule on the left.

We will use the graph mapper to change the size of the rectangle profile curves as they go along the curve. Add a new **Scale** component (Transform/Affine) and insert it between the **Rectangle** component and the **Rotate** component such that the **R** output of the **Rectangle** component plugs into its **G** input and its **G** output plugs into the **G** input of the **Rotate** component.

The **C** input gives the centre of the scaling operation. By default, this is set to (0,0,0) so all of those frames will be being scaled around the origin. To scale them around their own centre plug the** F** output from the **Perp Frames** component in here. Grasshopper will automatically convert the planes from this output into points, using the global origin of each plane (which in this case is the centroid of our Rectangles as well).

Finally, connect the output of the **Graph Mapper** to the **F** input to control the scaling factor.

The **Scale** component will probably now go red. This is because we have not yet finshed setting up the **Graph Mapper** – it has no graph type specified. Because of that it is simply outputting whatever we put into it unchanged. Since the first value in the list coming out of our **Range** component is 0, that is being passed along and the **Scale** component is attempting to scale the first rectangle by a factor of 0 – resulting in a Null result.

To fix this, let’s set up a graph in the** Graph Mapper** so that this doesn’t happen. Right click on the **Graph Mapper** and go down to ‘Graph types’ in the context menu. Select ‘Linear’. A straight line graph should now appear inside the Mapper.

What the **Graph Mapper** does is to take each of the values that are input into it, treat it as an* x* value and read off the equivalent *y* value from the graph. The linear graph type defaults to y=x, so it is the same as not having done anything – however on that line you should see two little handles that you can drag around in order to change the graph curve. Move them so that the line no longer passes through zero to fix the problem with the Scale component.

Move the handles around and note the effect this has on the shape of our twisted form.

Now try changing the graph type – experiment with the different kinds and see what results you get out.

## Example 4

The Graph Mapper is a nice tool for getting graphical control over the progression of a particular variable, but it is limited to certain pre-set graph types and can be hard to get exact control over. Sometimes you will want to get actual mathematical equations involved, and to do that we will now look at how to plot a mathematical function in Grasshopper.

Start a new definition and add a new **Range** component (Sets/Sequence). Set up Number Sliders to control both the Domain and Step Number.

Now add an** Evaluate** component from Maths/Script. This component lets you apply your own mathematical formula to a set of inputs. The number of these inputs is adjustable – if you zoom in on the component then you will see a set of small + and – symbols appear next to the **x** and **y** inputs. These can be used to add or remove inputs from the component. For the moment, we just want to take in a single input – **x** – so click on the small ‘-’ next to the **y** input to delete it.

Now plug the output of our Range component into **x**. We are going to take these *x* values, apply a function to them to get *y* and then plot *x* against *y*. The next step is to define the function. If you double-click on the **Evaluate** component an expression editor interface will pop up which displays a lot of the functions that it’s possible to use and also will perform basic error checking on the equation for you as you type. Alternatively, you can input a formula as a text string into **F**, which is what we will do here, using a **Panel** (Params/Input) to type our equation into. For starters, we’ll draw a straight line graph – add a **Panel**, plug its output into **F** and type in:

*2*x*

(The * is the sign we use for multiplication)

Now we need to plot the points and draw the line. To accomplish the former, add a **Construct Point** component to the document. Pretty much identical to **VectorXYZ**, **Construct Point** lets you, well, construct a point by specifying its **X**, **Y** and **Z** coordinates. Plug the output of our **Range** into **X** and the output of our **Evaluate** component into **Y**. You should see a straight line of points appear.

To actually draw the plotted curve itself we will use the **Interpolate** component (Curve/Spline). This works similarly to the **CurveThroughPt** command in Rhino in that it takes in a set of points and creates a curve (with a given degree) that passes through all of them. Plug the **Pt** output from our **Construct Point** component into the **V** input of an **Interpolate** component.

Now we can experiment with different equations and see what they do. Here are some of the operators and other functions that we can use:

*+* Add

*-* Subtract

*** Multiply

*/* Divide

*^* To the power of

% Modulus (divide by a given number and return the remainder)

*sqrt(x)* The square root of *x*

*sin(x) *The sine of *x*

*cos(x)* The cosine of *x*

*tan(x)* The tangent of *x*

*cot(x) * The cotangent of x

*asin(x)* The arcsine of x (inverse sine)

*acos(x)* The arccosine of x (inverse cosine)

*atan(x) * The arctangent of x (inverse tan)

For a full list of all available functions, go into the expression designer and click the button on the top right.

The complete example files for this session can be downloaded here: