Rhinoscript:exercises:mathematic alchemy
From ksteinfeWiki
Contents |
Overview
A good way to get started thinking and modeling programmatically, is to work with simple two-dimensional mathematics, and build up complexity only gradually. For this exercise, we'll be learning a simple way to plot points in rhino which describe familiar mathematical forms in XY space. From there, we'll look at methods for combining these forms into three-dimensional hybrids, which can quickly take on more complex forms.
Let's begin by reproducing the functionality of a friendly graphing calculator:
Prerequisites
To complete this exercise, you'll need a solid understanding of:
Plotting a Sine Wave
Much of what we do in architecture is rooted in trigonometry, so a sine wave ought to seem like familiar territory. Below is an example of a script which creates a set of points describing a sine wave in the XY-plane. The basic math here is simple, the X coord marches along from 0->2PI in regular increments, and the Y coord fluctuates in a sine wave, as a 'function' of X.
Mathematically, we can express this as:
x=theta
y=Sin(theta)
When we express equations in terms of X and Y (and Z), this is called a "parametric equation". In this case, theta is the parametric variable - meaning that X and Y can be expressed in terms of theta. For us, this is a most useful way of expressing things mathematically. Look for these forms of equations whenever you can.
We can programmatically plot a parametric equation as seen in the script below. Notice how I've setup this 'For' loop to iterate across the parametric variable. Also notice how we may affect the frequency of the wave with another variable I've built-in called dblFrequency. For this first go around, try copying this script, and altering it in some way... perhaps adding a variable to alter the Amplitude as well.
Dim dblFrequency
dblFrequency = Rhino.GetReal("frequency?")
'' sine wave
Dim x,y,z,theta
For theta = 0 To Rhino.Pi()*2 Step 0.1
x = theta
y = Sin(theta*dblFrequency)
z = 0
Rhino.addPoint(array(x,y,z))
Next
Plotting a Circle
Let's step it up a bit and try plotting something that I can't think of the equation for off the top of my head: a circle. To plot a circle, I first had to lookup the parametric form of the equation for a circle.
Mathworld, published by Wolfram Research is an excelled source for this kind of thing, but can be a bit heady. So, this time, I looked up Wikipedia's entry for circle. A short way down the page, in the 'analytic results' section, you can find the parametric form of the circle equation, which is expressed as:
x = a+r*cos(theta)
y = b+r*sin(theta)
where (a,b) is the center of the circle, and r is the radius of the circle. Below, I've implemented a slightly simplified version of this equation, leaving out the (a,b), and only parameterizing the circle's radius.
Your next task is to modify this script, adding in the bit where we can define the center AND radius of the circle through user prompts. For extra credit, rather than prompting for the user to type these coords, ask her to pick a point in space with her mouse.
Dim dblRadius
dblRadius = Rhino.GetReal("radius?")
'' circle
Dim x,y,z,theta
For theta = 0 To Rhino.Pi()*2 Step 0.1
x = Cos(theta)*dblRadius
y = Sin(theta)*dblRadius
z = 0
Rhino.addPoint(array(x,y,z))
Next
Plotting a Golden Spiral
Now, let's try something really challenging. Pretty much golden anything is a crowdpleaser... sections, ratios, showers. Mimicking a range of natural phenomenon from nautilus shells to spiral arms of the milky way, the golden spiral (also called a logarithmic spiral) is no exception.
Checking out wikipedia's entry on the logarithmic spiral, we find the following parametric form of the equation:
x = a*E^(b*theta)*cos(theta)
y = a*E^(b*theta)*sin(theta)
where E is a mathematical constant (equal to 2.718 or so), and a and b are arbitrary positive real constants, which affect the form of the spiral, but in strange sort of ways (a value of appx 1.0 for each is pretty good).
Below is the code for plotting a spiral using this equation. Notice that things get a little trickier here: our equations get a bit longer, and we have to define the mathematical constant 'E' to achieve what we're after.
Your task this section is to go out and find your own two dimensional form, expressed mathematically in parametric form. Then, use this template we've been working with to plot points programmatically. You should include at least one user-defined variable which affects the points plotted. Here are a couple of links to get your search started:
index of mathematic curves (wikipedia)
Dim dblA, dblB
dblA = Rhino.GetReal("A?")
dblB = Rhino.GetReal("B?")
Dim dblE
dblE = 2.71828183
'' log spiral
Dim x,y,z,theta
For theta = 0 To Rhino.Pi() Step 0.1
x = dblA*(dblE^(dblB*theta))*Cos(theta)
y = dblA*(dblE^(dblB*theta))*Sin(theta)
z = 0
Rhino.addPoint(array(x,y,z))
Next
Plotting a Mathematic Hybrid
So, it seems that we should be pretty comfortable by now picking up just about any parametric equation for a two dimensional form and plotting it out. Let's mix things up a bit.
Below is an example of a script which combines the two formulas above in three dimensions to create a set of points describing a hybrid mathematical form - a circle in plan, and a sine wave in section. As hybrid forms go, the one pictured below is pretty tame, you could easily do better.
Your last task for this exercise is to mimic what I've done below, combining two of the formulas we've learned thus far (including one that you've come across yourself). Be sure and plot the points in three dimensions, and add in at least two user-defined variables which alter the form.
Dim dblFrequency, dblRadius
dblFrequency = Rhino.GetReal("frequency?")
dblRadius = Rhino.GetReal("radius?")
'' sine saddle
Dim x,y,z,theta
For theta = 0 To Rhino.Pi()*2 Step 0.1
x = Sin(theta)*dblRadius
y = Cos(theta)*dblRadius
z = Sin(theta*dblFrequency)
Rhino.addPoint(array(x,y,z))
Next
