aa_lsystemThis is a featured page

/*
***************************************************************************
This library contains two functions for working with Lindenmayer Systems
(L-Systems). These two functions are aa_lsystem_replace and
aa_lsystem_draw, and together they perform all the recursive replacement
and drawing operations needed to create complex fractal forms.


***************************************************************************
Functions:
***************************************************************************
---------------------------------------------------------------------------
aa_lsystem_replace(string $start,string $rules[],int $iterations)
---------------------------------------------------------------------------
This function iteratively replaces characters within a string ($start)
based on a set of rules. The rules are defined using an array of
symbolic strings. For example, if the rule is "Replace all 'a' with
'aba'" it is written as "a->aba". The number of iterations in the
replacement operation is defined with the '$iterations' argument.
Example:
//start with a simple string
string $initial_string;
$initial_string = "a";
//define some rules of replacement and put them in an array
string $rules[];
$rules[0] = "a->aba";
//iteratively replace and return the final result into the variable $tmp
string $tmp;
$tmp = aa_lsystem_replace("a",$rules,2);
//print the result
print($tmp);

---------------------------------------------------------------------------
aa_lsystem_draw(string $lsystemstring,string $drawing_defs[],vector $start_position,float $start_az,float $start_alt)
---------------------------------------------------------------------------
*NOTE:* The function aa_lsystem_draw requires the aa_turtle.mel library of
drawing functions.
The aa_lsystem_draw function takes the results from the
aa_lsystem_replace function and draws them using splines. The function
takes 5 arguments, the string to process, the drawing definition, the
starting position, the starting altitude angle and the starting azimuth
angle. The drawing definitions map specific letters in the l-system
generated string to drawing actions (moving and turning). These are
defined in a manner similar to the l-system rules from
aa_lsystem_replace. If the rule is "'a' means turn 90 degrees in azimuth
and 10 degrees in altitude" then then the rule would be 'a->turn 90 10'.
Angles are always defined in degrees. The starting position, azimuth and
altitude are used to position and orient the drawing tool before the
drawing process begins.

Example:

//create some rules
$drawrules[0] = "a->move 10";
$drawrules[1] = "b->turn 90 0";
//draw a line using the rules and an lsystem string (using the $tmp string
// from the aa_lsystem_replace example)
aa_lsystem_draw($tmp,$drawrules,<<0,0,0>>,0,0);


***************************************************************************
Further Notes & Examples
***************************************************************************

'Quadratic Koch Island' - A typical fractal structure
$tmp = "a+a+a+a";
$rules[0] = "a->a+a-a-aa+a+a-a";
$drawrules[0] = "a->move 1";
$drawrules[1] = "+->turn 90 0";
$drawrules[2] = "-->turn -90 0";
$tmp = aa_lsystem_replace($tmp,$rules,2);
aa_lsystem_draw($tmp,$drawrules,<<0,0,0>>,0,0);

'Recursion' - Push and Pop
This function also can work recursively, allowing 'branching' of the
drawing. If the l-system string contains '[' or ']' the drawing function
will 'push' or 'pop' elements in a drawing stack, effectively making a
branch or bud.
A tree
$tmp = "a";
$rules[0] = "a->a[-a]a[+a][a]";
$drawrules[0] = "a->move 1";
$drawrules[1] = "+->turn 30 0";
$drawrules[2] = "-->turn -30 0";
$tmp = aa_lsystem_replace($tmp,$rules,3);
aa_lsystem_draw($tmp,$drawrules,<<0,0,0>>,90,0);


Other examples of using this script can be found at http://www.nthd.org/
************************************************************
WARNING: this script is memory intensive and too large of a
number used for iterations may cause performance trouble
************************************************************
Cory Clarke - cory@nthd.org
***************************************************************************
*/

