Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

recursion - sierpinskys triangle - HELP!!

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
 
rob wien
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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..
 
author
Posts: 3281
8
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
rob wien
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3281
8
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic