fractal_tree.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. DEG_TO_RAD = math.pi / 180
  18. ANGLE = abs(dialog.input_value("Angle (< 90)", 45))
  19. if ANGLE >= 90:
  20. dialog.show_message("Invalid angle")
  21. DEFAULT_SIZE_Y = 7*8/2*2 * (1.14+math.cos(ANGLE * DEG_TO_RAD))/2.14
  22. MULTIPLIER = image.get_height() / DEFAULT_SIZE_Y
  23. ZOOM = dialog.input_value("Vertical size (%)", 95)
  24. MULTIPLIER = MULTIPLIER * ZOOM/100
  25. def drawTree(x1, y1, angle, depth):
  26. if (depth > 0):
  27. x2 = x1 + (math.cos(angle * DEG_TO_RAD) * depth * MULTIPLIER)
  28. y2 = y1 + (math.sin(angle * DEG_TO_RAD) * depth * MULTIPLIER)
  29. line(x1, y1, x2, y2)
  30. drawTree(x2, y2, angle - ANGLE, depth - 2)
  31. drawTree(x2, y2, angle + ANGLE, depth - 2)
  32. image.do_begin()
  33. layer.new()
  34. drawTree(image.get_width() / 2, image.get_height(), -90, 14)
  35. flush_line()
  36. image.do_end()