z=f(x,y) type surfaces

As usual, we start with Command:EditPythonScript and later we swith the environment we are working on to the GhPython.

## (x,y) grid

Run the Python editor by "Command: Edit PythonScript".
A square shape grid of (x,y) coordinates can be obtained by running

xyGrid=list()
for i in range(-5,5,1):
for j in range(-5,5,1):
xyGrid.append((i,j))
print xyGrid


You may get
|[(-5, -5), (-5, -4), (-5, -3), (-5, -2), (-5, -1), (-5, 0), (-5, 1), (-5, 2), (-5, 3), (-5, 4), (-4, -5), (-4, -4), (-4, -3), (-4, -2), (-4, -1), (-4, 0), (-4, 1), (-4, 2), (-4, 3), (-4, 4), (-3, -5), (-3, -4), (-3, -3), (-3, -2), (-3, -1), (-3, 0), (-3, 1), (-3, 2), (-3, 3), (-3, 4), (-2, -5), (-2, -4), (-2, -3), (-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), (-2, 3), (-2, 4), (-1, -5), (-1, -4), (-1, -3), (-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2), (-1, 3), (-1, 4), (0, -5), (0, -4), (0, -3), (0, -2), (0, -1), (0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, -5), (1, -4), (1, -3), (1, -2), (1, -1), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, -5), (2, -4), (2, -3), (2, -2), (2, -1), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, -5), (3, -4), (3, -3), (3, -2), (3, -1), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (4, -5), (4, -4), (4, -3), (4, -2), (4, -1), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4)]
You can check the obtained result in more visual way.
Try
import rhinoscriptsyntax as rs
xyGrid=list()
for i in range(-5,5,1):
for j in range(-5,5,1):
xyGrid.append((i,j))
for xy in xyGrid:
rs.AddPoint(xy,xy,0)
#print xyGrid


Perhaps, the following one may be more readable.
import rhinoscriptsyntax as rs
xyGrid=list()
for i in range(-5,5,1):
for j in range(-5,5,1):
xyGrid.append((i,j))
for (x,y) in xyGrid:
rs.AddPoint(x,y,0)
#print xyGrid #### How to get more fine grid

Let's use rs.EnableRedraw(False) to disable the redraw feature.
Plus, let's use rs.frange() to obtain a more fine grid.

import rhinoscriptsyntax as rs
rs.EnableRedraw(False)
xyGrid=list()
for i in rs.frange(-5,5,0.5):
for j in rs.frange(-5,5,0.5):
xyGrid.append((i,j))
for (x,y) in xyGrid:
rs.AddPoint(x,y,0)
rs.EnableRedraw(True) Then, let's try

import rhinoscriptsyntax as rs
rs.EnableRedraw(False)
xyGrid=list()
for i in rs.frange(-5,5,0.5):
for j in rs.frange(-5,5,0.5):
xyGrid.append((i,j))
for (x,y) in xyGrid:
rs.AddPoint(x,y,x**2+y**2)
rs.EnableRedraw(True)


This is a simple case of surfaces given by the form of z=f(x,y). Before moving on to the GhPython environment, let's introduce a lambda function in advance.
import rhinoscriptsyntax as rs
rs.EnableRedraw(False)
xyGrid=list()
for i in rs.frange(-5,5,0.5):
for j in rs.frange(-5,5,0.5):
xyGrid.append((i,j))
func=lambda (x,y):x**2+y**2
z=map(func,xyGrid)
for ((x,y),_z) in zip(xyGrid,z):
rs.AddPoint(x,y,_z)
rs.EnableRedraw(True)


You may get the same result as the previous one.
A lambda function can take a tuple type object as its input.

## GhPython and z=f(x,y) type surfaces

#### z=x*x+y*y surface

