# Draw tree, clouds, leaves with recursion

• November 16th, 2013, 02:14 PM
BiggerBore
Draw tree, clouds, leaves with recursion
I am trying to use my random method to change how draw tree works but cannot figure it out. This is what i have so far.

Use pickRandom() to effect how drawTree() works. The number of branches, the angles of the branches, and the lengths of the branches will be varied randomly at each level of recursion. In my program, I varied the number of branches between 2 and 6; I varied the angles between -40 and +40 degrees from that of the parent branch; and I varied the length of a branch between 1/10 and 9/10 of that of the parent branch.

Code :

```//Program to create a recursive tree. //This is the base program to be used to start the cs258 project on recursion.   import java.applet.Applet; import java.awt.*; import java.util.Random;   public class Tree extends Applet { /** * */ private static final long serialVersionUID = -2796931303737879571L; private final int APPLET_WIDTH = 320; private final int APPLET_HEIGHT = 320; private final double STARTSIZE = 110.0; private final double STARTANGLE = 180.0; private final double CHANGEANGLE = 30.0; private final double FACTOR = 2.0; private final double MINSIZE = 10.0;   Color skyblue = new Color(31, 200, 226); Color grassgreen = new Color(9, 97, 47); Color treebark = new Color(77, 49, 25);   int r; int maxclouds = 30;   //Initialize the applet.   public void init() { setSize(APPLET_WIDTH, APPLET_HEIGHT); setBackground(skyblue);     } private void setColor(Color green) { // setColor(Color green);     }   public void paint(Graphics page) { Graphics2D ga = (Graphics2D) page; ga.setColor(grassgreen); ga.fillRect(0, APPLET_WIDTH-APPLET_WIDTH/4, APPLET_WIDTH, APPLET_HEIGHT/4 ); drawTree(page, APPLET_WIDTH/2, APPLET_HEIGHT, STARTSIZE, STARTANGLE); //drawCloud(page, 20, 20, 20, 20, 1);     }   public void drawTree( Graphics page, int x, int y, double size, double angle ) { page.setColor(treebark); Point endPoint = calculatePoint(x, y, size, angle ); page.drawLine(x, y, endPoint.x, endPoint.y);   if (size > MINSIZE) { drawTree(page, endPoint.x, endPoint.y , size/FACTOR, angle+CHANGEANGLE); drawTree(page, endPoint.x, endPoint.y , size/FACTOR, angle-CHANGEANGLE);     } }   public void drawGrass(Graphics page, int x, int y, double height, double width) { page.setColor(grassgreen); page.fillRect(APPLET_WIDTH, APPLET_HEIGHT, -320, -90); }   public void drawCloud (Graphics page, int x, int y, int w, int h, int order) { page.setColor(Color.white); page.fillOval(10, 10, 20, 30); for(int i = 0; i <30; i++) {   page.fillOval(pickRandom(0, 80),pickRandom(0, 10),pickRandom(1,3), pickRandom(1, 5)); }       }   public Point calculatePoint( int x, int y, double size, double degree ) { Point point = new Point(x, y); double radians = Math.PI/180. * degree;   point.x += (int)(size * Math.sin(radians)); point.y += (int)(size * Math.cos(radians)); return point; }   public int pickRandom(int min, int max)   {       return (int)Math.random()*(min-max+1) + min; }}```
• November 16th, 2013, 02:21 PM
Norm
Re: Draw tree, clouds, leaves with recursion
Can you explain what problems you are having? What does the code do when it is executed? What needs to be changed?
• November 16th, 2013, 02:26 PM
BiggerBore
Re: Draw tree, clouds, leaves with recursion
Use pickRandom() to effect how drawTree() works. The number of branches, the angles of the branches, and the lengths of the branches will be varied randomly at each level of recursion. In my program, I varied the number of branches between 2 and 6; I varied the angles between -40 and +40 degrees from that of the parent branch; and I varied the length of a branch between 1/10 and 9/10 of that of the parent branch.

i dont know how to do this
• November 16th, 2013, 02:35 PM
GregBrannon
Re: Draw tree, clouds, leaves with recursion
Confused. Your post says that you did all those things but then you add that you don't know how to do "this". What in that list of things do you specifically NOT know how to do?
• November 16th, 2013, 02:39 PM
Norm
Re: Draw tree, clouds, leaves with recursion
Make a list of the changes you want to make.
Find in the code where the item you want to change is being drawn.
Work on some logic to change how it is being drawn.
An item: the number of branches
What controls the number of branches? How can that logic be changed to use a random number?