python定义函数绘制多边形
时间: 2023-10-14 15:20:24 浏览: 37
可以使用Python的turtle模块来绘制多边形。以下是一个绘制正多边形的示例代码:
```python
import turtle
def draw_polygon(sides, length):
angle = 360 / sides
for i in range(sides):
turtle.forward(length)
turtle.right(angle)
turtle.speed(1)
draw_polygon(5, 100)
turtle.done()
```
在这个示例中,`draw_polygon`函数接受`side`参数表示多边形的边数,`length`参数表示每条边的长度。函数计算出每个内角的度数,然后使用循环语句绘制多边形。`turtle.forward(length)`函数向前移动指定的距离,`turtle.right(angle)`函数向右旋转指定的角度。
你可以根据需要更改`draw_polygon`函数参数的值来绘制不同的多边形。
相关问题
python正多边形绘制
要使用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 ]
python绘制泰森多边形
Python可以使用scipy库中的Delaunay三角剖分函数和shapely库中的Polygon函数来绘制泰森多边形。具体步骤如下:
1. 导入必要的库:numpy、scipy、matplotlib、shapely。
2. 生成随机点集。
3. 使用scipy库中的Delaunay函数对点集进行三角剖分。
4. 使用shapely库中的Polygon函数将每个三角形转换为多边形。
5. 计算每个多边形的重心。
6. 使用matplotlib库绘制泰森多边形和点集。
代码如下(代码类型为Python):
```
import numpy as np
from scipy.spatial import Delaunay
import matplotlib.pyplot as plt
from shapely.geometry import Polygon, Point
# 生成随机点集
points = np.random.rand(30, 2)
# 进行三角剖分
tri = Delaunay(points)
# 将每个三角形转换为多边形
polygons = []
for i in range(tri.nsimplex):
polygon = Polygon(points[tri.vertices[i]])
polygons.append(polygon)
# 计算每个多边形的重心
centroids = []
for polygon in polygons:
centroid = polygon.centroid
centroids.append(centroid)
# 绘制泰森多边形和点集
fig, ax = plt.subplots()
for polygon in polygons:
ax.add_patch(plt.Polygon(polygon.exterior))
ax.plot(points[:,0], points[:,1], 'o')
plt.show()
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)