Step 1

week 3

 
Seeding

The exercise for the third week is to use MEL to make a virtual plant, or more accurately, a weed. The exercise use loops, 'turtle' commands, and if-else statements to create a script that has a plant-like morphology able to generate shapes while aware of its environment.


Step 1

Step 1


The first step is to use a 'while' loop and the turtle commands to draw a curve.



//make a loop that draws a curve

//make the length of the curve (the number of points) a variable
$length = 100;

//create a new turtle
$myturtle = aa_turtle_newturtle("CURVE");

$i = 0;

//loop while $i is less than the number of points
while($i < $length){

//move and turn the turtle
aa_turtle_move($myturtle,10);
aa_turtle_turn($myturtle,10,10);


$i++;
}


Step 2

Step 2


The next step is to make the 'while' loop from the step before able to generate different random curves using the 'rand()' function.



//make the length random
$length = rand(100);

//create a new turtle
$myturtle = aa_turtle_newturtle("CURVE");

//generate a random number for the move distance (between 1 and 10)
$move = rand(1,10);
//generate a random number for the azimuth and altitude turn
$turn = rand(10);
$turn2 = rand(10);

$i = 0;
while($i < $length){

//move and turn the turtle
aa_turtle_move($myturtle,$move);
aa_turtle_turn($myturtle,$turn,$turn2);


$i++;
}


Step 3

Step 3


The next step is to take our 'while' loop and nest it within another loop to create 10 random curves all at once.



//make a loop that makse 10 of them
$j = 0;

//loop while $j is less than 10
while($j < 10){

//create a new turtle for each loop
$myturtle = aa_turtle_newturtle("CURVE");

//pick some random numbers (these will be different each time the $j loops goes through
$length = rand(100);
$move = rand(1,10);
$turn = rand(10);
$turn2 = rand(10);

//grow a line
$i = 0;
while($i < $length){

aa_turtle_move($myturtle,$move);
aa_turtle_turn($myturtle,$turn,$turn2);


$i++;
}

$j++;
}


Step 4

Step 4


Now that we are able to make 10 random curves with the turtle functions, instead of drawing lines we will use then turtle functions to position spheres instead.



//loop through the same way as before
$j = 0;

while($j < 10){

//this time, when you create a turtle don't make a curve (leave out "CURVE")
$myturtle = aa_turtle_newturtle("");

$length = rand(100);
$move = rand(1,10);
$turn = rand(10);
$turn2 = rand(10);

$i = 0;
while($i < $length){

//move and turn the turtle
aa_turtle_move($myturtle,$move);
aa_turtle_turn($myturtle,$turn,$turn2);

//get the current x,y,z position of the turtle
$position = aa_turtle_getPosition($myturtle);

//create a sphere
sphere;
//move the sphere to the current position of the turtle
move $position[0] $position[1] $position[2];

$i++;
}

$j++;
}


Step 5

Step 5


The last step is to make this growing structure aware of its environment, this is done with and 'if' statement and the 'aa_isInsideGroup' command. The 'aa_isInsideGroup' command tests whether a point is inside the bounding box of a cube (or a cube within a group of cubes). To use this command you first need to create a couple of cubes, position them in the scene, and group them together into a single group (use the Edit->Group command in the pull-down menu). Name the new group 'testgroup'.



//use the same loop as above
$j = 0;

while($j < 10){

$myturtle = aa_turtle_newturtle("");

$length = rand(100);
$move = rand(1,10);
$turn = rand(10);
$turn2 = rand(10);

$i = 0;
while($i < $length){

aa_turtle_move($myturtle,$move);
aa_turtle_turn($myturtle,$turn,$turn2);

//get the position of the turtle
$position = aa_turtle_getPosition($myturtle);

//test if the position is NOT inside of any of the cubes in the test group
if(aa_isInsideGroup("testgroup",$position[0],$position[1],$position[2]) == 0){

//if it is not inside, make a sphere and move it
sphere;
move $position[0] $position[1] $position[2];
}

$i++;
}

$j++;
}


Step 6

Email a screen shot (JPG or GIF) and a copy of your script to cory@arch.columbia.edu by the end of class.



  • week 3