Using Recursion to Draw Fractal Trees With Landscape and Clouds
#1
-
- D.I.C Head
Reputation: 2
- Posts: 53
- Joined: 05-May 10
Using recursion to draw fractal trees with landscape and clouds
Posted 13 May 2011 - 02:42 PM
First off here is what I need to do:
1)widen the branches of the tree
2)vary the number of branches each tree has
3)vary the angles
4)create clouds using recursion
4)make the trees wider (using fillPolygon() instead of drawLine())
5)draw leaves on the trees
Now I have a stick figure tree drawn but I need to figure out how to get that tree to be put in a random place using my pickRandom() method then I need to make the trees random themselves.
If anyone has suggestions as to what I could do to accomplish what I need to that would be great.
Thanks,
Here is my code thus far:
import java.applet.Applet; import java.awt.*; import java.awt.Color; import java.util.Random; public class Tree extends Applet { 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 sky = new Color(52, 232, 230); Random r = new Random(); // Initialize the applet. public void init() { setSize(APPLET_WIDTH, APPLET_HEIGHT); setBackground(sky); } // Create the drawing that displays on the applet. public void paint(Graphics page) { drawGrass(page, APPLET_WIDTH, APPLET_HEIGHT, 160, 320); drawTree(page, APPLET_WIDTH/2, APPLET_HEIGHT, STARTSIZE, STARTANGLE); } public void drawGrass(Graphics page, int x, int y, double height, double width) { Color green= new Color(0,255,0); page.setColor(green); page.fillRect(APPLET_WIDTH, APPLET_HEIGHT, -320, -90); } public void drawTree( Graphics page, int x, int y, double size, double angle ) { Color brown= new Color(125,0,0); page.setColor(brown); 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 int pickRandom(int min, int max) { return (int)Math.random()*(min-max+1) + min; } 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; } } When done my applet should look something similar to the image in the attached file.(if it resembles this at all ill be happy)
Attached image(s)
Is This A Good Question/Topic? 0
#2 macosxnerd101
Reputation: 12800
- Posts: 45,992
- Joined: 27-December 08
Re: Using recursion to draw fractal trees with landscape and clouds
Posted 13 May 2011 - 03:04 PM
The pickRandom() method only returns a single point. So perhaps invoke it twice, and use those values as the starting points (rather than the Applet width and height) for the root of the Tree. And just use those numbers when drawing out your fractal.
#3 johnathanc456
-
- D.I.C Head
Reputation: 2
- Posts: 53
- Joined: 05-May 10
Re: Using recursion to draw fractal trees with landscape and clouds
Posted 14 May 2011 - 01:58 PM
So in my case the paint method would change so that instead of
drawTree(page, APPLET_WIDTH/2, APPLET_HEIGHT, STARTSIZE, STARTANGLE);
it would be re written as
drawTree(page, pickRandom(100, 300), pickRandom(250, 320), STARTSIZE, STARTANGLE);
re setting the start points for the tree to two randomly selected points.
Im still a little confused about setting up the fillPolygon() instead of using drawLine() when im drawing the tree, to me it would seem easier to just use the fillRect() instead, is there a reason why i would need to use fillPolygon()?
Using Recursion to Draw Fractal Trees With Landscape and Clouds
Source: https://www.dreamincode.net/forums/topic/231922-using-recursion-to-draw-fractal-trees-with-landscape-and-clouds/
0 Response to "Using Recursion to Draw Fractal Trees With Landscape and Clouds"
Post a Comment