python用turtle画外切圆
时间: 2023-05-30 12:02:46 浏览: 274
使用Python的turtle模块画图
以下是使用turtle模块绘制外切圆的Python代码示例:
```python
import turtle
import math
# 设置绘图窗口大小
turtle.setup(500, 500)
# 创建画笔
pen = turtle.Turtle()
# 设置画笔颜色和粗细
pen.color("red")
pen.pensize(2)
# 计算外切圆半径
side_length = 100 # 正多边形边长
n = 6 # 正多边形边数
apothem = side_length / (2 * math.tan(math.pi / n)) # 正多边形内切圆半径
circumradius = side_length / (2 * math.sin(math.pi / n)) # 正多边形外接圆半径
# 绘制正多边形
pen.penup()
pen.goto(-side_length/2, -apothem)
pen.pendown()
for i in range(n):
pen.forward(side_length)
pen.left(360/n)
# 绘制外切圆
pen.penup()
pen.goto(0, circumradius)
pen.pendown()
pen.circle(circumradius)
# 隐藏画笔
pen.hideturtle()
# 显示绘图窗口
turtle.done()
```
运行上述代码将绘制一个六边形和其外切圆,结果如下图所示:
![外切圆](https://cdn.jsdelivr.net/gh/1078541234/CDN/blog/20210909130517.png)
阅读全文