-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathsierpinski_triangle.py
51 lines (40 loc) · 1.11 KB
/
sierpinski_triangle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# informasi tentang sierpinksi triangle
# https://mathigon.org/course/fractals/sierpinski
import turtle
name = "sierpinksi triangle"
points = [
[
-175,
-125,
],
[0, 175],
[175, -125],
]
def get_mid(p1, p2):
return ((p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2)
def triangle(points, depth):
drawing.up()
drawing.goto(points[0][0], points[0][1])
drawing.down()
drawing.goto(points[1][0], points[1][1])
drawing.goto(points[2][0], points[2][1])
drawing.goto(points[0][0], points[0][1])
if depth > 0:
triangle(
[points[0], get_mid(points[0], points[1]), get_mid(points[0], points[2])],
depth - 1,
)
triangle(
[points[1], get_mid(points[0], points[1]), get_mid(points[1], points[2])],
depth - 1,
)
triangle(
[points[2], get_mid(points[2], points[1]), get_mid(points[0], points[2])],
depth - 1,
)
if __name__ == "__main__":
drawing = turtle.Turtle()
# drawing.ht()
# drawing.speed(5)
# drawing.pencolor("black")
# triangle(points, 4)