render_fractal_tree.py 1.2 KB

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