JavaRanch » Java Forums »
Java »
Java in General
| Author |
recursion - sierpinskys triangle - HELP!!
|
rob wien
Greenhorn
Joined: Dec 08, 2008
Posts: 8
|
|
hi! i have to write a gui window, which draws sierpinskys triangle recursively, i have one main class: AND MORE IMPORTANT: the triangle class with the recursion THE PROBLEM: as you see, in the whle loop i commented two of the recursive calls out, if I just have one of the 3 calls (no matter which one of the three), it works, the triangles in that direction are drawn. but if I have two or three of the recursive call, it does not work, it's not drawing the triangles i want. the three recursive calls seem to affect each other in a way i don't want. i just don't get each of the recursive drawing just the triangles they do draw, when i just have the one recursive call in the while loop. i thought it should work, as i have separated, always newly defined points for A1, B1, C1 and so on... maybe i am misunderstanding how the java stack recursion works, can anyone maybe test my code and give any help?? would really be cool, thanks a lot btw: i know that the condition in the while loop is not the right recirsive base condition, but i didn't think about the right one yet and just used it like it is there for testing, i want to fix that if my method principally works correct
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4167
|
|
Just another cross poster. And an ambitious one, at that. http://www.java-forums.org/threads-synchronization/14036-recursion-sierpinskys-triangle-help.html http://forums.sun.com/thread.jspa?threadID=5353545 http://forums.java.net/jive/thread.jspa?messageID=320623 http://www.daniweb.com/forums/post752824.html
|
luck, db
There are no new questions, but there may be new answers.
|
 |
rob wien
Greenhorn
Joined: Dec 08, 2008
Posts: 8
|
|
@db: what's your/the problem with that? sorry, i didn't know, that it's not ok, to post on several forums! i thought posting in different categories in one forum is not ok, but why not in different forums?? i do not mean to spam or something, i am just looking for help!! are there the same people watching all java-forums anyway?? in this case, i can of course just post in one forum. i do not understand, why using different forums parallel is not liked! maybe someone can explain that to me! and besides, i'd still be happy for just some help with my problem..
|
 |
Martijn Verburg
author
Bartender
Joined: Jun 24, 2003
Posts: 3268
|
|
Hi Rob and welcome to Javaranch, As Darryl alludes to, please BeForthrightWhenCrossPostingToOtherSites Onto your question... Here's my hint to help you track this down. Output the variables everytime the recursive loop is called, can you spot what's happening?
|
Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
|
 |
rob wien
Greenhorn
Joined: Dec 08, 2008
Posts: 8
|
|
I added some print-statements as suggested, here's the changed class: the prints start with: then the following print comes very often with the same values: and then i get alternating prints for the recursive calls 2 and 3 with changing values like: I am now even more confused, about this order in which the recursive calls happen and have no clue about what and where i coded wrong...
|
 |
Martijn Verburg
author
Bartender
Joined: Jun 24, 2003
Posts: 3268
|
|
OK, this is a good step forward! Now think about: 1.) The _scope_ of the variables that you have declared 1.b) When you change their value 1.c) What you chagne that value to The print outs that you now have will help you see this. E.g. Are the variables going to get overridden each time the recursive call is made? Or are they Kept intact? Do they need to be reset? If there is too much "noise" start by commenting out 2 of the variables so you're only dealing with one point of the triangle, you should see a pattern emerge...
|
 |
rob wien
Greenhorn
Joined: Dec 08, 2008
Posts: 8
|
|
the prints are still hard to interpret for me! i don't get, why recursive call1 is executed so often before then call2 and call3 are coming alternately... the scope of my A1, B1, C1 is class-wide, as I declared them as fields.. but as I asign new Point objects to them in every execution of the method, thats probably not necessarry. the mentioned declarations of new points to the labels A1, A2 ,A3 are the only places I change values or not? thats what I want, but i am not sure, if i'm right here..! I thought, that if I just make new points in every call, with adjusting the parameter points to points for the next 3 smaller triangles, i can just call the method again 3 times with my new points and in that recursive call new points are created for the next 3 triangles... I now tried to define my points A1, B1, C1... within the method, it actually does change the gui pic and the print methods (but not correctly again), which i don't understand, because, I define new points, whenever the method is calles, why does it matter, if I declare the labels inside or outside the method, because, if I assign new points to the labels anyway, the scope should not matter anymore (obviously it does!) my main lack of understanding is, why a single recursive call in the while loop works correctly for the triangles it has to draw, but calling all 3 makes crap for all of them... huh, hard to keep overview all the relations here...
|
 |
 |
|
|
subject: recursion - sierpinskys triangle - HELP!!
|
|
|
|