python正多边形绘制
时间: 2023-10-17 08:37:25 浏览: 167
要使用Python绘制正多边形,可以使用以下方法:
首先,需要导入turtle库。然后,通过定义函数来绘制正多边形。可以使用get_side_lenth函数计算边长,get_exterior_angle函数计算外角度数。接着,在_init_函数中设置画笔和画布的属性,例如颜色、大小和速度等。最后,使用custom_regular_polygon函数来绘制正多边形。
以下是一个示例代码:
```python
import turtle
import math
def _init_(angle, radius, pencolor='black', bgcolor='white', pensize=25, speed=0):
an = angle
r = radius
turtle.hideturtle()
turtle.pencolor(pencolor)
turtle.bgcolor(bgcolor)
turtle.pensize(pensize)
turtle.speed(speed)
turtle.pu()
turtle.goto(0, r)
turtle.seth(180)
turtle.left(an / 2)
turtle.pd()
def get_side_lenth(radius, number_of_side):
nos = number_of_side
r = radius
ls = math.sin(math.radians(180 / nos)) * 2 * r
return ls
def get_exterior_angle(nos):
an = 360 / nos
return an
def custom_regular_polygon(number_of_side, lenth_of_side, angle):
nos = number_of_side
ls = lenth_of_side
an = angle
for i in range(nos):
turtle.forward(ls)
turtle.left(an)
print(f'正{nos}边形绘画完毕。\nDone.')
# 调用函数绘制正多边形
_init_(radius=100, angle=90)
custom_regular_polygon(number_of_side=4, lenth_of_side=200, angle=90)
turtle.done()
```
这段代码将绘制一个正方形,边长为200,半径为100。
希望这个例子能帮到你。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [python turtle绘制正多边形](https://blog.csdn.net/starlight_2007/article/details/128391732)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文