Let's move on to the GhPython environment.
Run "Command:Grasshopper" and place a GhPython component to the campus.
Copy and paste the last code to the GhPython component.
Remove rs.EnableRedraw() functions.
Initialize a (which is the output parameter) as an empty list type object.
Then, add the point ids by a.append(rs.Add….
Finally, you may get

import rhinoscriptsyntax as rs
xyGrid=list()
for i in rs.frange(-5,5,0.5):
for j in rs.frange(-5,5,0.5):
xyGrid.append((i,j))
func= lambda (x,y):x**2+y**2
z=map(func,xyGrid)
a=list()
for ((x,y),_z) in zip(xyGrid,z):
a.append(rs.AddPoint(x,y,_z)) Connect 'a' with a SrfGrid component. You can find the SrfGrid component in Surface->Freeform->Surface from Points.
Check how many points are output from the GhPython component. If it is 441, input 21 to 'U ' of the SrfGrid component.  #### z=f(x,y) type surface given by Gaussian function

Now, we use a 2d version of the Gaussian function, which is given by

(1)
\begin{align} f(x,y)=z_0+Ae^{\frac{(x-x_0)^2+(y-y_0)^2}{2C^2}} \end{align}

This has 5 parameters, A,C,x0 and y0.
Write

import rhinoscriptsyntax as rs
import math
xyGrid=list()
for i in rs.frange(-5,5,0.5):
for j in rs.frange(-5,5,0.5):
xyGrid.append((i,j))
A=1.0
C=1.0
x0=0
y0=0
func=lambda (x,y):A*math.exp(-((x-x0)**2+(y-y0)**2)/(2*(C**2)))
z=map(func,xyGrid)
a=list()
for ((x,y),_z) in zip(xyGrid,z):
a.append(rs.AddPoint(x,y,_z))


and run. Now, let's comment out A,C,x0, and y0.
Then, take their values from input parameters.
Set up input parameters A,C,x0 and y0 as float parameters.
Connect sliders to those inputs.
Then, run
import rhinoscriptsyntax as rs
import math
xyGrid=list()
for i in rs.frange(-5,5,0.5):
for j in rs.frange(-5,5,0.5):
xyGrid.append((i,j))
#A=1.0
#C=1.0
#x0=0
#y0=0
func=lambda (x,y):A*math.exp(-((x-x0)**2+(y-y0)**2)/(2*(C**2)))
z=map(func,xyGrid)
a=list()
for ((x,y),_z) in zip(xyGrid,z):
a.append(rs.AddPoint(x,y,_z))  #### Sum of two functions

Now, set up input parameters A1,C1,x01,y01and A2,C2,x02,y02 as float parameters.
Connect sliders to those inputs.
Then, try

import rhinoscriptsyntax as rs
import math
xyGrid=list()
for i in rs.frange(-5,5,0.5):
for j in rs.frange(-5,5,0.5):
xyGrid.append((i,j))
func1=lambda (x,y):A1*math.exp(-((x-x01)**2+(y-y01)**2)/(2*(C1**2)))
func2=lambda (x,y):A2*math.exp(-((x-x02)**2+(y-y02)**2)/(2*(C2**2)))
Func=lambda (x,y):func1((x,y))+func2((x,y))
z=map(Func,xyGrid)
a=list()
for ((x,y),_z) in zip(xyGrid,z):
a.append(rs.AddPoint(x,y,_z))  Let's rewrite func1 and func2 as elements of one list type object.
import rhinoscriptsyntax as rs
import math
xyGrid=list()
for i in rs.frange(-5,5,0.5):
for j in rs.frange(-5,5,0.5):
xyGrid.append((i,j))
funcList=list()
funcList.append(lambda (x,y):A1*math.exp(-((x-x01)**2+(y-y01)**2)/(2*(C1**2))))
funcList.append(lambda (x,y):A2*math.exp(-((x-x02)**2+(y-y02)**2)/(2*(C2**2))))
def Func(point):
z=0
for func in funcList:
z+=func(point)
return z
z=map(Func,xyGrid)
a=list()
for ((x,y),_z) in zip(xyGrid,z):
a.append(rs.AddPoint(x,y,_z))


You may get the same result as the previous one.
Now
import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
import math
xyGrid=list()
for i in rs.frange(-5,5,0.5):
for j in rs.frange(-5,5,0.5):
xyGrid.append((i,j))
x01=P1.X
y01=P1.Y
x02=P2.X
y02=P2.Y
A1=4.0
C1=1.0
A2=-2.0
C2=2.0
func1= lambda (x,y):A1*math.exp(-((x-x01)**2+(y-y01)**2)/(2*C1**2))
func2= lambda (x,y):A2*math.exp(-((x-x02)**2+(y-y02)**2)/(2*C2**2))
func = lambda (x,y): func1((x,y))+func2((x,y))
a=list()
for (x,y) in xyGrid:
a.append(rs.AddPoint(x,y,func((x,y))))  page revision: 26, last edited: 08 Apr 2013 13:48