编写初级程序实现绘制三角形、正方形、五边形、六边形以及圆,并填充不同的颜色,并在图形下方标注图形名称
时间: 2024-10-04 11:02:37 浏览: 32
Wincc7.5中如何绘制不规则图形并填充背景颜色?.docx
5星 · 资源好评率100%
为了在控制台上实现基本形状的绘制并添加颜色及标注,我们将使用文本描述的方式,因为控制台并不支持直接绘制图像和颜色。这里我将给出一个简单的Python示例,使用ASCII艺术来表示这些形状:
```python
def draw_shape(shape_name, sides):
print(f"{shape_name} ({sides} sides):")
if shape_name == "三角形":
for i in range(3):
print(" *" * (i + 1) + " /\\ " + " *" * (i + 1))
print("\n")
elif shape_name == "正方形":
for _ in range(sides):
print("*" * sides)
print("\n")
elif shape_name == "五边形":
side_length = 5
angle = 360 // sides
for _ in range(sides):
for _ in range(side_length):
print("*", end="")
print("\n", end="\r")
for _ in range(angle):
print(" ", end="")
print("\n")
elif shape_name == "六边形":
side_length = 6
angle = 360 // sides
for _ in range(sides):
for _ in range(side_length):
print("*", end="")
print("\n", end="\r")
for _ in range(angle - 90):
print(" ", end="")
print("\n")
elif shape_name == "圆形":
print("由于这是文本环境,我们无法直接画出完美的圆形,但可以用星号模拟:")
print(" " * 10 + "*" * 18 + " " * 10)
print(" " * 7 + "*" * 14 + " O " + "*" * 14 + " " * 7)
print(" " * 10 + "*" * 18 + " " * 10)
print("\n")
# 绘制每个形状
draw_shape("三角形", 3)
draw_shape("正方形", 4)
draw_shape("五边形", 5)
draw_shape("六边形", 6)
draw_shape("圆形", None) # 圆形不需要明确的边数
```
阅读全文