turtle绘制八卦镜

预览效果
turtle绘制八卦镜
效果图

import turtle as t
from math import sin, cos, tan, pi


# 阴爻(yáo)
def yin(size, color='black'):
    # 初始化画笔
    t.pensize(size // 8)
    t.pencolor(color)
    # 初始化位置
    t.pu()
    t.fd(-size / 2)

    # 落笔
    t.pd()
    # 绘制阴爻
    t.fd(size / 3)
    t.pu()
    t.fd(size / 3)
    t.pd()
    t.fd(size / 3)
    # 抬笔
    t.pu()
    # 返回起始位置
    t.fd(-size / 2)


# 阳爻(yáo)
def yang(size, color='red'):
    # 初始化画笔颜色
    t.pensize(size // 8)
    t.pencolor(color)
    # 初始化位置
    t.pu()
    t.fd(-size / 2)

    # 落笔
    t.pd()
    # 绘制阳爻
    t.fd(size)
    # 抬笔
    t.pu()
    # 返回起始位置
    t.fd(-size / 2)


def out(content, pos=(0, 0), size=25, color='blue'):
    t.setpos(pos[0], pos[1])
    t.pencolor(color)
    t.write(content, font=("Arial", size), align="center")


'''
乾三连(天)      离中空(火)
兑上缺(泽)      震仰盅(雷)
巽下断(风)      坎中满(水)
艮覆碗(山)      坤六断(地)'''


# 坎中满     [水]
def kan(size, heading=90):
    # 默认垂直向上方向为90°
    # 计算阳爻绘制角度
    h = heading - 90
    # 设置画笔角度
    t.seth(h)
    # 计算x坐标偏移量
    x = 0.33 * size * cos(heading * pi / 180)
    # 计算y坐标偏移量
    y = 0.33 * size * sin(heading * pi / 180)
    # 绘制第一个阳爻
    yin(size)
    # 移动画笔
    t.setpos(t.pos()[0] + x, t.pos()[1] + y)
    # 绘制第二个阳爻
    yang(size)
    # 移动画笔
    t.setpos(t.pos()[0] + x, t.pos()[1] + y)
    # 绘制第三个阳爻
    yin(size)


# 震仰盅     [雷]
def zhen(size, heading=90):
    # 默认垂直向上方向为90°
    # 计算阳爻绘制角度
    h = heading - 90
    # 设置画笔角度
    t.seth(h)
    # 计算x坐标偏移量
    x = 0.33 * size * cos(heading * pi / 180)
    # 计算y坐标偏移量
    y = 0.33 * size * sin(heading * pi / 180)
    # 绘制第一个阳爻
    yin(size)
    # 移动画笔
    t.setpos(t.pos()[0] + x, t.pos()[1] + y)
    # 绘制第二个阳爻
    yin(size)
    # 移动画笔
    t.setpos(t.pos()[0] + x, t.pos()[1] + y)
    # 绘制第三个阳爻
    yang(size)


# 巽下断   [风]
def xun(size, heading=90):
    # 默认垂直向上方向为90°
    # 计算阳爻绘制角度
    h = heading - 90
    # 设置画笔角度
    t.seth(h)
    # 计算x坐标偏移量
    x = 0.33 * size * cos(heading * pi / 180)
    # 计算y坐标偏移量
    y = 0.33 * size * sin(heading * pi / 180)
    # 绘制第一个阳爻
    yin(size)
    # 移动画笔
    t.setpos(t.pos()[0] + x, t.pos()[1] + y)
    # 绘制第二个阳爻
    yang(size)
    # 移动画笔
    t.setpos(t.pos()[0] + x, t.pos()[1] + y)
    # 绘制第三个阳爻
    yang(size)


# 艮覆碗   [山]
def gen(size, heading=90):
    # 默认垂直向上方向为90°
    # 计算阳爻绘制角度
    h = heading - 90
    # 设置画笔角度
    t.seth(h)
    # 计算x坐标偏移量
    x = 0.33 * size * cos(heading * pi / 180)
    # 计算y坐标偏移量
    y = 0.33 * size * sin(heading * pi / 180)
    # 绘制第一个阳爻
    yin(size)
    # 移动画笔
    t.setpos(t.pos()[0] + x, t.pos()[1] + y)
    # 绘制第二个阳爻
    yin(size)
    # 移动画笔
    t.setpos(t.pos()[0] + x, t.pos()[1] + y)
    # 绘制第三个阳爻
    yang(size)


# 坤(kūn)六断  [地]
def kun(size, heading=90):
    # 默认垂直向上方向为90°
    # 计算阳爻绘制角度
    h = heading - 90
    # 设置画笔角度
    t.seth(h)
    # 计算x坐标偏移量
    x = 0.33 * size * cos(heading * pi / 180)
    # 计算y坐标偏移量
    y = 0.33 * size * sin(heading * pi / 180)
    # 绘制第一个阳爻
    yin(size)
    # 移动画笔
    t.setpos(t.pos()[0] + x, t.pos()[1] + y)
    # 绘制第二个阳爻
    yin(size)
    # 移动画笔
    t.setpos(t.pos()[0] + x, t.pos()[1] + y)
    # 绘制第三个阳爻
    yin(size)


# 兑上缺(泽)    [泽]
def dui(size, heading=90):
    # 默认垂直向上方向为90°(由内而外,从下至上)
    # 计算阳爻绘制角度
    h = heading - 90
    # 设置画笔角度
    t.seth(h)
    # 计算x坐标偏移量
    x = 0.33 * size * cos(heading * pi / 180)
    # 计算y坐标偏移量
    y = 0.33 * size * sin(heading * pi / 180)
    # 绘制第一个阳爻
    yang(size)
    # 移动画笔
    t.setpos(t.pos()[0] + x, t.pos()[1] + y)
    # 绘制第二个阳爻
    yang(size)
    # 移动画笔
    t.setpos(t.pos()[0] + x, t.pos()[1] + y)
    # 绘制第三个阳爻
    yin(size)


# 乾(qián)三连 [天]
def qian(size, heading=90):
    # 默认垂直向上方向为90°
    # 计算阳爻绘制角度
    h = heading - 90
    # 设置画笔角度
    t.seth(h)
    # 计算x坐标偏移量
    x = 0.33 * size * cos(heading * pi / 180)
    # 计算y坐标偏移量
    y = 0.33 * size * sin(heading * pi / 180)
    # 绘制第一个阳爻
    yang(size)
    # 移动画笔
    t.setpos(t.pos()[0] + x, t.pos()[1] + y)
    # 绘制第二个阳爻
    yang(size)
    # 移动画笔
    t.setpos(t.pos()[0] + x, t.pos()[1] + y)
    # 绘制第三个阳爻
    yang(size)


# 离中虚   [火]
def li(size, heading=90):
    # 默认垂直向上方向为90°
    # 计算阳爻绘制角度
    h = heading - 90
    # 设置画笔角度
    t.seth(h)
    # 计算x坐标偏移量
    x = 0.33 * size * cos(heading * pi / 180)
    # 计算y坐标偏移量
    y = 0.33 * size * sin(heading * pi / 180)
    # 绘制第一个阳爻
    yang(size)
    # 移动画笔
    t.setpos(t.pos()[0] + x, t.pos()[1] + y)
    # 绘制第二个阳爻
    yin(size)
    # 移动画笔
    t.setpos(t.pos()[0] + x, t.pos()[1] + y)
    # 绘制第三个阳爻
    yang(size)


# 太极
def taiji(size, coor=(0, 0)):
    t.pensize(1)
    coor  = (coor[0], coor[1] - size)
    t.pu()
    t.goto(coor[0], coor[1])
    t.color('black', 'black')
    # 绘制黑色鱼
    t.pd()
    t.begin_fill()
    t.circle(size, 180)

    t.circle(size / 2, 180)
    t.left(180)
    t.circle(size / 2, -180)
    t.end_fill()
    # 绘制红色鱼
    t.color('red', 'red')
    t.begin_fill()
    t.circle(size, -180)

    t.circle(size / 2, 180)
    t.left(180)
    t.circle(size / 2, -180)
    t.end_fill()

    # 绘制黑色鱼眼睛
    t.pu()
    t.color('black', 'black')
    t.left(90)
    t.fd(size / 2)
    t.pd()
    t.begin_fill()
    t.circle(-size // 8)
    t.end_fill()

    # 绘制红色鱼眼睛
    t.color('red', 'red')
    t.pu()
    t.fd(size)
    t.pd()
    t.begin_fill()
    t.circle(size // 8)
    t.end_fill()
    t.pu()


# 八卦
def draw_bagua(radius, size,order=[qian, kun, xun, dui, kan, li, zhen,gen]):

    for i in range(0, 360, 45):
        t.setpos(radius * cos(i * pi / 180), radius * sin(i * pi / 180))
        order[i//45](size, i)
    else:
        t.pu()


# 八边形外框
def margin(size):
    # 计算八边形的高
    t.pu()
    hight = size * sin(67.5 * pi / 180) + 20
    length = hight * 2 * tan(22.5 * pi / 180)
    t.home()
    t.sety(hight)


    t.fd(-length/2)
    t.pd()
    t.pensize(8)
    t.color('black', 'gray')
    t.begin_fill()
    for i in range(8):
        t.fd(length)
        t.right(45)
    t.end_fill()


def main():
    t.setup(800, 800)
    t.hideturtle()
    # t.speed(10)
    margin(260)
    taiji(120)
    draw_bagua(150, 70)
    t.done()


if __name__ == '__main__':
    main()

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值