furas

Python - turtle - Barnsley Fern

Jul 3rd, 2018
143
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # Barnsley Fern
  3. #
  4. # https://en.wikipedia.org/wiki/Barnsley_fern
  5. #
  6.  
  7. import random
  8. import turtle
  9.  
  10. turtle.setup(850, 850)
  11. turtle.penup()
  12. turtle.speed(0)
  13. turtle.tracer(0, 0)
  14.  
  15. x = 0
  16. y = 0
  17.  
  18. for i in range(1, 16000):
  19.     r = random.uniform(0, 1)
  20.        
  21.     if r < 0.1:
  22.         temp_x = 0
  23.         temp_y = 0.16 * y
  24.         x = temp_x
  25.         y = temp_y
  26.     elif r < 0.80:
  27.         temp_x =  0.85 * x + 0.04 * y
  28.         temp_y = -0.04 * x + 0.85 * y + 1.6
  29.         x = temp_x
  30.         y = temp_y
  31.     elif r < 0.93:
  32.         temp_x =  0.20 * x + -0.26 * y
  33.         temp_y =  0.23 * x +  0.22 * y + 1.6
  34.         x = temp_x
  35.         y = temp_y
  36.     else:
  37.         temp_x = -0.15 * x + 0.28 * y
  38.         temp_y =  0.26 * x + 0.24 * y + 0.44
  39.         x = temp_x
  40.         y = temp_y
  41.    
  42.     turtle.goto(x*80, y*80 - 400) # move down -400 pixels
  43.     turtle.dot(2, 'green')
  44.  
  45. turtle.hideturtle()
  46. turtle.update() # need to run hideturtle()
  47. turtle.mainloop()
RAW Paste Data