1. 绘制空白的三角形
- 画一个绿色实心等边三角形
- 沿三条边中心点的连线
- 连好后,会多一个三角形,将这个三角形填充为白色
- 这个时候左 右 上 分别有三角形
- 对这三个小三角形重复 1,2和3 ,直到条件满足时停止
import turtle as t
def fun(x1,y1,x2,y2,x3,y3,c):
t.penup()
t.goto(x1,y1)
t.pendown()
t.color(c)
t.begin_fill()
t.goto(x2,y2)
t.goto(x3,y3)
t.goto(x1,y1)
t.end_fill()
def fun2(x1,y1,x2,y2,x3,y3):
if abs(x1 - x2) < 40:
return
else:
x4,y4 = (x1 + x2)/2 ,(y1 + y2)/2
x5,y5 = (x2 + x3)/2 ,(y2 + y3)/2
x6,y6 = (x1 + x3)/2 ,(y1 + y3)/2
fun(x4,y4,x5,y5,x6,y6,'white')
fun2(x1,y1,x4,y4,x6,y6)
fun2(x4,y4,x2,y2,x5,y5)
fun2(x6,y6,x5,y5,x3,y3)
x1,y1 = -100,0
x2,y2 = 100,0
x3,y3 = 0,(200*200-100*100) ** 0.5
fun(x1,y1,x2,y2,x3,y3,'green')
fun2(x1,y1,x2,y2,x3,y3)
t.ht()
t.done()
2. 绘制绿色的三角形
import turtle as t
def fun(d):
if d < 40:
t.begin_fill()
for i in range(3):
t.forward(d)
t.left(120)
t.end_fill()
else:
fun(d/2)
t.forward(d/2)
fun(d/2)
t.left(120)
t.forward(d/2)
t.right(120)
fun(d/2)
t.left(60)
t.backward(d/2)
t.right(60)
t.speed(0)
t.color('green')
fun(200)
t.ht()
t.done()