def draw_stars(): t = Turtle(center_origin=True, show_borders_and_origin=False, animate=False, animation_speed=0.1) def draw_n_star(t, star_points, triangle_side=200, line_width=1): """ Draws star star_points: number of star point triangle_side: line length from point ot point """ n = star_points if n % 2 == 0: n += 1 length = triangle_side # move to position t.penup() t.left(90) t.forward(length / 2) t.pendown() for i in range(1, n + 1): t.left(180 - (360 / n / 2)) t.forward(length, width=line_width) draw_n_star(t, 30, triangle_side=400) t.save("img/star.svg")
def draw_polygons(): t = Turtle(center_origin=True, show_borders_and_origin=False, animate=False, animation_speed=0.05) def draw_n_polygon(t, polygon_sides, triangle_side=100, line_width=1): """ Draws polygon polygon_sides: number of sides of the polygon triangle_side: length from the center of polygon to the endge """ center_triangle_angle = 360 / polygon_sides point_angle = (180 - center_triangle_angle) / 2 for i in range(1, polygon_sides + 1): t.penup() t.forward(triangle_side, width=line_width) t.right(180 - point_angle) half = math.sin(math.radians( center_triangle_angle / 2)) * triangle_side t.pendown() t.forward(half * 2, width=line_width) t.right(180 - point_angle) t.penup() t.forward(triangle_side, width=line_width) # get to right orientation t.angle += 180 for i in range(3, 26): draw_n_polygon(t, i, triangle_side=20 * i, line_width=i - 2) t.save("img/3_to_25_polygons.svg")
def tree(): """ Draws tree fractal TODO: - randomize number of branches, angles and lengths """ t = Turtle(width=800, height=800, center_origin=True, show_borders_and_origin=False, animate=True, animation_speed=0.005) t.set_y(-400) length = 200 branch_angle = 20 branch_scale = 0.7 steps = 10 t.left(90) # starting position t.forward(length, width=steps / 2) def draw_branch(length, direction, steps): if steps == 0: return if direction == "left": t.left(branch_angle) else: t.right(branch_angle) t.forward(length, width=steps / 3) draw_branch(length * branch_scale, "left", steps - 1) draw_branch(length * branch_scale, "right", steps - 1) t.penup() t.back(length, width=3) if direction == "left": t.right(branch_angle) else: t.left(branch_angle) t.pendown() draw_branch(length * branch_scale, "left", steps) draw_branch(length * branch_scale, "right", steps) t.save("img/tree_fractal_animated.svg")
def draw_sierpinski_triange(): turtle = Turtle(width=1100, height=1000, center_origin=True, show_borders_and_origin=False, animate=False, animation_speed=0.001) axiom = "B" rules = {"A": "B+A+B", "B": "A-B-A"} interpretation = {"A": ("forward", 4), "B": ("forward", 4), "+": ("right", 60), "-": ("left", 60)} depth = 8 turtle.left(180) turtle.set_x(500) turtle.set_y(-500) l_system_draw(turtle, "img/sierpinski_triangle.svg", axiom, rules, interpretation, depth)
def draw_hilbert_curve(): turtle = Turtle(width=1000, height=1000, center_origin=True, show_borders_and_origin=False, animate=False, animation_speed=0.001) axiom = "-L" rules = {"L": "LF+RFR+FL-F-LFLFL-FRFR+", "R": "-LFLF+RFRFR+F+RF-LFL-FR"} length = 10 interpretation = {"F": ("forward", length), "+": ("right", 90), "-": ("left", 90)} depth = 4 turtle.left(90) turtle.set_x(400) turtle.set_y(-400) l_system_draw(turtle, "img/hilbert_curve.svg", axiom, rules, interpretation, depth)
def pentagram_relative(): t = Turtle(center_origin=True, show_borders_and_origin=False, animate=True, animation_speed=0.5) def draw_polygon_pentagram_relative(t, line_width=1): n = 5 polygon_side = 200 center_angle = 360 / n triangle_angle = (180 - center_angle) / 2 turn_angle = 180 - triangle_angle - 90 # move to starting position x = math.cos(math.radians(turn_angle)) * polygon_side t.penup() t.forward(x) t.left(180 - turn_angle) t.pendown() # draw polygon for _ in range(n): t.forward(polygon_side, width=line_width) t.left(2 * turn_angle) # move to position for pentagram t.reset_angle() t.left(180) # draw pentagram pentagram_polygon_side = 2 * x for _ in range(n): t.forward(pentagram_polygon_side, width=line_width) t.left(180 - center_angle / 2) t.reset() draw_polygon_pentagram_relative(t, 5) t.save("img/pentagram_relative_animated.svg")
def draw_koch_snowflake(): turtle = Turtle(width=1000, height=1000, center_origin=False, show_borders_and_origin=False, animate=False, animation_speed=0.001) axiom = "F--F--F" rules = {"F": "F+F--F+F"} interpretation = {"F": ("forward", 1), "+": ("right", 60), "-": ("left", 60)} depth = 6 turtle.set_x(150) turtle.set_y(300) l_system_draw(turtle, "img/koch_snowflake.svg", axiom, rules, interpretation, depth)
def draw_fancy_tree_3_stochastic(): turtle = Turtle(width=800, height=1000, center_origin=True, show_borders_and_origin=False, animate=False, animation_speed=0.001) axiom = "F" rules = {"F": ["[+F]F[−F]", "F[+F]F", "F[-F]F"]} length = 5 angle = [25, 30, 35] interpretation = {"F": ("forward", length), "[": ("stack", "push"), "]": ("stack", "pop"), "+": ("right", angle), "-": ("left", angle)} depth = 9 turtle.left(90) turtle.set_y(-500) l_system_draw(turtle, "img/fancy_tree_3_stochastic.svg", axiom, rules, interpretation, depth, debug=True)
def draw_fancy_tree_2(): turtle = Turtle(width=700, height=1000, center_origin=True, show_borders_and_origin=False, animate=False, animation_speed=0.001) axiom = "F" rules = {"F": "F[+FF]F[-FF]F"} length = 3 angle = 25.7 interpretation = {"F": ("forward", length), "[": ("stack", "push"), "]": ("stack", "pop"), "+": ("right", angle), "-": ("left", angle)} depth = 5 turtle.left(90) turtle.set_y(-500) l_system_draw(turtle, "img/fancy_tree_2.svg", axiom, rules, interpretation, depth)
def draw_basic_tree(): turtle = Turtle(width=600, height=600, center_origin=True, show_borders_and_origin=False, animate=False, animation_speed=0.001) axiom = "A" rules = {"A": "F[+A]-A", "F": "FF"} length = 1 angle = 30 interpretation = {"F": ("forward", length), "[": ("stack", "push"), "]": ("stack", "pop"), "+": ("right", angle), "-": ("left", angle)} depth = 9 turtle.left(90) turtle.set_y(-300) l_system_draw(turtle, "img/basic_tree.svg", axiom, rules, interpretation, depth)
def snowflake(): """ Draws snowflake fractal TODO: is broken """ t = Turtle(width=1000, height=1000, center_origin=True, show_borders_and_origin=False, animate=False, animation_speed=0.01) n = 5 pentagon_side = 20 center_angle = 360 / n triangle_angle = (180 - center_angle) / 2 turn_angle = 2 * (180 - triangle_angle - 90) def draw_snowflake(t, pentagon_side): #t.set_x(x) #t.set_y(y) def draw_pentagon(t, pentagon_side): for _ in range(n): t.left(turn_angle) t.forward(pentagon_side) t.back(pentagon_side) t.left(turn_angle) for _ in range(5): draw_pentagon(t, pentagon_side) print(turn_angle / 2) MAGIC = 4.2 for i in range(1, 2 * n, 2): draw_snowflake(t, pentagon_side) t.left(i / 2 * turn_angle) # level to 0 t.back(MAGIC * pentagon_side, width=3) t.reset_angle() for i in range(1, 2 * n, 2): draw_snowflake(t, pentagon_side) t.left(i / 2 * turn_angle) # level to 0 t.back(MAGIC * pentagon_side, width=3) t.reset_angle() t.save("img/snowflake_fractal.svg")