from pyautocad import Autocad, APoint
def draw_tree(acad, base_point, trunk_height, branch_length_factor):
# Draw the trunk
trunk_base = base_point
trunk_top = APoint(base_point.x, base_point.y + trunk_height)
acad.model.AddLine(trunk_base, trunk_top)
# Recursive function to draw branches
def draw_branch(start_point, length, angle, depth):
if depth == 0:
return
# Calculate the end point of the branch
end_point = APoint(
start_point.x + length * math.cos(angle),
start_point.y + length * math.sin(angle)
)
# Draw the branch
acad.model.AddLine(start_point, end_point)
# Draw two smaller branches
# Left branch
draw_branch(end_point, length * branch_length_factor, angle + math.radians(30), depth - 1)
# Right branch
draw_branch(end_point, length * branch_length_factor, angle - math.radians(30), depth - 1)
# Draw branches starting from the trunk top
draw_branch(trunk_top, trunk_height * branch_length_factor, math.radians(90), 3)
if __name__ == '__main__':
import math
# Connect to AutoCAD
acad = Autocad(create_if_not_exists=True)
# Base point for the tree
base_point = APoint(0, 0)
# Parameters
trunk_height = 50 # Length of the trunk
branch_length_factor = 0.7 # To reduce the length of the branches
draw_tree(acad, base_point, trunk_height, branch_length_factor)
This code will draw a simple tree structure with a trunk and recursive branches. The draw_branch function is a recursive function that creates two branches at the end of each branch, reducing the length each time to create a fractal-like appearance.