render_fractal_tree.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Render > Fractal tree
  2. # (fr) Rendu > Arbre fractal
  3. from lazpaint import tools, image, layer, dialog
  4. import math, random
  5. translation = dialog.translate_dict(["Invalid angle", "Vertical size"])
  6. line_buf = []
  7. def line(x, y, x2, y2):
  8. global line_buf
  9. if len(line_buf) > 0 and line_buf[-1] == (x, y):
  10. line_buf.append( (x2, y2) )
  11. else:
  12. flush_line()
  13. line_buf = [(x, y), (x2, y2)]
  14. def flush_line():
  15. global line_buf
  16. if len(line_buf) > 0:
  17. tools.choose(tools.PEN)
  18. tools.mouse(line_buf)
  19. line_buf = []
  20. DEG_TO_RAD = math.pi / 180
  21. ANGLE = abs(dialog.input_value(dialog.translate_text("Angle") + " (< 90)", 45))
  22. if ANGLE >= 90:
  23. dialog.show_message(translation["Invalid angle"])
  24. exit()
  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(translation["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()