Results 1 to 3 of 3
Thread: Need help with recursion
- 04-29-2011, 12:49 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
Need help with recursion
I am trying to draw a fractal tree using turtle graphics
Java Graphics Turtle com.lrdev.turtle
The algorithm is supposed to be:
Draw the trunk
Turn left x degrees
Draw the left branch
recurse ... draw the trunk ...
Backup down the left branch to the trunk
Turn right x+x degrees (to counter the left turn you have to turn double)
Draw the right branch
recurse ... draw the trunk ...
Backup down the right branch to the trunk
Backup down the trunk
The recursive method is supposed to keep track of the recursivedepth
I can't seem to figure out how to implement the algorithm into my code. Please help
Java Code:import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import com.lrdev.turtle.Turtle; public class Tree extends JPanel{ public Tree() { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new JScrollPane(this)); f.setSize(600, 600); f.setLocation(200, 200); f.setVisible(true); } private void drawone (Turtle turtle, int x, int y, int deg, int steps){ turtle.penup(); turtle.setXY(x,y); turtle.pendown(); turtle.left(90); drawtwo(turtle, deg, steps); } // this is the recursive method, I need help writing this private void drawtwo (Turtle turtle, double degx, int stepsx){ if(stepsx == 0) { return; } turtle.forward(50); turtle.left(degx); turtle.forward(50); drawtwo(turtle, degx/1.5, stepsx-1); turtle.back(50); turtle.right(2*degx); turtle.forward(50); turtle.back(50); drawtwo(turtle, degx/1.5, stepsx-1); } protected void paintComponent(Graphics g) { Turtle turtle = new Turtle((Graphics2D) g, getBounds()); turtle.setHeadingMode(Turtle.DEGREE); drawone(turtle, 0, -200, 15, 2); } public static void main(String[] args) { Tree test = new Tree(); } }Last edited by Playboytrey; 04-29-2011 at 01:01 AM.
- 04-29-2011, 01:20 AM #2
Member
- Join Date
- Nov 2010
- Posts
- 44
- Rep Power
- 0
well to keep track you need to hold a reference of some sort between each running of your method. I would pass this as a parameter to the recursive method itself and while the depth doesn't reach the threshold you want let the recursive call continue. Once that point or depth has reached, obviously stop :)
- 04-29-2011, 01:36 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Recursion?
By Fingerz in forum New To JavaReplies: 10Last Post: 01-08-2011, 02:25 AM -
more fun... with recursion
By sonny in forum New To JavaReplies: 19Last Post: 03-23-2010, 05:09 AM -
recursion and tail-recursion differences
By OptimusPrime in forum New To JavaReplies: 2Last Post: 12-28-2009, 06:26 PM -
Recursion
By jachandru in forum New To JavaReplies: 1Last Post: 01-24-2009, 12:52 PM -
Please help with recursion
By pheonix in forum New To JavaReplies: 9Last Post: 12-27-2008, 11:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks