943,755 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1899
  • Python RSS
Sep 28th, 2009
0

[Help] Koch snowflake from Python 2.5 [Turtle]

Expand Post »
Hey I'm very new to the programming world, and what i am trying to do is produce a Koch Snowflake via turtle from python2.5.
this is what I have so far...
Python Syntax (Toggle Plain Text)
  1. from turtle import *
  2.  
  3. def snowflake(lengthSide, levels):
  4. if levels == 0:
  5. return forward(lengthSide), right(120), forward(lengthSide), right(120), forward(lengthSide)
  6.  
  7. forward(lengthSide)
  8. left(60)
  9. forward(lengthSide)
  10. right(60)
  11. right(60)
  12. forward(lengthSide)
  13. left(60)
  14. forward(lengthSide)
  15. right(120)
  16. snowflake(lengthSide, levels)
as you can see i've only solved for levels 0 and 1... the problem is i dont understand how to recursively redraw it from a certain location. If anyone has any ideas or tip, it would be helpful.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Oldguy1 is offline Offline
2 posts
since Sep 2009
Sep 28th, 2009
0

Re: [Help] Koch snowflake from Python 2.5 [Turtle]

I suggest this
python Syntax (Toggle Plain Text)
  1. from turtle import *
  2.  
  3. def snowflake(lengthSide, levels):
  4. if levels == 0:
  5. forward(lengthSide)
  6. return
  7. lengthSide /= 3.0
  8. snowflake(lengthSide, levels-1)
  9. left(60)
  10. snowflake(lengthSide, levels-1)
  11. right(120)
  12. snowflake(lengthSide, levels-1)
  13. left(60)
  14. snowflake(lengthSide, levels-1)
  15.  
  16.  
  17. if __name__ == "__main__":
  18. speed(0)
  19. length = 300.0
  20. penup()
  21. backward(length/2.0)
  22. pendown()
  23. snowflake(length, 4)
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Sep 28th, 2009
0

Re: [Help] Koch snowflake from Python 2.5 [Turtle]

Thank you very much, this helped alot!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Oldguy1 is offline Offline
2 posts
since Sep 2009
Dec 1st, 2010
0

snowflake program

how do you modify the code so that it can make a full snowflake?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ohernandezmit is offline Offline
3 posts
since Dec 2010
Dec 2nd, 2010
0
Re: [Help] Koch snowflake from Python 2.5 [Turtle]
how do you modify the code so that it can make a full snowflake?
Simply define
python Syntax (Toggle Plain Text)
  1. def full_snowflake(lengthside, levels):
  2. for i in range(3):
  3. snowflake(lengthside, levels)
  4. right(120)
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Dec 3rd, 2010
0
Re: [Help] Koch snowflake from Python 2.5 [Turtle]
And put at the end of program:
Python Syntax (Toggle Plain Text)
  1. mainloop()
Featured Poster
Reputation Points: 687
Solved Threads: 748
Industrious Poster
pyTony is offline Offline
4,202 posts
since Apr 2010
Dec 3rd, 2010
0
Re: [Help] Koch snowflake from Python 2.5 [Turtle]
where do you put the code in the program? I've tried to put it at the end but it doesn't work.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ohernandezmit is offline Offline
3 posts
since Dec 2010
Dec 3rd, 2010
0
Re: [Help] Koch snowflake from Python 2.5 [Turtle]
Python Syntax (Toggle Plain Text)
  1. from turtle import *
  2.  
  3. def snowflake(lengthSide, levels):
  4. if levels == 0:
  5. forward(lengthSide)
  6. return
  7. lengthSide /= 3.0
  8. snowflake(lengthSide, levels-1)
  9. left(60)
  10. snowflake(lengthSide, levels-1)
  11. right(120)
  12. snowflake(lengthSide, levels-1)
  13. left(60)
  14. snowflake(lengthSide, levels-1)
  15.  
  16. def full_snowflake(lengthside, levels):
  17. for i in range(3):
  18. snowflake(lengthside, levels)
  19. right(120)
  20.  
  21. if __name__ == "__main__":
  22. speed(0)
  23. length = 300.0
  24. penup()
  25. backward(length/2.0)
  26. pendown()
  27. full_snowflake(length, 4)
  28. mainloop()
Featured Poster
Reputation Points: 687
Solved Threads: 748
Industrious Poster
pyTony is offline Offline
4,202 posts
since Apr 2010
Dec 4th, 2010
0

A writer's snowflake.

Here is a snowflakes containing printable characters
python Syntax (Toggle Plain Text)
  1. from turtle import *
  2. import random, string
  3.  
  4. fact = (2.0/(3.0*sqrt(3.0)), sqrt(3.0)/2.0, 0.5)
  5.  
  6. def writers(cx, cy, lengthside, levels):
  7. if levels == 0:
  8. penup()
  9. goto(cx, cy)
  10. pendown()
  11. write(random.choice(string.printable))
  12. return
  13. r = lengthside * fact[0]
  14. a = r * fact[1]
  15. b = r * fact[2]
  16. centers = [(cx, cy), (cx, cy+r), (cx, cy-r), (cx+a, cy+b), (cx+a, cy-b), (cx-a, cy+b), (cx-a, cy-b)]
  17. for ux, uy in centers:
  18. writers(ux, uy, lengthside/3.0, levels-1)
  19.  
  20. if __name__ == "__main__":
  21. speed(0)
  22. length = 500.0
  23. writers(0.0, 0.0, length, 4)
  24. mainloop()
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Dec 4th, 2010
0
Re: [Help] Koch snowflake from Python 2.5 [Turtle]
Thank you. That really helped!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ohernandezmit is offline Offline
3 posts
since Dec 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Need help with knapsack style problem please. Thanks
Next Thread in Python Forum Timeline: Help with deck of cards





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC