Public Sub Tree(x as Integer, y as Integer, length as Integer, dir as Integer, Optional DX() as Integer, Optional DY() as Integer)
'initialize the graphic class
Var G As Graphics
Var Xnew, Ynew As Integer
'check dir
If dir < 0 Then dir = dir + 8
If dir >= 8 Then Dir = Dir -8
'calculate new position
Xnew = X + Length * DX(Dir)
Ynew = Y + length * DY(Dir)
'move to start point
Morph.MoveToPoint(x,y)
'draw a line to new position
Morph.AddLineToPoint(Xnew,Ynew)
'recursive calls for new branches
If length > 0 Then
Tree(Xnew,Ynew, length-1, dir-1)
Tree(Xnew,Ynew, length-1, dir+1)
End If
'draw the path
G.DrawPath(Morph)
End Sub
The parameters DX() and DY() are filled with integers. Initial run of the Tree() method is ok, but as soon as the recursive call is made these are both NIL. Parameters are declared as properties in the Window. How come?
Public Sub Tree(G as Graphics, x as Integer, y as Integer, length as Integer, dir as Integer, Optional DX() as Integer, Optional DY() as Integer)
'initialize the graphic class
Var Xnew, Ynew As Integer
'check dir
If dir < 0 Then dir = dir + 8
If dir >= 8 Then Dir = Dir -8
'calculate new position
Xnew = X + Length * DX(Dir)
Ynew = Y + length * DY(Dir)
'move to start point
Morph.MoveToPoint(x,y)
'draw a line to new position
Morph.AddLineToPoint(Xnew,Ynew)
'recursive calls for new branches
If length > 0 Then
Tree(G, Xnew,Ynew, length-1, dir-1,DX,DY)
Tree(G, Xnew,Ynew, length-1, dir+1,DX,DY)
End If
'draw the path
G.DrawPath(Morph)
End Sub
The second recursive call is never reached. But I want both calls to be executed as long as Length > 0. How to do that?
It is an implementation of a ‘biomorph’, originally developed by Richard Dawkins in Pascal.
It draws a more or less biological figure, controlled by ‘genes’ in the DX and DY arrays (values from -9 to 9). The first recursive call draws the left side and the second (where ‘dir+1’) draws the right side, which is identical but mirrored.
But because Length reaches zero by the first recursive calls, the second part is never reached.
In Pascal they are enclosed by a begin and end statement so they get called both (I guess)
'initialize the graphic class
Var Xnew, Ynew As Integer
Counter = Counter + 1
'check dir
If dir < 0 Then dir = dir + 8
If dir >= 8 Then Dir = Dir -8
'calculate new position
Xnew = X + Length * DX(Dir)
Ynew = Y + length * DY(Dir)
'move to start point
Morph.MoveToPoint(x,y)
'draw a line to new position
Morph.AddLineToPoint(Xnew,Ynew)
Length=Length-1
'recursive calls for new branches
If length > 0 Then
Tree(G, Xnew,Ynew, length, dir-1,DX,DY)
Tree(G, Xnew,Ynew, length, dir+1,DX,DY)
End If
'draw the path
G.DrawPath(Morph)
Ok. It finishes nicely, but only the first recursive call is executed and again, and again, until Length = 0. I see that in the debugger. I start with 6, 5 4 3 2 1 and with zero the If statement is not executed anymore so G.DrawPath gets called, drawing the lines.
Don’t forget that when Tree returns from the length=0 value, it goes up one level and then calls the other Tree. That should run and then return. You may need to step several times to get right back up the recursive calls and perhaps the debugger doesn’t show that very well.