Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- depth = 3
- def draw_a_line(given_turtle=None, start_x=0, start_y=0, end_x=0, end_y=0):
- if given_turtle is None:
- given_turtle = turtle.Turtle()
- given_turtle.penup()
- given_turtle.setposition(start_x, start_y)
- given_turtle.pendown()
- given_turtle.setposition(end_x, end_y)
- def draw_a_triangle(given_turtle, x1, y1, x2, y2, x3, y3):
- draw_a_line(given_turtle, x1, y1, x2, y2)
- draw_a_line(given_turtle, x1, y1, x3, y3)
- draw_a_line(given_turtle, x2, y2, x3, y3)
- def draw_sub_triangle(given_turtle, n, x1, y1, x2, y2, x3, y3):
- draw_a_triangle(given_turtle, x1, y1, x2, y2, x3, y3)
- if n < depth:
- n += 1
- a = (x1 + x2) / 2 + (x2 - x3) / 2
- b = (y1 + y2) / 2 + (y2 - y3) / 2
- c = (x1 + x2) / 2 + (x1 - x3) / 2
- d = (y1 + y2) / 2 + (y1 - y3) / 2
- e = (x1 + x2) / 2
- f = (y1 + y2) / 2
- draw_sub_triangle(given_turtle, n, a, b, c, d, e, f)
- a = (x3 + x2) / 2 + (x2 - x1) / 2
- b = (y3 + y2) / 2 + (y2 - y1) / 2
- c = (x3 + x2) / 2 + (x3 - x1) / 2
- d = (y3 + y2) / 2 + (y3 - y1) / 2
- e = (x3 + x2) / 2
- f = (y3 + y2) / 2
- draw_sub_triangle(given_turtle, n, a, b, c, d, e, f)
- a = (x1 + x3) / 2 + (x3 - x2) / 2
- b = (y1 + y3) / 2 + (y3 - y2) / 2
- c = (x1 + x3) / 2 + (x1 - x2) / 2
- d = (y1 + y3) / 2 + (y1 - y2) / 2
- e = (x1 + x3) / 2
- f = (y1 + y3) / 2
- draw_sub_triangle(given_turtle, n, a, b, c, d, e, f)
- window = turtle.Screen()
- window.bgcolor("black")
- (w, h) = window.screensize()
- brad = turtle.Turtle()
- brad.setheading(0)
- brad.shape("classic")
- brad.color("white")
- brad.hideturtle()
- brad.speed(10)
- # edge triangle
- x1 = -w / 2
- y1 = -h / 2
- x2 = w / 2
- y2 = -h / 2
- x3 = 0
- y3 = h / 2
- draw_a_triangle(brad, x1, y1, x2, y2, x3, y3)
- # start recursive
- a = (x1 + x2) / 2
- b = (y1 + y2) / 2
- c = (x1 + x3) / 2
- d = (y1 + y3) / 2
- e = (x2 + x3) / 2
- f = (y2 + y3) / 2
- draw_sub_triangle(brad, 0, a, b, c, d, e, f)
- window.exitonclick()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement