Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. import turtle
  2.  
  3. depth = 3
  4.  
  5.  
  6. def draw_a_line(given_turtle=None, start_x=0, start_y=0, end_x=0, end_y=0):
  7. if given_turtle is None:
  8. given_turtle = turtle.Turtle()
  9.  
  10. given_turtle.penup()
  11. given_turtle.setposition(start_x, start_y)
  12. given_turtle.pendown()
  13. given_turtle.setposition(end_x, end_y)
  14.  
  15.  
  16. def draw_a_triangle(given_turtle, x1, y1, x2, y2, x3, y3):
  17. draw_a_line(given_turtle, x1, y1, x2, y2)
  18. draw_a_line(given_turtle, x1, y1, x3, y3)
  19. draw_a_line(given_turtle, x2, y2, x3, y3)
  20.  
  21.  
  22. def draw_sub_triangle(given_turtle, n, x1, y1, x2, y2, x3, y3):
  23. draw_a_triangle(given_turtle, x1, y1, x2, y2, x3, y3)
  24.  
  25. if n < depth:
  26. n += 1
  27.  
  28. a = (x1 + x2) / 2 + (x2 - x3) / 2
  29. b = (y1 + y2) / 2 + (y2 - y3) / 2
  30. c = (x1 + x2) / 2 + (x1 - x3) / 2
  31. d = (y1 + y2) / 2 + (y1 - y3) / 2
  32. e = (x1 + x2) / 2
  33. f = (y1 + y2) / 2
  34. draw_sub_triangle(given_turtle, n, a, b, c, d, e, f)
  35.  
  36. a = (x3 + x2) / 2 + (x2 - x1) / 2
  37. b = (y3 + y2) / 2 + (y2 - y1) / 2
  38. c = (x3 + x2) / 2 + (x3 - x1) / 2
  39. d = (y3 + y2) / 2 + (y3 - y1) / 2
  40. e = (x3 + x2) / 2
  41. f = (y3 + y2) / 2
  42. draw_sub_triangle(given_turtle, n, a, b, c, d, e, f)
  43.  
  44. a = (x1 + x3) / 2 + (x3 - x2) / 2
  45. b = (y1 + y3) / 2 + (y3 - y2) / 2
  46. c = (x1 + x3) / 2 + (x1 - x2) / 2
  47. d = (y1 + y3) / 2 + (y1 - y2) / 2
  48. e = (x1 + x3) / 2
  49. f = (y1 + y3) / 2
  50. draw_sub_triangle(given_turtle, n, a, b, c, d, e, f)
  51.  
  52.  
  53. window = turtle.Screen()
  54. window.bgcolor("black")
  55. (w, h) = window.screensize()
  56. brad = turtle.Turtle()
  57. brad.setheading(0)
  58. brad.shape("classic")
  59. brad.color("white")
  60. brad.hideturtle()
  61. brad.speed(10)
  62.  
  63. # edge triangle
  64. x1 = -w / 2
  65. y1 = -h / 2
  66. x2 = w / 2
  67. y2 = -h / 2
  68. x3 = 0
  69. y3 = h / 2
  70. draw_a_triangle(brad, x1, y1, x2, y2, x3, y3)
  71.  
  72. # start recursive
  73. a = (x1 + x2) / 2
  74. b = (y1 + y2) / 2
  75. c = (x1 + x3) / 2
  76. d = (y1 + y3) / 2
  77. e = (x2 + x3) / 2
  78. f = (y2 + y3) / 2
  79. draw_sub_triangle(brad, 0, a, b, c, d, e, f)
  80.  
  81. window.exitonclick()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement