CSC212 Lab 11 Solution Program 2014
--D. Thiebaut (talk) 16:55, 30 November 2014 (EST)
import java.util.LinkedList;
import java.util.Queue;
import processing.core.PApplet;
public class FractalTree extends PApplet {
static int MAXWIDTH = 800;
static int MAXHEIGHT= 800;
Queue<Float> orangeCoordinates;
private void draw_tree(int order, // the level of recursion. Starts
// positive.
float theta, // angle of new branch leaving this trunk
float d, // size of this branch
float x, float y, // coordinates of base of this branch
float heading) { // angle of direction of this branch
if ( order <= 0 )
return;
float trunk_ratio = 0.4f; // How big is the trunk relative to whole
// tree?
float trunk = d * trunk_ratio; // length of trunk
// compute x, y of end of the current branch
float delta_x = (float) (trunk * Math.cos( heading ));
float delta_y = (float) (trunk * Math.sin( heading ));
float x2 = x + delta_x;
float y2 = y + delta_y;
// draw current branch
fill( 153, 76, 0 );
strokeWeight( 2 ); //order+2 );
line( x, y, x2, y2 );
// draw oranges at level 8
if ( order==6 ) {
//fill(204, 102, 0);
//strokeWeight( 1 );
//ellipse( x2, y2, 20, 20 );
orangeCoordinates.add( x2 );
orangeCoordinates.add( y2 );
}
// make the recursive calls to draw the two subtrees
float newsz = d * (1 - trunk_ratio);
draw_tree(order - 1, theta, newsz, x2, y2, heading - theta);
draw_tree(order - 1, theta, newsz, x2, y2, heading + theta);
}
public void setup() {
// define the size of the applet
size( MAXWIDTH, MAXHEIGHT );
// make the graphic shapes smooth
smooth();
// make the background white
background( 255, 255, 255 );
// set the angle of branches departing from trunk
float theta = 0.68f; // use 0.02 for tall skinny trees, 0.7 for fat trees
orangeCoordinates = new LinkedList<Float>();
// draw the tree recursively
draw_tree( 11, theta, MAXWIDTH * 0.7f, (float) MAXWIDTH/2,
(float) MAXHEIGHT-50, - (float) (Math.PI/2) );
while( ! orangeCoordinates.isEmpty() ) {
float x2 = orangeCoordinates.poll( );
float y2 = orangeCoordinates.poll( );
fill(204, 102, 0);
strokeWeight( 1 );
ellipse( x2, y2, 20, 20 );
}
fill( 255, 0, 255 );
text( "Mickey Mouse", MAXWIDTH/2+10, MAXHEIGHT-50 );
}
public void draw() {
// nothing to do iteratively;
}
}