用非常简陋的方法画谢尔宾斯基三角形
废话不多直接上代码(基于python3)half = 70
flag0 = ' '
flag1 = '0'
a = [*half++*half]
b_0 =
b_1 =
b_2 =
b_3 =
b_4 =
b_5 =
b_6 =
b_7 =
for i in range(1,half):
a.append([])
for j in range(2*half+1):
if j == 0 or j == 2*half:
a.append(flag0)
else:
c = a
if c == b_0 or c == b_2 or c == b_5 or c == b_6 or c == b_3:
a.append(flag0)
elif c == b_1 or c == b_4 or c == b_7:
a.append(flag1)
for i in range(half):
print()
for j in range(2*half+1):
print(a,end="")
half可以自己改变长度
flag0和flag1的字符也可以自己改变二者长度相等就不影响结果
修改参数
half = 50
flag0 = '1'
flag1 = ' '
import turtle
def Sierpinski(size,times,total_angle):
if times == 0:
turtle.fd(size)
else:
for angle in :
total_angle += angle
turtle.right(angle)
total_angle = total_angle%180
if total_angle == 0:
Sierpinski(size/2,times-1,total_angle)
else:
turtle.fd(size/2)
def main():
turtle.setup(800,800)
turtle.penup()
turtle.goto(-300, -300)
turtle.pendown()
turtle.pensize(2)
for angle in :
turtle.left(angle)
turtle.fd(size)
turtle.right(120)
total_angle=0
Sierpinski(size,times,total_angle)
turtle.hideturtle()
size = 600
times = 5
main()
复杂一点的,times自行设置 大佬牛逼 大佬牛
厉害!{:10_257:}
页:
[1]