The Tree Factal
The Tree Fractal is more complicated than the Sierpinski or the H-Tree fractals. You begin by drawing a 'trunk' for the tree. In the next step, you draw 'branches' (which are 'trunks' of smaller trees) (Figure 1). The length of each branch is a proportion of the length of the trunk (like 0.8 * trunklength). The initial trunk is at an angle with the horizontal axis (angle1) and each branch angle differs from the previous trunk angle by a constant amount (angle2). Figure 2 illustrates this. Continue drawing 'trunks' until a specific number of iterations are completed. To get the green 'leaves', draw a small green circle at the last iteration.
To draw the Tree fractal, you need these things:
-
x
,y
coordinates of starting point of branch. For the initial call, this is the base of the trunk. -
trunkAngle
- angle of the trunk from horizontal axis (radians) (angle1 in Figure 2). The trunkAngle for the first call is the angle that the trunk makes with the horizontal axis. Since computer graphics put the origin (0,0) at the upper left corner of the canvas, a vertical trunk has angle -PI/2. (That is: -90 degrees). For subsequent calls, trunkAngle is adjusted by splitAngle. -
splitAngle
- angle to split branches (in radians) (angle2 in Figure 2). If you use PI/2, the branches will angle away at 90 degrees, forming a T. Smaller angles will give a Y-shape. -
length
- initial length of first branch (pixels) -
iterations
- number of iterations deep for recursion. A small value gives you a sparse tree with few branches. Larger values give you a denser tree with more small branches. -
ratio
- relative size of next branch (0 < ratio < 1)
So overall, the signature for your recursive method might look like this:
public void drawTree(int x, int y, double trunkAngle, double splitAngle, int length, int iterations, double ratio)