Plotted Plants - Lindenmayer Systems and Fractals with Maple

Demostration by Steven Dugaro. 2D Reference: 'Maple a Comprehensive Introduction' by Roy Nicolaides.

This worksheet will attempt to build a shrub using maple plots programming and and export this document for viewing on the internet. The algorithm is a two step process.

Step 1 - Rule Definition

First we use an initialization procedure to define a rule. This is a rule for an l-system that is nothing more than a string of characters that define the path for a 'turtle' to follow. The rule characters are as follows:

Seed strings can be specified with a rewrite rule to be recursively define more sophisticated rules to prescribed number of levels. We will define the following rules:

> seed1 := [f]:

> rule1 := x -> subs(f=(f,f,p,l,p,f,m,f,m,f,r,m,l,m,f,p,f,p,f,r),x):

> levl1 := 3:

> thet1 := Pi/6:

> gamm1 := Pi/3:

> seed2 := [f]:

> rule2 := x -> subs(f=(f,l,p,f,r,f,l,m,f,r,f),x):

> levl2 := 4:

> thet2 := .4488:

> gamm2 := Pi/2:

Step 2 - Rule Interpretation

We need to write a proceedure to take the rule, the number of levels to recursively expand the string, and the amount by which to orient the turtle.
Our procedure will output a list of branches - the information necessary to connect a sequence of points, and we will use maple to plot these points.

> grow := proc(seed::list, rule::procedure, level::integer, theta::realcons, gamma::realcons)
local i,lst,plant,pts,stack,tt,gg,xx,yy,zz,xyztg;
xx:=0:yy:=0:zz:=0:tt:=Pi/2:gg:=0:lst:=NULL:stack:+[]:pts:=[xx,yy,zz]:plant:=seed:
for i from 1 to level do plant:= map(rule, plant) od;
for i in plant do
if i = p then tt:= tt + theta: gg:= gg + gamma:
elif i = m then tt:= tt - theta: gg:= gg - gamma:
elif i = l then stack := [ [xx,yy,zz,tt,gg], op(stack)]:
elif i = r then
if nops(stack) < 1 then error("Empty stack."):fi:
xyztg := stack[1]:
stack := subsop(1=NULL,stack):
xx := xyztg[1]:
yy := xyztg[2]:
zz := xyztg[3]:
tt := xyztg[4]:
gg := xyztg[5]:
lst:= lst,[pts]:
pts:= [xx,yy,zz]:
elif i = f then
xx := xx + cos(tt):
yy := yy + sin(tt):
zz := zz + sin(gg)*(-1)^rand():
pts:= pts,[xx,yy,zz]:fi:od:
plots[spacecurve]({lst,[pts]}, scaling=constrained):
end proc:

Step 3 - Graphic Generation

We now input our seed, rule, level or recursion, and tendancy of orientation into our rule interpreter and view the graphic

> grow(seed1,rule1,levl1,thet1,gamm1);

[Maple Plot]

> a:=%:

> grow(seed2,rule2,levl2,thet2,gamm2);

[Maple Plot]

> b:=%:

Step 4 - Output to the web

> with(JavaViewLib):

> setWorkingPath("C:\\temp\\"):

> exportHTM(a,plant1.mpl);

> exportMPL(b,plant2.mpl);

An applet based html page has now lives at c:\temp\htm\plant1.htm , referring to geometry data in c:\temp\mpl\plant1.mpl .
Export this worksheet as a static html file from Maple: File > Export As > Html, to
c:\temp\htm , then simply look for the

static <img> tags in the Maple export, and replace them with the dynamic <applet> tags from the JavaViewLib export.