render_fractal_tree.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Render > Fractal tree
  2. # (fr) Rendu > Arbre fractal
  3. # (es) Renderizar > Árbol fractal
  4. # (de) Rendering > Fraktaler Baum
  5. from lazpaint import tools, image, layer, dialog
  6. import math, random
  7. line_buf = []
  8. def line(x, y, x2, y2):
  9. global line_buf
  10. if len(line_buf) > 0 and line_buf[-1] == (x, y):
  11. line_buf.append( (x2, y2) )
  12. else:
  13. flush_line()
  14. line_buf = [(x, y), (x2, y2)]
  15. def flush_line():
  16. global line_buf
  17. if len(line_buf) > 0:
  18. tools.choose(tools.PEN)
  19. tools.mouse(line_buf)
  20. line_buf = []
  21. DEG_TO_RAD = math.pi / 180
  22. ANGLE = abs(dialog.input_value("Angle (< 90)", 45))
  23. if ANGLE >= 90:
  24. dialog.show_message("Invalid angle")
  25. DEFAULT_SIZE_Y = 7*8/2*2 * (1.14+math.cos(ANGLE * DEG_TO_RAD))/2.14
  26. MULTIPLIER = image.get_height() / DEFAULT_SIZE_Y
  27. ZOOM = dialog.input_value("Vertical size (%)", 95)
  28. MULTIPLIER = MULTIPLIER * ZOOM/100
  29. def drawTree(x1, y1, angle, depth):
  30. if (depth > 0):
  31. x2 = x1 + (math.cos(angle * DEG_TO_RAD) * depth * MULTIPLIER)
  32. y2 = y1 + (math.sin(angle * DEG_TO_RAD) * depth * MULTIPLIER)
  33. line(x1, y1, x2, y2)
  34. drawTree(x2, y2, angle - ANGLE, depth - 2)
  35. drawTree(x2, y2, angle + ANGLE, depth - 2)
  36. image.do_begin()
  37. layer.new()
  38. drawTree(image.get_width() / 2, image.get_height(), -90, 14)
  39. flush_line()
  40. image.do_end()