posted 10 years ago
Not sure if I should post this in the beginner's section but here it goes. I thought this "Koch Curve" would be fun because it looks cool. The problem I'm having is that it is stuck on the first iteration and it won't draw more of those triangles. This is the code I have so far that runs:
I'm having a feeling that I'm missing a parameter for the recursion part of it since it is basically redrawing the same starting triangle over again.
Thanks
I'm having a feeling that I'm missing a parameter for the recursion part of it since it is basically redrawing the same starting triangle over again.
Thanks
posted 10 years ago
Justin,
In recursion, there is always a "base case" and a recursive case that should bring you one step closer to the base case.
In your example, the base case is c != level. The recursive case makes level one smaller and c one bigger each time. Since c is starting out larger than level (c=3, level=0), how does the recursion end.
This forum (intermediate) is fine since your question is about recursion. That's too advanced for beginners. If it was about the graphics themselves, it would go in the applets forum.
In recursion, there is always a "base case" and a recursive case that should bring you one step closer to the base case.
In your example, the base case is c != level. The recursive case makes level one smaller and c one bigger each time. Since c is starting out larger than level (c=3, level=0), how does the recursion end.
This forum (intermediate) is fine since your question is about recursion. That's too advanced for beginners. If it was about the graphics themselves, it would go in the applets forum.
[OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Tom Cameron
Ranch Hand
Posts: 33
posted 10 years ago
Thanks Jeanne for a fast reply. I think I fixed my base case now with this new code:
My main problem is that I can't get my program to draw the next "set of triangles" on the next "level", i.e. 2 smaller triangles on the bigger triangle and 1 triangle on each end of the line ( a total of 5 triangles). instead, I'm stuck with 1 triangle the whole time, so I figure I'm doing my recursion wrong somehow.
My main problem is that I can't get my program to draw the next "set of triangles" on the next "level", i.e. 2 smaller triangles on the bigger triangle and 1 triangle on each end of the line ( a total of 5 triangles). instead, I'm stuck with 1 triangle the whole time, so I figure I'm doing my recursion wrong somehow.
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJIDEA
