Rhinoscript:exercises:multidem mathematic alchemy
From ksteinfeWiki
Overview
Building on what we've learned in the Mathematic Alchemy Exercise, let's extend the lesson to utilize our new-found array skills.
Prerequisites
To complete this exercise, you'll need a solid understanding of:
As well as:
Plotting With Lines
As you may recall, our mathematic alchemy exercise left off with the plotting of a mathematic hybrid, a set of points which followed one geometric form in x-y space, and another in z-space. Your hybrid will likely be quite different from mine (mine's a bit tame), but here's what I got:
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
Which results in the form you see below when plotted in rhino.
Let's start off with a simple task: I'd like to plot lines along the points you see in the image above, rather than the points themselves.
To achieve this, we will, of course, need to know how to plot a curve in rhino - something we haven't done yet. Looking up how to add a curve object to rhino from rhinoscript, we come across the AddCurve() function. This function requires just one argument: the array of points we wish to plot. Recalling from our 2d array lession that a point is simply an array of three numbers (the coords of the point), it should be clear that an array of points is actually an array of arrays - a 2d array. So, it follows that somehow by the end of our script we need to arrive at a 2d array defining the points which lie along the curve we wish to plot.
Working in pseudocode, I would imagine this revised script would look something like this:
declare frequency and radius
ask user for frequency
ask user for radius
declare variables x,y,z, and theta
declare variable numberOfPoints
declare variable arrPts to store point data,
and initialize at zero storage capacity
declare variable n
For n goes from 0->numberOfPoints
'' find theta via linear interpolation
theta = (n/numberOfPoints)*2Pi
'' find the x,y,z, coords for this theta
x = Sin(theta)*radius
y = Cos(theta)*radius
z = Sin(theta*frequency)
'' remember the data for this point in our point array
ReDimension Preserve arrPts() to make an extra storage slot
set the Nth item in arrPts() equal to (x,y,z)
Next
'' now, the loop is finished, and we've got a nice
'' collection of points stored in our array
'' plot a curve through these points using AddCurve function
AddCurve(arrPts)
To achieve this, we'll need to utilize a two-dimensional array.
Dim dblFrequency, dblRadius
dblFrequency = 2
dblRadius = 1
Dim numberOfPoints
numberOfPoints = 10
ReDim arrPts(0)
Dim x,y,z,theta
Dim n
For n=0 To numberOfPoints
'' find theta via linear interperlation
theta = (n/numberOfPoints)*Rhino.Pi()*2
'' find xyz coords for this point
x = Sin(theta)*dblRadius
y = Cos(theta)*dblRadius
z = Sin(theta*dblFrequency)
'' remember this point in our ptArr
ReDim Preserve arrPts(n)
arrPts(n) = array(x,y,z)
Next
Call AddCurve(arrPts)
