I do this to plot z=f(x,y) where the user codes f() in XojoScript and its fast. I think the problem is you’re recompiling for every point to compute. Instead I add boiler plate head and tail code to the user supplied source so that the input values are calculated in the script and then saved out to the context. All the sampling of the function happens in a single run of the script.
So a user script like
y = x * x
becomes something like
dim y As double
dim x As double
dim i As integer
for i = 0 to 200
x = i / 100 - 1
//========= user code
y = x * x
//=========
setCoord(i, y)
next
The context has a method for collecting the returned values and storing them in an array. When the script is finished that array is used for drawing.
For what it’s worth here’s the code I have, maybe you can make sense of it. sourceTA is the TextArea with user code, setCoord stores the values in the 2D array matField. display is an OpenGLSurface and dlField is a Display List object of the created model/drawing.
[code]Private Sub buildField()
script.Context = self
//============================ combine script source code
static srcHead As String
static srcTail As String
if srcHead = “” then
dim sa() As String
sa.Append “dim ix As integer”
sa.Append “dim iy As integer”
sa.Append “dim x As double”
sa.Append “dim y As double”
sa.Append “dim z As double”
sa.Append “for iy = 0 to 200”
sa.Append “for ix = 0 to 200”
sa.Append “y = iy / 100.0 - 1.0”
sa.Append “x = ix / 100.0 - 1.0”
sa.Append “”
srcHead = Join(sa, EndOfLine)
redim sa(-1)
sa.Append “”
sa.Append “setCoord(ix, iy, z)” //context
sa.Append “next”
sa.Append “next”
srcTail = Join(sa, EndOfLine)
end
script.Source = srcHead + sourceTA.Text.Trim + srcTail
//======================================= skip out if didn’t compile
redim scriptErrs(-1)
if script.Precompile(XojoScript.OptimizationLevels.High) then
errLabel.Text = “”
else
errLabel.Text = Join(scriptErrs)
return
end
//======================================= run script and fill all matField values
redim matField(200, 200)
script.Run
//======================================= build the ‘drawing’ from matField
display.MakeCurrent
dlField = new EZDisplayList
dlField.beginList
dim g As ezgl = display.getEZGL
dim x, y, xp1 As integer, tx0, tx1, ty As single
for x = 0 to 199
xp1 = x + 1
tx0 = x / 100
tx1 = xp1 / 100
g.draw.begin.quadStrip
for y = 0 to 200
ty = -y / 100’200
g.draw.textureCoord(tx0, ty)
g.draw.vertex(tx0, matField(x, y), ty)
g.draw.textureCoord(tx1, ty)
g.draw.vertex(tx1, matField(xp1, y), ty)
next
g.draw.endBegin
next
dlField.endList
display.Render
End Sub
[/code]