python画八卦图

该代码示例展示了如何利用Python的turtle库来绘制八卦图,包括设置窗口大小、背景颜色,画出内外圆弧,添加小圆点,以及绘制三个不同大小的八边形和白色的分割线。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

画八卦图,需要思路和计算

可以用turtle的粗画笔来实现挖空操作 

import turtle
# 创建一个新的画布
win = turtle.Screen()
# 设置窗口大小和背景颜色
win.setup(500,500)
win.bgcolor("white")
# 创建一个turtle对象
t = turtle.Turtle()
#设置八卦图大小
r = 100
# 画圆
t.speed(10)
t.penup()
t.goto(0,-r)
t.pendown()
t.circle(r)
# 画上下部分的黑色圆弧
t.penup()
t.goto(0,0)
t.pendown()
t.begin_fill()
t.pencolor("black")
t.fillcolor("black")
t.circle(r/2,180)
t.circle(r,180)
t.circle(r/2,-180)
t.end_fill()
# 添加两个小圆点
t.pencolor("black")
t.fillcolor("white")
t.begin_fill()
t.penup()
t.goto(0,r/2)
t.pendown()
t.circle(r/15)
t.end_fill()
t.pencolor("black")
t.fillcolor("black")
t.begin_fill()
t.penup()
t.goto(0,-r/2)
t.pendown()
t.circle(r/15)
t.end_fill()
# 三个八边形
t.pensize(12)
t.pencolor("black")
t.penup()
t.goto(-45,108)
t.seth(360)
t.pendown()
for i in range(8):
    t.forward(90)
    t.right(45)
t.penup()
t.goto(-55,132)
t.seth(360)
t.pendown()
for i in range(8):
    t.forward(110)
    t.right(45)
t.penup()
t.goto(-65,156)
t.seth(360)
t.pendown()
for i in range(8):
    t.forward(130)
    t.right(45)
# 画白线
t.penup()
t.pencolor("white")
t.pensize(12)
t.goto(-42,100.8)
t.pendown()
t.seth(112.5)
t.forward(300)
t.pensize(12)
t.penup()
t.goto(42,100.8)
t.pendown()
t.seth(67.5)
t.forward(300)
t.pensize(12)
t.penup()
t.goto(85,85)
t.pendown()
t.seth(45)
t.forward(300)
t.pensize(12)
t.penup()
t.goto(100.8,42)
t.pendown()
t.seth(22.5)
t.forward(300)
t.pensize(12)
t.penup()
t.goto(105.5,0)
t.pendown()
t.seth(0)
t.forward(35)
t.pensize(12)
t.penup()
t.goto(100.8,-42)
t.pendown()
t.seth(-22.5)
t.forward(300)
t.pensize(12)
t.penup()
t.goto(108,-108)
t.pendown()
t.seth(-45)
t.forward(300)
t.pensize(12)
t.penup()
t.goto(42,-100.8)
t.pendown()
t.seth(-67.5)
t.forward(300)
t.penup()
t.goto(-42,-100.8)
t.pendown()
t.seth(-112.5)
t.forward(300)
t.penup()
t.goto(-74.5,-74.5)
t.pendown()
t.seth(-135)
t.forward(300)
t.penup()
t.goto(-100.8,-42)
t.pendown()
t.seth(-157.5)
t.forward(300)
t.penup()
t.goto(-105.5,0)
t.pendown()
t.seth(-180)
t.forward(13)
t.penup()
t.goto(-100.8,42)
t.pendown()
t.seth(-202.5)
t.forward(300)
t.penup()
t.goto(0,105.5)
t.pendown()
t.seth(90)
t.forward(10)
t.penup()
t.goto(0,150)
t.pendown()
t.seth(90)
t.forward(30)
t.penup()
t.goto(0,-127)
t.pendown()
t.seth(-90)
t.forward(12)
# 隐藏turtle
t.hideturtle()
# 程序图形化界面待用户关闭
turtle.done()

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力中的代码人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值