python turtle模块(三)画等边三角形,用圆形组成等边三角形阵列
#!/user/bin/env python
# -*- coding:UTF-8 -*-
import math
import turtle
import day2
# 画一个等边三角形 边长作为参数
def draw_a_isoscelestriangle(length):
print(turtle.screensize())
# 设置绘图速度
turtle.speed(1)
turtle.penup()
turtle.setx(300-length/2)
turtle.sety(300-length)
turtle.pendown()
turtle.fd(length)
# 设置画笔角度为120
turtle.seth(120)
turtle.fd(length)
# 设置画笔角度为240度
turtle.seth(-120)
turtle.fd(length)
def draw_a_isoscelestriangle_by_circle(floors, radius):
# 需要画N层 因为range函数是左闭右开的,所以+1处理
floors = floors+1
for floor in range(floors):
turtle.penup()
# 每层X坐标左移一个半径,第0层不左移
turtle.setx(0-radius*floor)
# 上下两层圆正好相切的话,Y坐标需要下移根号下3乘以半径的距离
turtle.sety(300-radius*floor*math.sqrt(3))
# 画每一层的圆
for i in range(floor):
turtle.pendown()
turtle.seth(0)
day2.draw_a_circle(radius)
turtle.penup()
# 画好一个圆,右移一个直径的距离
turtle.fd(2 * radius)
if __name__ == "__main__":
# 等边三角形边长
length = 50
# 圆半径
radius = 50
# 圆的层数
floors = 6
# 画一个等边三角形
draw_a_isoscelestriangle(length)
draw_a_isoscelestriangle_by_circle(floors, radius)
执行效果