recursion problem...

Thread Solved

Join Date: May 2006
Posts: 17
Reputation: jaden403 is an unknown quantity at this point 
Solved Threads: 0
jaden403 jaden403 is offline Offline
Newbie Poster

recursion problem...

 
0
  #1
Jan 25th, 2007
I am trying to write a recursive function that will produce a fractal snowflake, but am not getting the correct results. For some reason it gets caught in an infinte loop, but I cannot figure out why. Any help is appreciated.

private void recursiveDraw(double x1, double y1, double x2, double y2, int orderNum, Graphics gr) {
 
if (orderNum == 0) {
gr.drawLine((int) x1, (int) y1, (int) x2, (int) y2);
} else {
 
double x4 = x1 * 2 / 3 + x2 * 1 / 3;
double y4 = y1 * 2 / 3 + y2 * 1 / 3;
double x5 = x1 * 1 / 3 + x2 * 2 / 3;
double y5 = y1 * 1 / 3 + y2 * 2 / 3;
double x6 = (x4 + x5) / 2 + (y4 - y5) * sqrt(3) / 2;
double y6 = (y4 + y5) / 2 + (x5 - x4) * sqrt(3) / 2;
 
recursiveDraw(x1, y1, x4, y4, orderNum, gr);
recursiveDraw(x4, y4, x6, y6, orderNum, gr);
recursiveDraw(x6, y6, x5, y5, orderNum, gr);
recursiveDraw(x5, y5, x2, y2, orderNum, gr);
 
}
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: recursion problem...

 
1
  #2
Jan 26th, 2007
where do you change orderNum in order to ever leave the recursion?
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 17
Reputation: jaden403 is an unknown quantity at this point 
Solved Threads: 0
jaden403 jaden403 is offline Offline
Newbie Poster

Re: recursion problem...

 
0
  #3
Jan 26th, 2007
orderNum - 1

I can't believe I didn't catch that. Thanks for your help...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC