fractal_tree_random.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from lazpaint import tools, image, layer, dialog
  2. import math, random
  3. line_buf = []
  4. def line(x, y, x2, y2):
  5. global line_buf
  6. if len(line_buf) > 0 and line_buf[-1] == (x, y):
  7. line_buf.append( (x2, y2) )
  8. else:
  9. flush_line()
  10. line_buf = [(x, y), (x2, y2)]
  11. def flush_line():
  12. global line_buf
  13. if len(line_buf) > 0:
  14. tools.choose(tools.PEN)
  15. tools.mouse(line_buf)
  16. line_buf = []
  17. MULTIPLIER = dialog.input_value("Zoom", 10)
  18. DEG_TO_RAD = math.pi / 180
  19. def drawTree(x1, y1, angle, depth):
  20. if (depth > 0):
  21. x2 = x1 + (math.cos(angle * DEG_TO_RAD) * depth * MULTIPLIER)
  22. y2 = y1 + (math.sin(angle * DEG_TO_RAD) * depth * MULTIPLIER)
  23. line(x1, y1, x2, y2)
  24. drawTree(x2, y2, angle - random.randint(15,50), depth - 1.44)
  25. drawTree(x2, y2, angle + random.randint(10,25), depth - 0.72)
  26. drawTree(x2, y2, angle - random.randint(10,25), depth - 3)
  27. drawTree(x2, y2, angle + random.randint(15,50), depth - 4)
  28. image.do_begin()
  29. layer.new()
  30. drawTree(image.get_width() / 2, image.get_height(), -91, 7)
  31. flush_line()
  32. image.do_end()