五角星
from tkinter import *
import math as m
root = Tk()
#r1 = R * sin(2 * π/5) ]
#r2 = R * cos(2 * π/5) |}三角函数
#x1 = x - r1 = x - R * sin(2 * π/5)|}
#y1 = y - r2 = y- R * cos(2 * π/5) ]
w = Canvas(root, width=200, height=100)
w.pack()
center_x = 100
center_y = 50
r = 50
points = [
#A
center_x - int(r * m.sin(2 * m.pi / 5)),
center_y - int(r * m.cos(2 * m.pi / 5)),
#C
center_x + int(r * m.sin(2 * m.pi / 5)),
center_y - int(r * m.cos(2 * m.pi / 5)),
#E
center_x - int(r * m.sin(m.pi / 5)),
center_y + int(r * m.cos(m.pi / 5)),
#B
center_x,
center_y - r,
#D
center_x + int(r * m.sin(m.pi / 5)),
center_y + int(r * m.cos(m.pi / 5)),
]
w.create_polygon(points, outline='red', fill='red')
mainloop() 您好!要绘制一个五角星,可以使用`create_polygon`方法,通过计算角度和距离来确定每个顶点的位置。下面是一个完整的示例代码:
from tkinter import *
import math as m
def draw_star(canvas, center_x, center_y, size):
points = []
# 计算五角星每个顶点的坐标
for i in range(5):
angle = m.pi * 2 / 5 * i + m.pi / 2
x = center_x + size * m.sin(angle)
y = center_y - size * m.cos(angle)
points.append(x)
points.append(y)
# 绘制五角星
canvas.create_polygon(points, outline='red', fill='red')
root = Tk()
canvas = Canvas(root, width=200, height=200)
canvas.pack()
center_x = 100
center_y = 100
size = 50
draw_star(canvas, center_x, center_y, size)
mainloop()
在这个例子中,我们定义了一个名为`draw_star`的函数,它接受画布、中心点的坐标和大小作为参数。该函数计算每个顶点的坐标,并使用`create_polygon`方法将这些点连接起来绘制五角星。
在主程序部分,我们创建了一个画布,并调用`draw_star`函数来绘制一个位于`(100, 100)`的中心点,大小为50的五角星。
希望对您有所帮助!如果还有其他问题,请随时提问。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
页:
[1]