See it in action @ https://rafaelcenzano.com/apjava/tendron
In this lab, you will use recursion to generate a tendron. A tendron has a cluster of 7 tendrils. At the end of each tendril is a cluster of 7 smaller tendrils. This pattern of clusters of tendrils repeats until the tendrils reach a minimum tendril length. You can think of this as a slightly more complex Fractal tree. Instead of 2 branches in a v shape, there are 7 tendrils that radiate in all directions. Instead of straight branches, the tendrils wiggle randomly left and right as they radiate outward.
Your code will draw the Tendron recursively, modifying the structure as the elements get smaller, going through smaller tendrils and clusters. Every time the draw()
function runs, it generates a new and different Tendron since growth occurs randomly.
The main program, Tendron.pde
, sets up the size()
and background()
. Then when draw()
is called, it creates a new tendron by calling the constructor of the Cluster
class, telling it to create a cluster of tendrils starting at the midpoint of the screen with each tendril using 50 short segments.
The cluster of seven tendrils each randomly turn a bit left or right as they move out from the center. At the end of each tendril the new cluster will have fewer line segments, therefore each tendril will be shorter than before. Recursively, these tendrils will again spawn clusters until a minimum tendril length is reached. Here is the final result:
When drawn, the tendrils together look very plant-like for such a simple algorithm. The effect is enhanced by using the tendril length to determine the stroke
used when drawing.
-
Fork and clone down this repository
-
Complete the
Tendril
class constructor -
Write code in the
Cluster
class constructor to make onenew Tendril()
with the same initial number of segments and starting (x,y) coordinates. Give the tendril a random angle between 0 and 2π. Then call the tendril'sshow()
function. We'll add more tendrils to the cluster later. -
Complete the
Tendril
classshow()
function which does the actual drawing. Theshow
method draws the tendril using only the two Processing functionsline
andstroke
. In drawing the tendril, we will wiggle the direction with a random walk (similar to the Lightning assignment) for a life-like appearance. Here's one way to randomly wiggle the line:- declare
startX
and initialize it tomyX
- declare
startY
and initialize it tomyY
- repeat
myNumSegments
times- Change
myAngle
by adding a random decimal from -0.2 to 0.2 - declare
endX
and initialize it tostartX
plus the cosine ofmyAngle
times the segment length - declare
endY
and initialize it tostartY
plus the sine ofmyAngle
times the segment length - draw a line from (
startX
,startY
) to (endX
,endY
) - Set
startX
toendX
- Set
startY
toendY
- Change
- declare
-
Run your program. You should see one random tendril every time you click the screen
-
Now go back to the
Cluster
constructor and modify it to make 7 Tendrils. You may want to evenly space each tendril's angle so that is a multiple of 2π/7. -
In the Tendril
show()
add code that recursively creates a newCluster
at the end of each Tendril ifmyNumSegments
is greater than or equal to the minimum tendril length (3 is a good choice) -
Add code at the beginning of the tendril
show()
function to change thestroke()
based on the length of the tendril.
The instructions above are only a suggestion. Have fun and be creative. Your Tendron doesn't have to look like any other.
Note: this assignment was adapted from Roger Frank's APCS assignment