global proc string aa_lsystem_draw(string $start,string $rules[],vector $startposition,float $startazimuth, float $startaltitude){

//given a set of rules, translates an lsystem string into a drawing
global int $keyframe;
string $turtle;
int $rule_set_size;
int $thisrule_length;
string $thisrule;
string $thisrule_operand;
string $thisrule_operation;
string $command;
int $advanceoff;
int $i;

//$turtle = aa_turtle_newturtle("CURVEONSURFACE");
$turtle = aa_turtle_newturtle("CURVE");
aa_turtle_setPosition($turtle,$startposition);
aa_turtle_setDirection($turtle,$startazimuth,$startaltitude);

$advanceoff = 0;
$position = 1;
$length = `size($start)`;
$nextchar = `substring $start $position $position`;
$rule_set_size = `size($rules)`;
while($nextchar != ""){

$keyframe++;
//if it is a pop or push, do it
if($nextchar == "["){
//push
string $remainder;
int $remainderpos;
$remainderpos = $position + 1;
$remainder = `substring $start $remainderpos $length`;
float $currentposition[];
$currentposition = aa_turtle_getPosition($turtle);
float $currentdirection[];
$currentdirection = aa_turtle_getDirection($turtle);

$start = aa_lsystem_draw($remainder,$rules,<<$currentposition[0],$currentposition[1],$currentposition[2]>>,rad_to_deg($currentdirection[0]),rad_to_deg($currentdirection[1]));

$position = 1;

$length = `size($start)`;
if($length>0){
$nextchar = `substring $start $position $position`;
}else{
$nextchar = "";
}

if($nextchar == "["){
$advanceoff = 1;
}

}
if($nextchar == "]"){
//pop
string $remainder;
int $remainderpos;
$remainderpos = $position + 1;
$remainder = `substring $start $remainderpos $length`;

return $remainder;

}

//check against rules
$i = 0;
while($i < $rule_set_size){
$thisrule = $rules[$i];
$thisrule_length = `size($thisrule)`;
$thisrule_operand = `substring $thisrule 1 1`;
$thisrule_operation = `substring $thisrule 4 $thisrule_length`;
string $tmp[];
int $num;
$num = `tokenize $thisrule_operation " " $tmp`;

if($nextchar == $thisrule_operand){
if($tmp[0] == "turn"){
aa_turtle_turn($turtle,$tmp[1],$tmp[2]);
}else{
aa_turtle_move($turtle,$tmp[1]);
}
}

/*
if($keyframe%20 == 0){
aa_turtle_snapshotAtKeyframe($turtle,$keyframe);
}
*/
$i++;
}
//status blip
print(".");

if($advanceoff == 0){
//get the next char
$position = $position + 1;
if($position <= $length && $position > 0){
$nextchar = `substring $start $position $position`;
}else{
$nextchar = "";
}
}else{
$advanceoff = 0;
}

}
return "";
}

global proc string aa_lsystem_replace(string $start, string $rule[],int $recursion){
string $final;
string $tmp;
string $nextchar;
string $final;
int $length;
int $ruleset;
int $rule_set_size;
int $thisrule_length;
string $thisrule;
string $thisrule_operand;
string $thisrule_replace;


//step through start string and perform rules
$rule_set_size = `size($rule)`;
$position = 1;
$length = `size($start)`;
$nextchar = `substring $start $position $position`;
print("---------------");
print($recursion + "\n");
//if last recursion, return
if($recursion == 0){
return $start;
}

while($nextchar != ""){
//replace the char with the rule
//rule structure a->abcd
$i = 0;
$ruleset = 0;
while($i < $rule_set_size){
$thisrule = $rule[$i];
$thisrule_length = `size($thisrule)`;
$thisrule_operand = `substring $thisrule 1 1`;
$thisrule_replace = `substring $thisrule 4 $thisrule_length`;

if($nextchar == $thisrule_operand){

$final = $final + $thisrule_replace;
$ruleset = 1;
}
$i++;
}

//if there was no rule for this character
if($ruleset == 0){
$final = $final + $nextchar;
}

//get the next char
$position = $position + 1;
if($position <= $length){
$nextchar = `substring $start $position $position`;
}else{
$nextchar = "";
}
}



print($final + "\n");
$recursion = $recursion - 1;
$tmp = aa_lsystem_replace($final,$rule,$recursion);
return $tmp;
}


print("aa_lsystem....loaded\n");


gilakos
gilakos
Latest page update: made by gilakos , Sep 18 2006, 3:27 PM EDT (about this update About This Update gilakos Edited by gilakos

901 words added

view changes

- complete history)
Keyword tags: None (edit keyword tags)
More Info: links to this page

Anonymous  (Get credit for your thread)


There are no threads for this page.  Be the first to start a new thread.