绘制圆形三角形 正方形的代码
时间: 2023-03-30 20:01:00 浏览: 98
以下是绘制圆形、三角形和正方形的代码:
绘制圆形:
#include <graphics.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
circle(300, 200, 100);
getch();
closegraph();
return ;
}
绘制三角形:
#include <graphics.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int points[] = {300, 100, 200, 300, 400, 300, 300, 100};
drawpoly(4, points);
getch();
closegraph();
return ;
}
绘制正方形:
#include <graphics.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
rectangle(200, 100, 400, 300);
getch();
closegraph();
return ;
}
相关问题
python在一张图上分别绘制4个子图,分别绘制圆形、正方形、三角形和直线
好的,以下是python代码实现:
```python
import matplotlib.pyplot as plt
import numpy as np
# 创建一个4x4的figure对象,包含4个子图
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(10, 10))
# 绘制圆形
axs[0, 0].set_title('Circle')
circle = plt.Circle((0, 0), 1, color='blue', fill=False)
axs[0, 0].add_artist(circle)
# 绘制正方形
axs[0, 1].set_title('Square')
square = plt.Rectangle((-1, -1), 2, 2, color='green', fill=False)
axs[0, 1].add_artist(square)
# 绘制三角形
axs[1, 0].set_title('Triangle')
triangle = plt.Polygon(np.array([[-1, -1], [1, -1], [0, 1]]), color='red', fill=False)
axs[1, 0].add_artist(triangle)
# 绘制直线
axs[1, 1].set_title('Line')
axs[1, 1].plot([-1, 1], [0, 0], color='purple', linewidth=3)
# 调整子图之间的间距
plt.subplots_adjust(wspace=0.4, hspace=0.4)
# 显示图形
plt.show()
```
以上代码会生成一个4x4的figure对象,包含四个子图。第一个子图绘制圆形,第二个子图绘制正方形,第三个子图绘制三角形,第四个子图绘制直线。每个子图都有一个标题。我们使用`add_artist`方法将圆形、正方形和三角形添加到对应的子图中,使用`plot`方法绘制直线。最后使用`subplots_adjust`方法调整子图之间的间距,并使用`show`方法显示图形。
3.在一张图上分别绘制4个子图,分别绘制圆形、正方形、三角形和直线,如附图 11-2所示。
抱歉,我是一个语言模型 AI,无法进行图像绘制。但我可以告诉你如何使用 Python 中的 Matplotlib 库来绘制这四个子图。
首先,你需要导入 Matplotlib 库和 NumPy 库:
```python
import matplotlib.pyplot as plt
import numpy as np
```
然后,你可以使用 Matplotlib 的子图功能来创建四个子图:
```python
fig, axs = plt.subplots(2, 2)
# 绘制圆形
axs[0, 0].add_artist(plt.Circle((0.5, 0.5), 0.4, color='r'))
axs[0, 0].set_xlim(0, 1)
axs[0, 0].set_ylim(0, 1)
# 绘制正方形
axs[0, 1].add_artist(plt.Rectangle((0.2, 0.2), 0.6, 0.6, color='g'))
axs[0, 1].set_xlim(0, 1)
axs[0, 1].set_ylim(0, 1)
# 绘制三角形
axs[1, 0].plot([0.1, 0.5, 0.9], [0.1, 0.9, 0.1], color='b')
axs[1, 0].set_xlim(0, 1)
axs[1, 0].set_ylim(0, 1)
# 绘制直线
axs[1, 1].plot([0.1, 0.9], [0.5, 0.5], color='k')
axs[1, 1].set_xlim(0, 1)
axs[1, 1].set_ylim(0, 1)
plt.show()
```
这样就可以得到四个子图,分别绘制圆形、正方形、三角形和直线。