Normalize both of the new vectors:
$vec = unit($vec);
$dir = unit($dir);
Next, the dot product will be used.
7.2.13 The dot Command
The dot command returns a scalar value that is the product of two
vectors. This is a verbatim definition, so I will quickly demonstrate
the actual math that takes place to calculate a scalar value.
Multiply each component of each vector to one another, and add
the products together:
print (($vec.x * $dir.x) + ($vec.y * $dir.y) + ($vec.z * $dir.z));
-0.884532
The dot command performs the exact same function, with less
work:
dot($vec, $dir);
// Result: -0.884532 //
Now that I have covered exactly what the dot command does, let’s
return to our example. The reflect() procedure will return a calcula-
tion that looks as follows:
return $vec-2*dot($vec, $dir) * $dir;
To demonstrate this final step in the procedure, take the formula
above and store it into a vector variable called
$return:
vector $return = $vec - 2 * dot($vec,$dir) * $dir;
// Result: <<0.398039, 0.884532, 0.243246>> //
The next step is not part of the current procedure, and occurs later
in the tree() procedure. Not to confuse you, but I do not want to end
this demonstration without letting you see a final result. Add the
two vectors in
$return and $base together:
vector $end = $return + $base;
// Result: <<1.398039, 1.884532, 1.243246>> //
Chapter 7
412
Now create another curve using the new vector values:
curve
-degree 1
-point ($base.x) ($base.y) ($base.z)
-point ($end.x) ($end.y) ($end.z);
A shorter branch is positioned, reflecting the direction of the origi-
nal branch.
The final procedure returns a vector and takes two vectors as
arguments:
proc vector reflect(vector $vec, vector $dir)
{
// Create a normalized vector from the
// first argument passed from tree()
$vec = unit($vec);
// Create a normalized vector from the
// second argument passed from tree()
$dir = unit($dir);
Recursion
413
Chapter 7
Figure 7-36

Get Professional MEL Solutions for Production now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.