写一个函数: regular_polygon(n, x, y, col, border), 能绘制出正n边形, 且多边形中心默认在原点, 通过col和border可以设置正多边形内部和边框的颜色,在调用画出图形
时间: 2023-06-23 09:09:44 浏览: 46
下面是使用Python的turtle库实现的代码:
```python
import turtle
def regular_polygon(n, x, y, col, border):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.fillcolor(col)
turtle.begin_fill()
for i in range(n):
turtle.forward(100)
turtle.right(360 / n)
turtle.end_fill()
turtle.pencolor(border)
for i in range(n):
turtle.forward(100)
turtle.right(360 / n)
turtle.done()
# 测试代码
regular_polygon(5, 0, 0, 'red', 'blue')
```
函数接受五个参数,分别为多边形边数n、中心点坐标x和y、内部填充颜色col、边框颜色border。
在函数内部,我们使用turtle库来绘制多边形。首先把画笔移动到中心点,然后开始填充内部颜色,接着绘制多边形的边界。
最后,我们调用turtle.done()函数来保持窗口打开状态,直到手动关闭。测试代码中,我们绘制了一个5边形,并设置了红色填充和蓝色边框。
相关问题
用R语言写一个函数: regular_polygon(n, x, y, col, border), 能绘制出正n边形, 且多边形中心默认在原点, 通过col和border可以设置正多边形内部和边框的颜色
以下是使用R语言编写的函数 `regular_polygon()`:
```r
regular_polygon <- function(n, x=0, y=0, col="black", border="white") {
angles <- seq(0, 2*pi, length.out = n+1)[-1]
vertices <- cbind(x + cos(angles), y + sin(angles))
polygon(vertices, col=col, border=border)
}
```
这个函数接受五个参数:
- `n`: 正多边形的边数
- `x`: 多边形中心的x坐标,默认为0
- `y`: 多边形中心的y坐标,默认为0
- `col`: 多边形内部的颜色,默认为黑色
- `border`: 多边形边框的颜色,默认为白色
该函数首先计算出正n边形各个顶点的坐标,然后使用 `polygon()` 函数绘制出多边形。`polygon()` 函数会自动将第一个顶点和最后一个顶点相连,因此在计算顶点坐标时,需要在正n边形的角度序列中去掉第一个元素。
例如,要绘制一个红色的边框、蓝色的内部的正五边形,可以这样调用 `regular_polygon()` 函数:
```r
regular_polygon(n=5, col="blue", border="red")
```
利用 python 自定义一个正多边形函数 reg_polygon( ),至少设置两个形参:边数 n,边长 length
### 回答1:
下面是一个使用 Python 实现自定义正多边形函数的示例代码:
```
import math
def reg_polygon(n, length):
angle = 360 / n
radius = length / (2 * math.sin(math.pi / n))
perimeter = n * length
area = 0.5 * n * radius ** 2 * math.sin(2 * math.pi / n)
return perimeter, area
```
这个函数接收两个参数:`n` 表示多边形的边数,`length` 表示边长。它返回一个元组,分别包含多边形的周长和面积。
### 回答2:
可以使用 Python 自定义一个正多边形函数 `reg_polygon()`,代码如下:
```python
import turtle
def reg_polygon(n, length):
angle = 360 / n # 每个内角的度数
for _ in range(n):
turtle.forward(length)
turtle.right(angle)
# 测试
reg_polygon(3, 100) # 调用函数绘制一个边长为100的正三角形
turtle.done()
```
上述代码使用`turtle`库绘制一个正多边形,其中`n`表示多边形的边数,`length`表示每条边的长度。首先,根据边数 `n` 计算每个内角的度数 `angle`,然后循环 `n` 次,每次向前移动 `length` 的距离,然后向右旋转 `angle` 的角度。运行上述代码,绘制了一个边长为 100 的正三角形。
通过调用 `reg_polygon()` 函数,可以绘制不同边数和边长的正多边形。
### 回答3:
利用 Python 自定义一个正多边形函数 reg_polygon(),可以实现绘制一个正多边形。该函数至少需要设置两个形参,即边数 n 和边长 length。
下面是一个满足要求的 reg_polygon() 函数的示例实现:
```python
import turtle
def reg_polygon(n, length):
angle = 360 / n
pen = turtle.Turtle()
pen.penup()
pen.goto(-length / 2, length / 2)
pen.pendown()
for _ in range(n):
pen.forward(length)
pen.right(angle)
turtle.done()
```
这个函数使用 turtle 库来绘制图形。先计算出每个角度需要旋转的角度 angle,然后创建一个 Turtle 对象 pen。通过调用 pen 的一系列方法,包括 pen.penup()、pen.goto()、pen.pendown()、pen.forward() 和 pen.right(),依次绘制出多边形的各边。
函数中的循环部分会执行 n 次,每次向前移动 length 的距离,然后右转 angle 角度,实现了正多边形的绘制。
最后调用 turtle.done() 函数,保持绘图窗口打开,直到手动关闭。
要使用该函数,可以在程序中调用 reg_polygon() 函数并传递合适的参数,例如:
```python
reg_polygon(6, 100)
```
这样就会绘制一个边数为 6,边长为 100 的正六边形。
注意:在运行这段代码前需要确保已经安装了 turtle 模块。如果没有安装,则可以在命令行终端中执行以下命令来安装:
```python
pip install turtle
```
希望以上信息对你有帮助!
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)