使用Python绘制十字架可以通过多种方法实现,以下是使用`turtle`库和`matplotlib`库的两种方法:
使用`turtle`库绘制十字架
import turtle
设置画布大小和画笔颜色
turtle.screensize(800, 800, "white")
turtle.pencolor("red")
turtle.pensize(1)
turtle.speed(50)
开始填充图案
turtle.fillcolor("red")
turtle.begin_fill()
移动画笔绘制十字
turtle.goto(25, 0)
turtle.goto(25, -5)
turtle.goto(0, -5)
turtle.goto(0, 0)
放下画笔,移动时会绘制图像
turtle.penup()
turtle.goto(10, 10)
turtle.pendown()
turtle.goto(15, 10)
turtle.goto(15, -35)
turtle.goto(10, -35)
turtle.goto(10, 10)
结束颜色填充
turtle.end_fill()
使图像一直显示在屏幕上
turtle.mainloop()
使用`matplotlib`库绘制十字架
import matplotlib.pyplot as plt
import numpy as np
创建一个空白图像
im = np.zeros((400, 400, 3), dtype=np.uint8)
在图像中央绘制十字
cross_length = 100
center_x = im.shape // 2
center_y = im.shape // 2
使用cv2.polylines函数绘制多条线
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
cv2.polylines(im, [np.array([[center_x - cross_length, center_y], [center_x + cross_length, center_y], [center_x, center_y + cross_length], [center_x, center_y - cross_length]])], True, 255, 3)
显示图像
plt.imshow(im)
plt.show()
以上代码展示了如何使用`turtle`和`matplotlib`库在Python中绘制十字架。您可以根据需要选择合适的方法